使用Web3进行测试时如何将参数传递给构造函数

时间:2019-02-21 14:56:56

标签: solidity smartcontracts web3

我需要使用web3ganache-cli测试我的合同。在我的合同中,我必须向argument函数发送一个constructor。使用web3部署时如何做。

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

我的合同是

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;
    }
}

我在交换堆栈时经历了这个link,但是我无法解决它。

1 个答案:

答案 0 :(得分:1)

您可以通过向arguments函数的.deploy()属性提供数据来实现。

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