Web3.js – How to Pass Arguments to Constructor When Testing a Contract

contract-deploymentweb3js

I need to test my contract using web3 and ganache-cli. In my contract, I have to send an argument to the constructor function. How to do it while deploying it with web3.

factory = await web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({
      data: compiledFactory.byteCode,
    })
    .send({
      from: accounts[0],
      gas: "1000000",
    });

And my contract is,

contract Factory{
    CrowdFunding[] public deployedContractAddresses;

    constructor(uint minimum) public {
        CrowdFunding newContract = new CrowdFunding(minimum, msg.sender);
        deployedContractAddresses.push(newContract);
    }

    function getDeployedContractAddresses() public view returns(CrowdFunding[] memory) {
        return deployedContractAddresses;
    }
}

I have went through this link in stack exchange, but I cannot able to solve it.

Best Answer

Change this:

.deploy({
  data: compiledFactory.byteCode,
})

To this:

.deploy({
  data: compiledFactory.byteCode,
  arguments: [arg1, arg2, arg3],
})

Where arg1, arg2, arg3 are the constructor arguments by order.