Contract-Development

對合約的 Web3 交易因還原而失敗。松露控制台上的相同交易有效

  • May 22, 2018

我有一個創建另一個契約的簡單契約交易。從 Node.js 呼叫此事務時使用Web3truffle-contract拋出一個還原。

ContractFactory.deployed().then(function(instance) {
   return instance.createStore({from: accounts[0]}); 
});

結果:

2018-05-14T07:34:58.260Z develop:testrpc   Transaction: 0xc93859052bea36bd76c80ab21d30c90a3002303ec7eb1e3ea6d08cd002990d74
2018-05-14T07:34:58.261Z develop:testrpc   Gas usage: 89462
2018-05-14T07:34:58.261Z develop:testrpc   Block Number: 39
2018-05-14T07:34:58.261Z develop:testrpc   Block Time: Mon May 14 2018 13:04:58 GMT+0530 (India Standard Time)
2018-05-14T07:34:58.261Z develop:testrpc   Runtime Error: revert

當我呼叫相同的事務truffle consoletruffle develop事務成功時。

truffle (develop) > ContractFactory.deployed().then(function (instance) { return instance.createStore() });

結果:

2018-05-14T07:30:33.789Z develop:testrpc   Transaction: 0xbdc51bb74de2b59e98ad1b8f38ed4367c2d975a77340d778e44ee2ced969e5b9
2018-05-14T07:30:33.789Z develop:testrpc   Gas usage: 135843
2018-05-14T07:30:33.789Z develop:testrpc   Block Number: 38
2018-05-14T07:30:33.789Z develop:testrpc   Block Time: Mon May 14 2018 13:00:33 GMT+0530 (India Standard Time)
2018-05-14T07:30:33.789Z develop:testrpc
2018-05-14T07:30:33.791Z develop:testrpc eth_getTransactionReceipt

我一直在嘗試開始truffle debug工作,但它無法調試創建另一個契約的交易。

我最初的猜測是我的 Web3 交易已經用完了設定的氣體限制,但這會引發還原嗎?我嘗試將氣體限制設置為非常高的值,但沒有更好的結果。

ContractFactory.defaults({
 gasLimit: "100000000000000000"
});

合約程式碼:

pragma solidity ^0.4.16;

contract SimpleStore {
   uint public value;
   function SimpleStore (uint num) public {
       value = num;
   }

   function setValue (uint num) public {
       value = num;
   }
}

contract ContractFactory {

   address public store;

   function ContractFactory () public {

   }

   function createStore () public {
       store = new SimpleStore(12);
   }
}

注意:其他事務和呼叫非常適合 Web3 設置。這個問題對於所有創建合約的交易都很常見。

我能夠重現您面臨的問題。我做了以下來解決這個問題。在契約文件夾中為 SimpleStore 和 ContrctFactory 創建了兩個單獨的契約文件,如下所示:- SimpleStore.sol 文件-

pragma solidity ^0.4.16;

contract SimpleStore {
   uint public value;
   function SimpleStore (uint num) public {
       value = num;
   }

   function setValue (uint num) public {
       value = num;
   }
}

ContractFactory.sol 文件 -

pragma solidity ^0.4.16;

contract SimpleStore {
   uint public value;
   function SimpleStore (uint num) public {
       value = num;
   }

   function setValue (uint num) public {
       value = num;
   }
}
contract ContractFactory {

   address public store;

   function ContractFactory () public {

   }

   function createStore () public {
       store = new SimpleStore(12);
   }
}

2_deploy_contracts.js 文件 -

var SimpleStore = artifacts.require("./SimpleStore.sol");
var ContractFactory = artifacts.require("./ContractFactory.sol");
module.exports = function(deployer) {
 deployer.deploy(SimpleStore,10, {gas: 6700000});
 deployer.deploy(ContractFactory,{gas: 6700000});
};

app.js 文件 -

import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
import SimpleStore_artifacts from '../../build/contracts/SimpleStore.json'
import ContractFactory_artifacts from '../../build/contracts/ContractFactory.json'

var SimpleStore = contract(SimpleStore_artifacts);
var ContractFactory = contract(ContractFactory_artifacts);
$( document ).ready(function() {
 if (typeof web3 !== 'undefined') {
   console.warn("Using web3 detected from external source like Metamask")
   // Use Mist/MetaMask's provider
   window.web3 = new Web3(web3.currentProvider);
 } else {
   console.warn("No web3 detected. Falling back to http://localhost:8545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
   // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
   window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

 }
 SimpleStore.setProvider(web3.currentProvider);
 ContractFactory.setProvider(web3.currentProvider);
 ContractFactory.deployed().then(function(instance) {
   return instance.createStore({from: web3.eth.accounts[0],gas: 6700000}); 
   console.log("******");
 });
});

添加gas成本作為參數解決了VM異常的問題。

instance.createStore({來自:web3.eth.accounts

$$ 0 $$,gas: 6700000});

我還可以從松露控制台執行交易-

truffle(development)> ContractFactory.deployed().then(function (instance) { return instance.createStore({from: web3.eth.accounts[0],gas: 6700000});})
{ tx: '0x4d34e45cddbfa6f087f5dcb02bb5e7dc0fa18a0f7d24e87f8fff8702dd3e53bc',
 receipt: 
  { transactionHash: '0x4d34e45cddbfa6f087f5dcb02bb5e7dc0fa18a0f7d24e87f8fff8702dd3e53bc',
    transactionIndex: 0,
    blockHash: '0xf77b2baa3cacc941a8376376425c642c6db580e4a26918a0905ed589a618bfe7',
    blockNumber: 26,
    gasUsed: 123078,
    cumulativeGasUsed: 123078,
    contractAddress: null,

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