3

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.

2 Answers 2

5

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.

1
0

Here is a snippet if you are using ethers.js v6

const initialBalance = ethers.parseEther("0.1");
const myContract = await ethers.deployContract("MyContract",{value: initialBalance});
await myContract.waitForDeployement();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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