After execution of the transaction by miner, the transactor is refunded a remaining gas. it is explained in section 6 of Ethereum's yellow paper. When calculating the amount to be refunded gas, the following formula is used.

enter image description here

In the picture above, how can i understand the formula 64. When g' means remaining gas after execution of the transaction, What does mean the second term of left side of the equation?

  • 1
    As far as I've understood, you may get a bit more gas refunded than what is actually remaining. For example when you reset some values in the contract you gain some gas. – Lauri Peltonen Apr 18 '18 at 8:50

Ar is a part of the transaction substate. It's defined in section 6.1:

the refund balance, increased through using the SSTORE instruction in order to reset contract storage to zero from some non-zero value. Though not immediately refunded, it is allowed to partially offset the total execution costs.

It's capped up to a maximum of half of the total amount used before it's added to the refunded gas, as explained in the section that you referenced.

| improve this answer | |

The second term indicates that the refunded gas will be equal to the remaining plus the minimum value between what the refund counters says and half of the used gas. Lets see this with an example:

Assume you make a transaction to a function that sets the variable x = 1 and that x was initially 0.

Your costs (simplified) in gas are:

gas limit 100000 gas (for the sake of this example example)
Transaction = 21000 ()
Execution = 20000 (x=1, changes the storage from zero to nonzero)
Total cost = 41000 gas
Refund counter = 0 gas
refunded amount = 59000 gas

Now, if you execute the function this time making x = 0, the costs are:

gaslimit 100000 gas (for the sake of this example)
Transaction 21000 gas
setting x = 0, 5000 gas
refund counter = -15000 gas

Total refund = 74000 + minimum(13000, 15000)

where 13000 is half of the used gas = (100000- 74000)/2 and 15000 is the gas counter value.

So you will be refunded = 74000 + 13000

hope this helps

| improve this answer | |

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.