Load contract objects from built artifacts or ABIs. Includes support for both web3-eth-contract and @truffle/contract objects.
npm install @openzeppelin/contract-loader
You will also need to install web3-eth-contract
and/or @truffle/contract
, depending on which abstractions you want to be able to load.
const { setupLoader } = require('@openzeppelin/contract-loader');
const loader = setupLoader({
provider,
defaultSender, // optional
defaultGas, // optional - defaults to 8 million
});
const web3Loader = loader.web3;
// Load from artifacts built by the compiler (stored in .json files)
const ERC20 = web3Loader.fromArtifact('ERC20');
// Or load directly from an ABI
const abi = [ ... ];
const ERC20 = web3Loader.fromABI(abi);
// Deploy token
const token = await ERC20.deploy().send();
// Query blockchain state and send transactions
const balance = await token.methods.balanceOf(sender).call();
await token.methods.transfer(receiver, balance).send({ from: sender });
const truffleLoader = loader.truffle;
// Load from artifacts built by the compiler (stored in .json files)
const ERC20 = truffleLoader.fromArtifact('ERC20');
// Or load directly from an ABI
const abi = [ ... ];
const ERC20 = truffleLoader.fromABI(abi);
// Deploy token
const token = await ERC20.new();
// Query blockchain state and send transactions
const balance = await token.balanceOf(sender);
await token.transfer(receiver, balance, { from: sender });