首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON-RPC估计气体误差

JSON-RPC估计气体误差
EN

Ethereum用户
提问于 2018-05-07 16:40:19
回答 1查看 1.1K关注 0票数 1

我用params调用方法:

代码语言:javascript
复制
{
  "jsonrpc":"2.0",
  "method":"eth_estimateGas",
  "params": [
    {
      "to": "0x8f0921f30555624143d427b340b1156914882c10",
      "data":"0xa9059cbb0000000000000000000000001fb330ab08bdba6e218e55fabc357643cf8252e300000000000000000000000000000000000000000000000000005af3107a4000"
    }
  ],
  "id":1
}

但我总是会犯错误:

代码语言:javascript
复制
{
  "jsonrpc": "2.0",
  "id": 1,
  "error": {
    "code": -32000,
    "message": "gas required exceeds allowance or always failing transaction"
  }
}

0x8f0921f30555624143d427b340b1156914882c10这是一些ERC-20令牌的地址.当我把这份合同换成别人的时候,一切都很好。有什么问题吗?

EN

回答 1

Ethereum用户

回答已采纳

发布于 2019-02-11 18:02:03

eth_estimateGas JSON调用最终导致在data字段中定义的契约函数的“试用执行”。对于ERC20传输,此函数(通常是这样)如下所示:

代码语言:javascript
复制
function transfer(address _to, uint256 _value) public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    // SafeMath.sub will throw if there is not enough balance.
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value);
    return true;
}

注意,在发送方的平衡中有一个对msg.sender的非本地引用和一个require()

尝试将一个from字段添加到您的JSON params数据中;这将在msg.sender中提供一个值,并有希望使require()成功。

注:这肯定不是直观的,估计气体将需要一个平衡检查。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/47841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档