2

我正在尝试测试简单的传输方法(将某人的以太转移到另一个),但是 remix(solidity web 编译器)说

transact to browser/Exchange.sol:Exchange.transfer pending ... 
transact to browser/Exchange.sol:Exchange.transfer errored: Error: VM Exception while executing eth_estimateGas: invalid opcode
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:59368:17
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:69306:5
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:11335:9
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:7895:16
at replenish (/Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:8415:25)
at iterateeCallback (/Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:8405:17)
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:8380:16
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:11332:13
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:69302:9
at /Users/strender/.nvm/versions/node/v8.8.1/lib/node_modules/ethereumjs-testrpc/build/cli.node.js:63982:7 

而且,我的 Solidity 文件结构是

pragma solidity ^0.4.18;

contract Exchange {
  mapping (address => uint256) public balances;
  string public helloworld;

  event LogDeposit(address sender, uint amount);
  event LogWithdraw(address receiver, uint amount);
  event LogTransfer(address sender, address receiver, uint amount);
  event HelloworldEvent(string helloworld);


  function Exchange() {
    helloworld = "helloworld!";
  }

  function helloWorld() returns (string) {
    return helloworld;
  }

  function deposit() payable returns(bool success) {
    balances[msg.sender] += msg.value;
    LogDeposit(msg.sender, msg.value);
    return true;
  }

  function withdraw(uint value) returns (bool success) {
    if (balances[msg.sender] < value) throw;
    balances[msg.sender] -= value;
    msg.sender.transfer(value);
    LogWithdraw(msg.sender, value);
    return true;
  }

  function transfer(address to, uint value) payable returns (bool success) {
    if (balances[msg.sender] < value) throw;
    balances[msg.sender] -= value;
    to.transfer(value);
    LogTransfer(msg.sender, to, value);
    return true;
  }

}

并且,在 web3 javascript 上,我尝试像这样调用函数“transfer(address to, uint value):

  $("#button").click(function() {
      Exchange.transfer(receiver.toString(), 2);
      // $("#loader").show();
    });

当我向此 Exchange.transfer() 方法添加一些 gas、value 参数时,VM 返回“基本费用超过 gas 限制”

4

1 回答 1

1

每条链都有一个气体限制,即您可以为矿工完成您的操作提供的最大气体量。因此,当您收到错误“基本费用超过气体限制”时,它表示执行您的操作所提供的气体量超过了我解释的限制。您可以像这样手动更改此限制:

Exchange.transfer(receiver.toString(), 2,{gas:'amount here'});

你可能已经知道了,但我的猜测是你提供的太多了。

至于你在混音中的错误,很难准确地说出发生了什么,但毫无疑问,使用你提供的气体和类似的参数可能会奏效。

于 2017-12-22T17:55:47.263 回答