简体   繁体   中英

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.

I have a problem with the deploy function as in the docs it isn't clear how to format arguments.

As Alchemy docs I have to specify gasLimit and gasPrice , but I also want to specify custom arguments for my contract's constructor.

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
  • 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.
  • 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;

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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