Search code examples
ethereumsoliditysmartcontractsweb3js

how to pass my arguments to constructor function while testing using web3


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

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 gone through this link in Ethereum.SE, but I'm not able to solve it.


Solution

  • You can do it by providing data to the arguments property of .deploy() function.

        contractInstance = await new web3.eth.Contract(interface).deploy({
            data: bytecode,
            arguments: [INITIAL_minimum]
        }).send({
            from: accounts[0],
            gas: 1000000
        });