Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (4.1k points)

I have two identical transactions, in this case sending same amount of tokens, that result in different actual gas consumed (NOT the cost, and a significant difference).

Here are the tx hashes:

At first I thought the data might cause the difference (amount of '0' in the payload?) or the difference in mining difficulty for their corresponding blocks, but none of these seem to justify this Gas use gap (if relevant at all)

Does anyone have an explanation of how come this happens? Note that I have more transactions like these that present the same gaps.



Following smarx comment below - it sounds right, however, I would expect to have 4 different Gas values at most (2X2 combinations, as you have mentioned). In reality, we have more than 4 different results. Here are few transactions, with their Gas usage values:

22280 0x26c4b28a068e6ec91579c96e32aba449f2ad73168aca51e476a5a84072a620b6 22344 0x1341a9c4f6641746a50f8d05bb907d16150edf5e313b697908ddf3546e8fb9ae 22408 0x163eed32fee0f5999505dff804bd047620d0c063e07ad142ba0e257ed30cc4ee 22472 0xec326e42069efdefc79bd07fa98724ca2ec00432129cc3179e582a0e88af5112 22536 0xbd06b1722447fa8aaa783717c8237dd8b3934137ac8eabfd20bbdc6d1ec1af50 22600 0x9c3df4902887c94f9bf7901b8273c8b9da94d36f80801c3c5dbec9b4e7a5d8a8 36960 0x0ec051a92b1821d264f70b949cae68c2f463ae741330dc6a3a103b2612d1ea1c 37280 0x233e7c7abbf0e482c4c5b55efb31a7cd3c073ab00d2116fcff171f9e71542c1b 37344 0x3a93f860418732e76ca5941d7f9e6f0ec78df19905b8303ea520cea0994168aa 37408 0x6ce02124e33088fa7d13bfe2802039afecd78ba10e0a46598dea577c2ea61f27 37472 0x1f5be1f26f97f74aeb06d156d2221854597673640bc3c38690191501d2cd8f71 37536 0xd0edcfe4c179294bffca80f812a4a827a560294a1d944ac77adb9b8a0b22aa60 37600 0x2d7eca881486d69968eb3ab1f16850c0cd497003d06a2ea61efaeed6467f9a6a 52280 0x77d607600bf0110785cd08de78399d61c369274baa3a180841bc0d1f015f328d 52344 0xa0a3f46e35abe608c27cec2cc188beddabad724ea6362a4584e9325a5cd9d276 52408 0x41c63e00b4e58ce2292a17216e7dc2554733feb70d2a0fe9c505689ef1dd50a3 52472 0x4ed5e9366948a23340b7f572dc69da5b0525cc4f7b5d0ddd83945ef4cdbaf05c 52536 0x40b33b7f9bc08171b1b2f54241db6f58ae72bd89c9b9d7650ba3ca5174144dcd

1 Answer

0 votes
by (14.4k points)

Let’s understand this in a mathematical manner. 

The Transfer function commits 2 writes to storage. One while updating the balance of the from address and one while updating the balance of the to address. 

Depending on old and new values, gas costs vary significantly. 

Coming to your transactions now, 

In the first transaction, to address has zero initial balance but the from address has some tokens left after the transaction. We know that changing a zero to non-zero in storage costs us 20,000 gas. This is basically the cost of updating the to address. However, changing a non-zero value to another non-zero value costs 5,000 gas. And this is the cost of updating the from address. Therefore the total cost of both these instructions sums up to be 25,000 gas. 

In the second transaction, to address has a positive initial balance and this balance increases. But the balance in the from address is completely taken up by the transaction. The fact to consider here is that when we update the from address, a non-zero value is changed to zero and therefore you get a gas refund. Cost is 5,000 but after considering the refund of 15000 gas the total becomes -10000 gas. Therefore the total of both transactions comes down to -5000 gas. 

Finally, the overall difference is 25000 - -5000=30,000 gas.

Browse Categories