0

I have a smart contract with a payable constructor. I'm trying to figure out the syntax with ethers js how I can deploy the smart contract and send an initial balance with the deployment.

const initialBalance = ethers.utils.parseEther("0.1");
const myContract = await ethers.getContractFactory('MyContract');   
const deployedContract = await myContract.deploy(initialBalance);

The above gives me an error, I assume because it's reading the initialBalance as an argument to the constructor and my constructor takes in no arguments. How can I send an initial balance during deployment like you can from the Remix IDE? Thanks.

0

Just change initialBalance to { value: initialBalance }

const deployedContract = await myContract.deploy({ value: initialBalance });

The deploy function takes constructor arguments followed by an optional argument called overrides object (docs). If your function is payable, then you are allowed to use the value override. You can see list of overrides.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.