# ETH same transaction different amount of gas used

The two transactions:

are exactly the same but as you can see TX (a) failed as it used up all its available gas. The limit was set to 53,356. The second TX (b) is exactly the same but gas limit was set to 160,000 and the transaction succeeded. Although it used only 38,368 gas which is less than the limit of TX (a). But before I get answers like "some instructions take a different amount of gas depending on XYZ" I checked the execution of the contracts:

They seem to be doing the exact same thing with the exact same amounts of gas used. So why did the first transaction fail? And why does is say there's only 31700 gas to start with on TX (a)?

• The cost also depends on any state variable being changed during the transaction. For example, changing a uint 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 you 5K (conditioned to the fact that the refund does not exceed half of your transaction cost). Sep 18, 2020 at 17:31
• From the gas limit of 53356 you have to pay 21000 for the transaction plus some extra for the input data. Another thing to consider is gas refunds, thery are accounted at the end of the transaction.
– Ismael
Sep 18, 2020 at 18:21
• @Ismael why does the extra 21k not show in etherscan? Sep 24, 2020 at 6:23
• @goodvibration but looking at instructions in the execution of the contract everything looks exactly the same. Sep 24, 2020 at 6:24
• @Majster I don't know, perhaps Etherscan doesn't know how to display it. If you substract gas limit minus starting gas available, you get 21656, which is close to 21k (the rest should be input data cost).
– Ismael
Sep 24, 2020 at 12:57

I dont know the answer but I can provide some clues.

Both transactions are combined of the same steps, but the total gas is different.

You can see steps in:

And I made a google sheet to show the total gas consume step by step:

At step 436, both costs 31700. The success one finally cost 31712 at step 440. Then the total of success one should be `31712 + (160000 - 138344) = 53368`. But actually it only cost 38368. The diff is 15000.