0

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.

2

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.

0

Try this way

factory = await web3.eth.Contract(JSON.parse(compiledFactory.interface))
.deploy({
  data: compiledFactory.byteCode, 
  arguments: [100] //your argument
})
.send({
  from: accounts[0],
  gas: "1000000",
});
  • 1
    AFAIK, you should use an array of arguments (i.e., in your example, it should be [100]). – goodvibration Feb 21 at 15:35

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.