0

I have a smart contract that maps a string to a string value and stores it on the Blockchain. I want to analyse the Gas Used by Transaction. One time the execution consumes 92,843 Gas (https://rinkeby.etherscan.io/tx/0x04ac9ef0403d0d9362779de3f15d25942e861270aa53d4896692f68bc06150aa), the other time it only takes 43,043 Gas (https://rinkeby.etherscan.io/tx/0x103693f2d548da60e14470ced0825f5a5263a9e618658e1812d3c84c29571f4a). How does that happen?

Also: The transaction costs 21.000 Gas and the storage operation for 32Byte is 5.000 Gas. Since I store 2 64Byte Strings that is 21.000 + 2*5.000 + 2*5.000 = 41.000. Where does the rest of 2.043 Gas come from?

To narrow it down, I also uploaded an even simpler contract which only stores one uint value:

pragma solidity ^0.5.7;
contract storeNumber{
   uint test;
    function setValue(uint number) public{
        test = number;
    } 
}

The transaction https://rinkeby.etherscan.io/tx/0x5327f3a2a74f2589c7f8e323a698f78c17b249cf5090cec74d11e7456cb0ec38 consumes 26,684 Gas - 21.000 + 5.000 is obvious to me, but what about the 684 additional Gas?

1 Answer 1

1

A fee of 21,000 gas units applies only when transferring Ether to an externally-owned account.

When transferring Ether to a contract, it will cost those 21,000 gas units plus the execution cost of the function which handles the transfer (aka the fallback function).

The cost of calling (any other) contract function depends on the implementation of that specific function, and will necessarily be higher than 21,000 gas units.

6
  • Thank you. That is what I understood as well. But how is the gas consumption of the function calculated? Specifically the "smaller" numbers? E.g. the 684 of the 26,684 Gas?
    – Felix
    May 1, 2019 at 10:02
  • @Felix: Possibly the "extra" input argument yields an additional cost. Try to remove it, set test = THE_VALUE_THAT_YOU_HAVE_USED and then measure the gas cost once again. May 1, 2019 at 10:15
  • Good point. Thank you - without the parameter it takes 41.408 Gas: rinkeby.etherscan.io/tx/… I assume there are 200 Gas for reading the variable from storage, then the remaining 208 are still unclear...
    – Felix
    May 1, 2019 at 10:57
  • contract storeNumber{ uint test; function setValue() public{ test = 1; } } executing the setValue function needs 20.136 Gas
    – Felix
    May 1, 2019 at 11:00
  • @Felix: You might also want to test different values (for easier testing, add the input parameter back as it was before). I'm pretty sure that you get slightly different gas costs when changing zero to different non-zero values (and also when changing different non-zero values to zero). May 1, 2019 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.