2

I noticed that token transfers sent to Binance are barely using more gas than a regular Ether transfer. ~22,000 gas. Other transfers of these tokens seem to charge almost double the amount of gas.

https://etherscan.io/address/0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be#tokentxns

Examples: https://etherscan.io/tx/0x7043bbef6b0fdf40a163fdf4f5d179e7f55094de72420b2577ce39e34b295881 https://etherscan.io/tx/0x637ce9ce1e6df798af45253ad017005fab553289a24e4e27672e317cb50a60d3

7

My guess is that these transactions resulted in a zero token balance for the sender and started with a non-zero balance for the recipient. When you store a value, the gas cost varies:

  • It costs 20,000 gas if you store a non-zero value where a zero value used to be.
  • It costs 5,000 gas otherwise.
  • But if you have store a zero where a non-zero used to be, you get a 15,000 gas refund.

The refund is capped to half the consumed gas.

So when transferring all of an account's tokens to an account that already has tokens, the cost should be approximately:

  • 21,000 base transaction cost
  • 5,000 to update the sender's balance
  • 5,000 to update the recipient's balance
  • -15,000 refund for setting the sender's balance to zero

That adds up to 16,000 gas. Add to that reading values from storage, logging an event, and other miscellaneous stuff.

I can't give a full accounting for the gas usage, but I believe you'll find that any token transfer under ~30,000 gas has the properties that the sender is sending their full balance and that the recipient already holds some of the token.

  • Ok that does appear to be the case. So if the transaction used 40,000 gas, you could only get refunded up to 20,000 gas? – arete Feb 1 '18 at 10:36
  • Yes, the refund can never be more than half the consumed gas. – user19510 Feb 1 '18 at 20:37
  • Great. So I guess it's optimal to delete during functions that consume quite a bit. – arete Feb 6 '18 at 14:46
3

I think what you're seeing is the gas refund.

Since holding state is one of the most expensive things to do in the EVM, there is an incentive to remove stuff from the state. There are really 3 rules around pricing of state operations:

  1. Cost of initializing a storage slot (setting a 256 bit slot in state from zero to non-zero) is 20k
  2. Cost of updating the storage slot (setting a 256 bit slot in state from non-zero to non-zero) is 5k
  3. There is also a refund of 15k for deleting a storage slot (setting a 256 bit slot from non-zero to zero).

What's happening is that you're looking at transactions where someone sends all their tokens to the contract, meaning their state in the OMG contract goes from non-zero to zero, and there is a 15k refund.

See the sclear opcode in the yellowpaper.

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.