Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethereum - two identical transactions but different actual Gas use - how come?

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:

  • 0x2cbb4b35d87cabe1a7b7bcb562e4e046e9ef0b4b27ac9de99a6a3ebf6d088f89
  • 0x9b1d32a07d0332b55b800c59f860acef8791a7da6be5105ff2d2ed579fecb334

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.

Tnx!

UPDATE

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

like image 322
Tzahi Stox Avatar asked Aug 29 '18 07:08

Tzahi Stox


People also ask

Does ETH gas fee depend on amount?

Ethereum gas is essentially the costs or fees for making transactions on the Ethereum blockchain. The tricky part, however, is that Ethereum gas prices aren't fixed. The amount of gas required for each transaction depends on how complex the exchange is.

What happens if ETH transaction does not have enough gas?

A transaction that runs Out of Gas is reverted, but still included in a block and the associated fee is paid to the miner.

Can you add more gas to an Ethereum transaction?

Gas is measured in Gwei - a denomination of ETH. Gas prices fluctuation due to supply and demand. Gas fees may be tax deductible - it depends on the transaction you're making. If you're buying, selling or trading crypto - you can add the gas fee to your cost basis.

How are ETH gas charges calculated?

How Is the Gas Fee Calculated? The gas fee is calculated using Gas Limit * Gas Price per Unit. 1 So if the gas limit was 20,000 and the price per unit was 200 gwei, the calculation would be 20,000 * 200 = 4,000,000 gwei or 0.004 ETH.


1 Answers

Just a guess, but a fairly likely one:

The transfer function does two writes to storage:

  1. Update the balance of the from address.
  2. Update the balance of the to address.

Writing to storage has different gas costs depending on the previous value and the new value.

My guess is that in the first transaction, the to address has an initial balance of zero, and the from address still has some tokens left after the transaction. Changing a zero in storage to a non-zero value costs 20,000 gas, so this is the cost of updating the to address. Changing a non-zero value to another non-zero value has a gas cost of 5,000 gas, so that's the cost for updating the from address. Total cost of the two store instructions: 25,000.

My guess is that in the second transaction, the to address has a positive initial balance that is increased, and the balance of the from address is fully depleted by the transaction. Updating the to address costs 5,000 gas (changing a non-zero value to another non-zero value). Updating the from address involves changing a non-zero value to a zero, for which you get a gas refund. The cost is 5,000 gas but then a gas refund of 15,000 is given at the end of the transaction. That's a net cost of -10,000, so the total for the two store instructions is -5,000.

The difference between 25,000 and -5,000 is 30,000, which is precisely the gas difference between the two transactions.

like image 179
user94559 Avatar answered Jan 02 '23 21:01

user94559