solidity – Passing Constructor Arguments to Smart Contract with Ethers.js

ethers.jssolidity

i have a smartcontract that expects constructor arguments.
i would like to deploy the contract with arguments and also pass the options gasPrice and gasLimit.

this is my function for deploying:

const deploy=async() => {
    
    let tempProvider = new ethers.providers.Web3Provider(window.ethereum);
    let tempSigner = tempProvider.getSigner();
    const price = ethers.utils.formatUnits(await tempProvider.getGasPrice(), 'gwei')
    const options = {gasLimit: 100000, gasPrice: ethers.utils.parseUnits(price, 'gwei')}
    // Deploy the contract
    const factory = new ethers.ContractFactory(abi, bytecode, tempSigner)
    const contract = await factory.deploy(options)
    await contract.deployed()


}

i can pass the options, but how can i pass my constructor arguments next to it?
from ethers docs i have this:

// If your contract constructor requires parameters, the ABI
// must include the constructor
const abi = [
  "constructor(address owner, uint256 initialValue)",
  "function value() view returns (uint)"
];

// The factory we use for deploying contracts
factory = new ContractFactory(abi, bytecode, signer)

// Deploy an instance of the contract
contract = await factory.deploy("ricmoo.eth", 42);

they just show how you can pass the arguments, but they dont show how to pass the options. I want to pass both, the options and the contructor values.

here is the definition of the ContractFactory

contractFactory.deploy( …args [ , overrides ] ) ⇒ Promise< Contract >

Best Answer

Ethers is aware how many arguments are expected for the constructor. If more arguments are passed to the deploy functions than expected for the constructor it will take the last parameters as the options.

Ethers code: https://github.com/ethers-io/ethers.js/blob/73a46efea32c3f9a4833ed77896a216e3d3752a0/packages/contracts/src.ts/index.ts#L1216-L1219

Related Topic