From what I understand, the gas required for a ethereum contract is based on the computation it requires. So given two very similar transactions, the gas required should be very similar right?

However, I'm looking at two transactions. These two look pretty similar but have vastly different gas costs.

https://etherscan.io/tx/0xda234098b51fc78f670e111aff049289d2fecebda68ea26a359f65eac47c5eff This one is 26k gas cost

https://etherscan.io/tx/0x598d73cbfa1a4d93595ed33a852f1536b225ef4476ca8aabe79959107b590e66 This one is 41k

https://etherscan.io/tx/0x9310e289b369e5a50752e7cbceee9e406235274ddb0f2c26e8c7ddc8f25d9bb1 This one is 56k

How are all of this calculated?

Here is a copy of the contract code https://gist.github.com/plutoegg/e852a3fc51efea550eadcb162080c7f6

  • The gas cost also depends on the total size of state variables that you change, and on the actual values that you set into them. For example, changing a 256-bit slot from zero to non-zero costs 20K, changing it from non-zero to non-zero costs 10K, and changing it from non-zero to zero actually refunds the sender by 5K (subjected to certain conditions). – goodvibration Aug 25 '20 at 15:06

The gas costs is computed based on the base fee (21000) plus execution cost.

Two transactions which have exactly the same parameters and call exactly the same function may result in very different gas costs. The reason is that the gas cost may vary based on the current state of the contract.

For example here you can see the gas costs of different operations: https://medium.com/coinmonks/understanding-gas-in-ethereum-53ad816f79ae If you look at the SSTORE operation (storing a value in storage) the cost is either 20000 or 5000, based on what the previous value was.

Furthermore, the contract may have various conditions in place which reroute the transaction differently. For example it might have a condition which forwards every second transaction to a different contract, or something similar.

  • Beat you to it :) – goodvibration Aug 25 '20 at 15:09
  • 1
    Why not just add a real answer and not just a comment - answers can be accepted as the correct answer which also signifies certain things to other users. Comments are...well..just comments – Lauri Peltonen Aug 25 '20 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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