7

Is refunded gas for "freed" storage given to the contract, the "allocator", or the "deleter"?

Suppose a contract allocates some storage. This storage is paid for by the sender of the transaction that does the allocation, right?

Now, when this storage is "freed" by delete operator in Solidity, who gets the refund and how is the refund computed?

Is it the contract that gets the refund, the sender ("allocator") of the allocating transaction, or the sender ("deleter") of the deleting transaction? In particular, what gas price is used to compute the refund?

1 Answer 1

9

In short, the sender of the transaction that causes the storage location to be freed (set to zero) will have an amount (a net 10000 gas per freed storage location) deducted from the total amount of gas used for the transaction.

It's a bit more nuanced in reality:

  1. The gas cost of setting the location to zero is 5000 (G_sreset in the Yellow Paper).

  2. 15000 gas is added into the refund counter (R_sclear in the Yellow Paper).

  3. At the end of a successful transaction the amount of gas in the refund counter (up to a cap of half the total gas used) is added to the unused gas and returned to the caller (Eqn 72 in the Yellow Paper).

References above are to this version of the Yellow Paper, which discusses the Refund Counter in sections 6.1 and 6.2.

The gas price is whatever gas price applies to the whole transaction in which the refund occurs.

5
  • Does this mean that if user "A" allocates and pays for storage in a transaction, then another user "B" can get the refund if his transaction happens to delete this storage? Wouldn't it be more fair, if the user "A" was rewarded the refund (using some mechanism)?
    – Shuzheng
    Dec 4, 2017 at 15:12
  • 1
    Also, can't this be misued? Suppose, a gas-price of 20 GWei is used to pay for gas for storage allocation. What if the transaction that frees this storage has a gas-price of 2 ether?
    – Shuzheng
    Dec 4, 2017 at 15:31
  • 2
    The point is that the party that clears the blockchain of some data is rewarded since that data no longer needs to be persisted forever. "A" filled up the blockchain; "B" cleared it - B is rewarded. On the second point, no - it's just a reduction in the fee that I pay for my transaction; there's no net gain or way to make any Eth using this. Dec 4, 2017 at 16:57
  • Thanks for your answer. So, if I have allocated lots of storage, when would be a good time to free it? If I free it in a "light" transaction that doesn't cost much gas to execute, then most of the gas added to the refund counter will not be added to the unused gas (up to cap of half the total gas used)?
    – Shuzheng
    Dec 5, 2017 at 9:11
  • I really don't have any recommendations, except that if data doesn't need to be on the blockchain it's good for it to be removed as soon as possible. I think your original questions are answered in any case, so feel free to open a new question and mark this one done. Dec 5, 2017 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.