Unable to match calculated "gas used" value using Solidity IDE to the etherscan explorer "Gas Used By Transaction"

1k Views Asked by At

I am trying find the gas used by the transaction when a method is clicked in the remix solidity IDE. my code is as below. Value I am getting in the gasUsed variable is different to the value that is being shown on the etherscan explorer for this transaction. It would be helpful if someone helps me in correcting my code.

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;
}

}

1

There are 1 best solutions below

2
On BEST ANSWER

The value of "gas used" in remix IDE is Execution Cost and the value of "Gas Used By Transaction" in etherscan is "Transaction cost".

Execution Costs are based on the cost of computational operations which are executed as a result of the transaction.

Transaction Costs are always based on the cost of which type of data you will send to blockchain. This depends on,

  1. base cost of transaction (21000 gas)
  2. the cost of a contract deployment (32000 gas)
  3. the cost for every zero byte of data or code for a transaction.
  4. the cost of every non-zero byte of data or code for a transaction.

You can understand easily by this image enter image description here

Hope this answer clear your doubt.