Contract-Development

使用“應付”建構子部署契約並包含輸入參數?

  • July 2, 2018

這裡有一份有用的Solidity契約。但是,在部署時有兩件事:

(1) 第一件事是,contract balance因為它constructorpayable。(2) 第二個是constructor's parameters value( (address **_recipient**, uint256 **duration**))

要部署我使用的契約web3.js 1.0.0-beta.34。以下是我部署此合約的步驟:

Web3 = require("web3")
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

const bytecode = '0x1234 ....
const abi = ....

web3.eth.personal.unlockAccount('0x123...', 'password')

var thisContract = new web3.eth.Contract(abi, {
   from: '0x123...',
   gasPrice: '20000000000'
});

thisContract.deploy({ data: bytecode }).estimateGas(function(err, gas) { console.log(gas); });

thisContract.deploy({ data: bytecode }).send({
       from: '0x123...',
       gas: 5000000 ,
       gasPrice: '3000000000'
   },
   function(error, transactionHash) {
       console.log(error);
       console.log(transactionHash);
       console.log('function exec');
   }).then(function(newContractInstance) {
   console.log('Contract Instance:' + newContractInstance.options.address);
});

但是,我不知道在上述過程中我必須在哪裡確定**(**(1) the contract balance因為它constructorpayable)和(2) the constructor's parameters value(即(address _recipient, uint256 duration))?

注意:我想首先我必須使用remix我確定的位置部署此合約constructor's parameters values,然後在上面的 web3.js 程式碼中使用生成的bytecodeabi通過。remix但是,我不確定在哪裡可以使用value: web3.toWei(1/*contract balance*/, 'ether').

看看這個例子


const thisContract = new web3.eth.Contract(abi);
thisContract.deploy({  
       data: bytecode,
       arguments: [ "0x2345...", 12000 ] &lt- Here you put your constructor arguments in order
   }).send({
      from: "0x1234...",
      gas: 5000000 ,
      gasPrice: '3000000000',
      value: 120 &lt- Here you put how many wei to send to the constructor
   },
   function(error, transactionHash) ...

引用自:https://ethereum.stackexchange.com/questions/52483