Solidity

無法部署呼叫另一個合約的合約

  • September 15, 2017

當我連接到時執行以下程式碼時geth --dev --mine,輸出為:

A transmitted, waiting for mining...
aAddress: 0xbaxxxxx4ae42d
B transmitted, waiting for mining...
bAddress: 0x47xxxxxfe8aa
53

當我通過 連接時在實時網路上執行它時geth,輸出為:

A transmitted, waiting for mining...
aAddress: 0x04xxx76
B transmitted, waiting for mining...
[Error: The contract code couldn't be stored, please check your gas amount.]

當我在函式中替換a.a()with時,輸出為:7``b()

A transmitted, waiting for mining...
aAddress: 0xbaxxxxxae42d
B transmitted, waiting for mining...
bAddress: 0x472xxxfe8aa
53

我正在執行以下程式碼node

var Web3 = require('web3');
var web3 = new Web3();

web3.setProvider(new web3.providers.HttpProvider('http://0.0.0.0:8545'));
web3.eth.defaultAccount = web3.eth.coinbase;

var aSource = 'contract A { function a() constant returns (uint) { return 7; } }';
var aCompiled = web3.eth.compile.solidity(aSource);
var aCode = aCompiled.A.code;
var aAbi = aCompiled.A.info.abiDefinition;

web3.eth.contract(aAbi).new({data: aCode}, function (err, contract) {
   if (err) {
       console.error(err);
       return;
   } else if(contract.address){
       var aAddress = contract.address;
       console.log('aAddress: ' + aAddress);
       deployB(aAddress);
   } else {
       console.log("A transmitted, waiting for mining...");
   }
});

function deployB(aAddress) {
   var bSource = 'contract A { function a() constant returns (uint); }'+
   'contract B { A a; function B(A _a) { a = _a; } function b() returns (uint) { uint c = 46 + a.a(); return c; } }';
   var bCompiled = web3.eth.compile.solidity(bSource);
   var bCode = bCompiled.B.code;
   var bAbi = bCompiled.B.info.abiDefinition;
   var bContract = web3.eth.contract(bAbi);

   bContract.new(aAddress,{data: bCode}, function (err, contract) {
       if (err) {
           console.error(err);
           return;

       } else if(contract.address) {
           var bAddress = contract.address
           console.log('bAddress: ' + bAddress);
           var res = bContract.at(bAddress).b.call();
           console.log(res.toString());
       } else {
           console.log("B transmitted, waiting for mining...");
       }
   });
}

感謝@eth 的一些指示(見評論),正如錯誤消息所暗示的,解決方案是簡單地發送更多的氣體。出於某種原因,部署包含程式碼的a.a()合約的成本比使用硬編碼的7. 學過的知識!

我添加了以下程式碼以更智能地處理 gas 成本:

var contractData = web3.eth.contract(bAbi).new.getData({data: bCode});
console.log("estimating gas price of creating B...");
var gasEstimate = web3.eth.estimateGas({data: contractData});
console.log(gasEstimate);

bContract.new(aAddress,{data: bCode, gas:gasEstimate+30000}, function (err, contract) {

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