【问题标题】:Unable to match calculated "gas used" value using Solidity IDE to the etherscan explorer "Gas Used By Transaction"无法将使用 Solidity IDE 计算的“使用的气体”值与 etherscan 资源管理器“交易使用的气体”相匹配
【发布时间】:2019-04-15 21:14:34
【问题描述】:

我正在尝试在 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;
}

}

【问题讨论】:

    标签: ethereum solidity remix etherscan


    【解决方案1】:

    remix IDE中“gas used”的值为Execution Cost,etherscan中“Gas Used By Transaction”的值为“交易成本”。

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

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

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

    通过这张图你可以很容易理解

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

    【讨论】:

    • 我明白了。那么到底有没有可能在remix代码中得到交易成本的值呢?
    • 不,没有办法通过remix code获取交易费用,只能在合约部署后获取交易费用。
    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 2011-02-16
    • 1970-01-01
    • 2015-11-21
    • 2016-11-06
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多