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;
}
}
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,
You can understand easily by this image
Hope this answer clear your doubt.