'Ethers.js Contract Factories
I'm trying to make a ContractFactory on ethers.js and deploy the smart contract on Polygon's testnet 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
gasLimitandgasPriceparameters - if I put in the deploy function the parameters of the constructor and
gasLimitandgasPriceI have a problem with the smart contract's constructor because of the parameter's order. - If I put just
gasLimitandgasPriceit 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 :)
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
