如何在使用web3进行测试时将我的参数传递给构造函数

问题描述 投票:0回答:1

我需要使用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,但我无法解决它。

solidity smartcontracts web3
1个回答
1
投票

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

    contractInstance = await new web3.eth.Contract(interface).deploy({
        data: bytecode,
        arguments: [INITIAL_minimum]
    }).send({
        from: accounts[0],
        gas: 1000000
    });
© www.soinside.com 2019 - 2024. All rights reserved.