UNPKG

@nomiclabs/buidler-ethers

Version:
70 lines 3.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const plugins_1 = require("@nomiclabs/buidler/plugins"); const ethers_1 = require("ethers"); async function getSigners(bre) { const accounts = await bre.ethers.provider.listAccounts(); return accounts.map((account) => bre.ethers.provider.getSigner(account)); } exports.getSigners = getSigners; async function getContractFactory(bre, nameOrAbi, bytecodeOrSigner, signer) { if (typeof nameOrAbi === "string") { return getContractFactoryByName(bre, nameOrAbi, bytecodeOrSigner); } return getContractFactoryByAbiAndBytecode(bre, nameOrAbi, bytecodeOrSigner, signer); } exports.getContractFactory = getContractFactory; async function getContractFactoryByName(bre, name, signer) { const artifact = await plugins_1.readArtifact(bre.config.paths.artifacts, name); return getContractFactoryByAbiAndBytecode(bre, artifact.abi, artifact.bytecode, signer); } exports.getContractFactoryByName = getContractFactoryByName; async function getContractFactoryByAbiAndBytecode(bre, abi, bytecode, signer) { if (signer === undefined) { const signers = await bre.ethers.getSigners(); signer = signers[0]; } const abiWithAddedGas = addGasToAbiMethodsIfNecessary(bre.network.config, abi); return new bre.ethers.ContractFactory(abiWithAddedGas, bytecode, signer); } exports.getContractFactoryByAbiAndBytecode = getContractFactoryByAbiAndBytecode; async function getContractAt(bre, nameOrAbi, address, signer) { if (typeof nameOrAbi === "string") { const factory = await getContractFactoryByName(bre, nameOrAbi, signer); return factory.attach(address); } if (signer === undefined) { const signers = await bre.ethers.getSigners(); signer = signers[0]; } const abiWithAddedGas = addGasToAbiMethodsIfNecessary(bre.network.config, nameOrAbi); return new bre.ethers.Contract(address, abiWithAddedGas, signer); } exports.getContractAt = getContractAt; // This helper adds a `gas` field to the ABI function elements if the network // is set up to use a fixed amount of gas. // This is done so that ethers doesn't automatically estimate gas limits on // every call. function addGasToAbiMethodsIfNecessary(networkConfig, abi) { if (networkConfig.gas === "auto" || networkConfig.gas === undefined) { return abi; } // ethers adds 21000 to whatever the abi `gas` field has. This may lead to // OOG errors, as people may set the default gas to the same value as the // block gas limit, especially on Buidler EVM. // To avoid this, we substract 21000. // HOTFIX: We substract 1M for now. See: https://github.com/ethers-io/ethers.js/issues/1058#issuecomment-703175279 const gasLimit = ethers_1.ethers.BigNumber.from(networkConfig.gas) .sub(1000000) .toHexString(); const modifiedAbi = []; for (const abiElement of abi) { if (abiElement.type !== "function") { modifiedAbi.push(abiElement); continue; } modifiedAbi.push(Object.assign(Object.assign({}, abiElement), { gas: gasLimit })); } return modifiedAbi; } //# sourceMappingURL=helpers.js.map