简体   繁体   English

Ethers.js 合约工厂

[英]Ethers.js Contract Factories

I'm trying to make a ContractFactory on ethers.js and deploy the smart contract on Polygon's tes.net Mumbai through Alchemy.我正在尝试在 ethers.js 上创建一个 ContractFactory,并通过 Alchemy 在 Polygon 的 tes.net Mumbai 上部署智能合约。

I have a problem with the deploy function as in the docs it isn't clear how to format arguments.我在部署 function 时遇到问题,因为在文档中不清楚如何格式化 arguments。

As Alchemy docs I have to specify gasLimit and gasPrice , but I also want to specify custom arguments for my contract's constructor.作为 Alchemy 文档,我必须指定gasLimitgasPrice ,但我还想为我的合约构造函数指定自定义 arguments 。

The errors that I get:我得到的错误:

  • if I put in the deploy function just the parameters of the constructor it says that I didn't specified the gasLimit and gasPrice parameters如果我在部署 function 中只输入构造函数的参数,它表示我没有指定gasLimitgasPrice参数
  • if I put in the deploy function the parameters of the constructor and gasLimit and gasPrice I have a problem with the smart contract's constructor because of the parameter's order.如果我在部署 function 中放入构造函数和gasLimitgasPrice的参数,由于参数的顺序,智能合约的构造函数出现问题。
  • If I put just gasLimit and gasPrice it deploys correctly but I don't have the custom smart contract that I want as there are the constructor's parameters missing;如果我只gasLimitgasPrice ,它会正确部署,但我没有我想要的自定义智能合约,因为缺少构造函数的参数;

this is the piece of code that deploys the smart contract:这是部署智能合约的一段代码:

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));

Thanks for your time:)谢谢你的时间:)

You need to pass the constructor arguments first, and the overrides object last.您需要首先传递构造函数 arguments,最后传递overrides object。

const deployedContract = await factory.deploy(tokenURI, maxQuantity, cost, {
  gasLimit: 2,
  gasPrice: ethers.utils.parseUnits(price, price_unit),
})

Docs: https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory-deploy文档: https://docs.ethers.io/v5/api/contract/contract-factory/#ContractFactory-deploy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM