首页 > 解决方案 > 无法将使用 Solidity IDE 计算出的“使用的气体”值与 etherscan 资源管理器“交易使用的气体”相匹配

问题描述

当在 remix solidity IDE 中单击方法时,我正在尝试查找事务使用的气体。我的代码如下。我在 gasUsed 变量中获得的值与该交易在 etherscan 浏览器上显示的值不同。如果有人帮助我更正我的代码,那将会很有帮助。

pragma solidity ^0.4.22;

contract id{

uint public id;
uint public senderValue;
uint256 public gasUsed;

constructor() public {
    senderValue= msg.sender;
}

function setId(uint _id) public {
    uint256 gasInitial = gasleft();
    id= _id;
    setGasUsed(gasInitial - gasleft());
}

function setGasUsed(uint256 _gasUsed) private {
    gasUsed = _gasUsed;
}

}

标签: ethereumsolidityremixetherscan

解决方案


remix IDE中“ gas used ”的值为Execution Cost,etherscan中“ Gas Used By Transaction ”的值为“ Transaction cost ”。

执行成本基于作为交易结果执行的计算操作的成本。

交易成本始终基于您将发送到区块链的数据类型的成本。这取决于,

  1. 交易的基本成本(21000 gas)
  2. 合约部署的成本(32000 gas)
  3. 交易中每个零字节数据或代码的成本。
  4. 交易的每个非零字节数据或代码的成本。

通过这张图你可以很容易理解在此处输入图像描述

希望这个答案能消除你的疑惑。


推荐阅读