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.
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.
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:
- Cost of initializing a storage slot (setting a 256 bit slot in state from zero to non-zero) is 20k
- Cost of updating the storage slot (setting a 256 bit slot in state from non-zero to non-zero) is 5k
- 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.
sclear opcode in the yellowpaper.