首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ethers.js合同工厂

Ethers.js合同工厂
EN

Stack Overflow用户
提问于 2022-03-12 15:00:06
回答 1查看 2.1K关注 0票数 0

我试图在ethers.js上制作一个ethers.js,并通过炼金术在波利贡的测试网孟买部署智能合同。

我对deploy函数有一个问题,就像在文档中一样,不清楚如何格式化参数。

作为炼金术文档,我必须指定gasLimitgasPrice,但我也希望为契约的构造函数指定自定义参数。

我所犯的错误:

  • 如果我只输入构造函数的参数部署函数,它就会说我没有指定gasLimitgasPrice参数
  • 如果我输入deploy函数、构造函数以及gasLimitgasPrice的参数,那么由于参数的顺序,智能契约的构造函数会出现问题。
  • 如果我只放置gasLimitgasPrice,它将正确部署,但我没有我想要的自定义智能契约,因为构造函数的参数丢失了;

这是部署智能契约的代码段:

代码语言:javascript
复制
const price_unit = "gwei";
const contractFile = await fs.readFileSync('artifacts/contracts/Midly.sol/NFTCollectible.json');
const contract = JSON.parse(contractFile.toString());

const provider = new ethers.providers.AlchemyProvider("maticmum", process.env.ALCHEMY_API_KEY);
const wallet = new ethers.Wallet(process.env.WALLET_PRIVATE_KEY, provider);
const price = ethers.utils.formatUnits(await provider.getGasPrice(), price_unit);

const factory = new ethers.ContractFactory(contract.abi, contract.bytecode, wallet);

const deployedContract = await factory.deploy({
  gasLimit: 2,
  gasPrice: ethers.utils.parseUnits(price, price_unit),
}, tokenURI, maxQuantity, cost)
.then(data => console.log(data))
.catch(error => console.log(error));

谢谢你抽出时间:)

EN

回答 1

Stack Overflow用户

发布于 2022-03-12 16:08:24

您需要首先传递构造函数参数,最后传递overrides对象。

代码语言:javascript
复制
const deployedContract = await factory.deploy(tokenURI, maxQuantity, cost, {
  gasLimit: 2,
  gasPrice: ethers.utils.parseUnits(price, price_unit),
})

博士:https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory-deploy

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71450647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档