An SSTORE refund is a gas refund mechanism in the Ethereum Virtual Machine (EVM) that returns a portion of the gas cost to a transaction when it executes an SSTORE opcode to clear a storage slot (changing a non-zero value to zero). This mechanism, governed by the EIP-3529 specification, was designed to incentivize developers to write smart contracts that clean up unused storage, thereby reducing the perpetual growth of the blockchain's state size. The refund is applied at the end of a transaction's execution, after the total gas cost is calculated, and can offset up to a protocol-defined maximum of the transaction's total gas cost.
SSTORE Refund
What is SSTORE Refund?
An Ethereum protocol mechanism that refunds gas to a transaction when it clears storage, incentivizing efficient state management.
The refund exists to counterbalance the asymmetric cost of storage operations. Writing to storage (SSTORE from zero to non-zero) is intentionally expensive to reflect the permanent burden it places on the network. The refund for clearing storage creates an economic incentive for contracts to manage their state responsibly. For example, when a user withdraws their entire balance from a smart contract, setting that balance storage slot to zero triggers a refund. Key related concepts include gas refunds, state rent (a conceptual alternative), and state bloat, which this mechanism aims to mitigate.
The rules and economics of SSTORE refunds were significantly altered by EIP-3529, implemented in the London hard fork. This update reduced the maximum refund from 15,000 gas to 4,800 gas per cleared slot and, crucially, removed refunds for certain opcodes like SELFDESTRUCT. It also introduced a cap where refunds cannot exceed half of the total gas used by the transaction. These changes were made to mitigate gas token manipulation attacks, where projects like CHI Gastoken exploited the old refund system to create and burn tokens for arbitrage, destabilizing block gas limits.
For developers, understanding SSTORE refunds is critical for gas optimization. Efficient contract patterns, such as using packed storage variables or implementing cleanup functions, can leverage these refunds to reduce net transaction costs. However, due to the post-EIP-3529 cap, it is possible for a transaction to trigger a refund but still run out of gas if the upfront execution cost exceeds the block gas limit before the refund is applied. This makes precise gas estimation more complex when storage clearance is involved.
How SSTORE Refund Works
An explanation of the Ethereum gas refund mechanism for clearing storage, its historical role in gas optimization, and its impact on network security.
An SSTORE refund is a gas refund mechanism in the Ethereum Virtual Machine (EVM) that returns a portion of the gas cost when a smart contract clears storage by setting a state variable from a non-zero value to zero. This refund, historically set at 15,000 gas per cleared storage slot, was designed to incentivize developers to write efficient contracts that clean up unused data, thereby reducing the perpetual growth of the global state. The refund is not an immediate discount but is applied at the end of the transaction's execution, reducing the total gas consumed and, consequently, the transaction fee paid by the user.
The mechanism interacts with the EIP-2929 gas cost increases for state-accessing opcodes. While the base cost for an SSTORE that clears storage is higher post-EIP-2929, the refund amount remained a significant incentive. This created complex gas optimization strategies, such as gas token contracts, which would mint tokens by writing to storage when gas prices were low and later burn them to trigger refunds when gas prices were high, effectively "banking" cheap gas for future use. These strategies exploited the refund to subsidize other operations within the same transaction.
Due to these exploitative patterns and their negative impact on state size and network security, the SSTORE refund was significantly reduced via EIP-3529 in the London hardfork. The refund was lowered from 15,000 to 4,800 gas, and the total refund cap per transaction was introduced and later removed, severely curtailing the economic viability of gas tokens. The change rebalanced the incentive towards genuine state cleanup rather than financial engineering, aligning the mechanism's economic effects more closely with its original purpose of state size management.
Key Features of SSTORE Refunds
SSTORE refunds are a gas cost mitigation mechanism in the Ethereum Virtual Machine (EVM) that returns gas when contract storage is cleared.
Refund Trigger
A refund is credited when a storage slot's value is set from a non-zero value to zero. This incentivizes efficient state management by partially compensating for the high initial cost of storage. The refund is not immediate; it is applied at the end of the transaction, reducing the total gas used.
- Key Action:
SSTORE(slot, 0)where the previous value was non-zero. - No Refund: Setting a slot from zero to non-zero incurs the full cost with no refund.
Refund Amount & Cap
The refund amount is a fixed value per storage slot cleared. Following EIP-3529, the maximum refund per transaction is capped at 20% of the total gas used.
- Current Refund: 4800 gas per cleared slot (reduced from 15,000 gas by EIP-3529).
- Purpose of Cap: Prevents gas token abuse and ensures transaction fees remain predictable. The refund can never make a transaction's net gas cost negative.
Interaction with SELFDESTRUCT
The SELFDESTRUCT opcode, which deletes a contract, also triggers a refund. Historically, it granted 24,000 gas, but EIP-3529 removed this refund entirely to further discourage gas token patterns and simplify state management. Clearing storage via SSTORE to zero is now the only way to receive a gas refund.
Impact on Gas Tokens
Gas tokens (e.g., GST1, GST2) exploited pre-EIP-3529 refunds by minting (storing data) when gas was cheap and burning (clearing storage) when gas was expensive to offset transaction costs. The reduction of the SSTORE refund and elimination of the SELFDESTRUCT refund rendered most gas token strategies economically non-viable, aligning gas costs more closely with actual network usage.
State Change Finalization
Refunds are processed after all opcodes in a transaction have executed but before the state root is finalized. The EVM calculates the total refund amount, applies the 20% cap, and subtracts it from the transaction's total gas consumption. This final gas used is what the user pays for and is reflected on the block explorer.
Developer Consideration
While refunds can optimize gas, they should not be the primary design goal. Best practices include:
- Using mappings over arrays for deletable data.
- Packaging multiple storage clears into a single transaction to approach the refund cap.
- Understanding that refunds only partially offset the initial 20,000 gas
SSTOREcost, so minimizing storage writes is still paramount.
Evolution: The London Upgrade and EIP-3529
The London Upgrade, activated on the Ethereum mainnet in August 2021, was a significant hard fork that introduced several key Ethereum Improvement Proposals (EIPs) designed to improve network economics, security, and user experience.
The London Upgrade was a pivotal hard fork in Ethereum's history, primarily focused on reforming the network's fee market and transaction pricing. Its most famous component was EIP-1559, which introduced a base fee that is burned and a variable tip for miners/validators, fundamentally changing how users bid for block space. However, the upgrade also included other critical EIPs, such as EIP-3529, which targeted gas refunds, and EIP-3198, which added the BASEFEE opcode. Together, these changes made transaction fees more predictable and began a deflationary pressure on ETH's supply.
EIP-3529: Reduction in Refunds specifically addressed the gas refund mechanism for storage operations. Prior to London, the SSTORE opcode offered substantial gas refunds (up to 15,000 gas) for clearing contract storage slots. This was intended to incentivize good state hygiene but was exploited in gas token schemes like CHI and GST2. These tokens would mint by writing to storage when gas was cheap and later burn/refund themselves when gas was expensive, artificially inflating block sizes and creating network instability. EIP-3529 drastically reduced these refunds, eliminating the economic viability of such tokens and reducing block size variance.
The reduction had a direct impact on smart contract design and network security. By lowering the maximum refund from 15,000 to 4,800 gas and removing refunds for certain opcodes like SELFDESTRUCT, it became less economical for contracts to perform complex state-clearing operations purely for refunds. This made gas cost calculations more predictable for developers and reduced the incentive for miners to include spammy, refund-maximizing transactions, leading to more stable block gas limits and improved overall network performance.
The London Upgrade, through EIP-3529 and its siblings, exemplifies Ethereum's iterative approach to protocol improvement. It directly tackled unintended economic side-effects (gas tokens) while reinforcing the intended use of gas: to pay for computation and storage, not to be gamed for arbitrage. This change was a necessary step towards the long-term health of the network, paving the way for subsequent upgrades by simplifying the state growth and gas estimation models that are critical for Ethereum's scalability roadmap, including the transition to Proof-of-Stake.
Gas Optimization Use Cases
SSTORE refunds are a gas optimization mechanism where the EVM returns gas to the caller for clearing storage slots, incentivizing efficient state management.
Core Mechanism
The Ethereum Virtual Machine (EVM) provides a gas refund when a smart contract sets a storage slot's value from a non-zero value to zero. This refund is applied at the end of transaction execution, offsetting the total gas cost. The refund is capped at a maximum of gasUsed / 5 or 4800 gas per cleared slot, whichever is lower. This mechanism incentivizes developers to clean up unused state to reduce the blockchain's overall storage burden.
Common Use Cases
Refunds are leveraged in several key patterns:
- Token Burning & Transfers: Burning tokens or transferring an entire balance to zero an account's storage slot.
- Proxy Upgrades: Clearing old implementation addresses in upgradeable proxy patterns.
- Temporary State: Resetting flags, counters, or locks after an operation completes.
- Contract Self-Destruction: The
SELFDESTRUCTopcode triggers a 24,000 gas refund for clearing the entire contract's storage, though its behavior is changing post-EIP-6780.
Refund Cap & Transaction Ordering
A critical constraint is the refund cap: the total refund cannot exceed 50% of the total gas used in the transaction (gasUsed / 2). This prevents refunds from making transaction execution free or profitable. Furthermore, refunds are only applied after all execution is complete. Therefore, a gas-intensive operation after clearing storage does not increase the refundable amount, making the order of operations within a transaction crucial for optimization.
EIP-3529: Reduction in Refunds
Implemented in the London Hard Fork, EIP-3529 significantly reduced SSTORE refunds to mitigate certain gas token and spam attack vectors. Key changes:
- Reduced maximum refund for clearing a storage slot from 15,000 gas to 4,800 gas.
- Removed the refund for the
SELFDESTRUCTopcode (effective post-EIP-6780). - Lowered the overall refund cap from
gasUsed / 2togasUsed / 5. This made reliance on large refunds for gas savings less viable and altered optimal contract patterns.
Optimization Example: Clearing Arrays
A classic optimization is clearing a dynamic array by iterating and setting elements to zero, then resetting the array length. For example:
solidityfor (uint i; i < array.length; i++) { delete array[i]; // SSTORE to zero, triggers refund } delete array; // Clears the length slot
The gas cost of the loop is partially offset by the refunds from each delete. Developers must calculate if the net gas after the 50% cap (post-EIP-3529: 20% cap) is lower than alternative patterns like tracking a separate index.
Related Concepts
- Gas Tokens: Projects like GST1/GST2 (GasToken) exploited pre-EIP-3529 refunds by minting storage when gas was cheap and clearing it when gas was expensive, effectively "banking" gas.
- Net Gas Cost: The final transaction cost calculated as
gasUsed - min(refund, gasUsed/5). - State Rent: Refunds were initially introduced as a soft incentive for state clearance, contrasting with proposed (but not implemented) explicit state rent models.
- EIP-2200: Earlier update that refined net gas metering for SSTORE, defining the exact gas costs for different storage transitions.
Security Considerations & Exploits
The SSTORE Refund is a gas refund mechanism in the Ethereum Virtual Machine (EVM) that incentivizes data cleanup by returning gas when storage is cleared. This feature, while designed for efficiency, has been exploited in complex gas-related attacks.
Core Mechanism
The SSTORE opcode is used to write data to contract storage. When a non-zero storage slot is set to zero (e.g., sstore(slot, 0)), the EVM grants a gas refund of 4,800 gas. This refund is applied at the end of the transaction, reducing the total gas cost. The refund exists to encourage developers to free up storage, which helps reduce the size of the Ethereum state.
- Refund Cap: The total refund per transaction is capped at a maximum of 20% of the gas used.
- Historical Context: This was part of the original "Yellow Paper" gas schedule (EIP-150).
The Gas Token Exploit
Projects like GST2 and CHI tokenized this refund mechanism. Users would mint tokens by writing data to a contract's storage (consuming gas), receiving tokens in return. Later, they could burn the tokens, which triggered an SSTORE operation to clear that data, generating a large gas refund.
- Arbitrage: Users would mint tokens when gas prices were low and burn them to refund gas when prices were high, effectively "locking in" cheap gas costs.
- Impact: This created perverse incentives, bloating the Ethereum state with temporary data solely for financial arbitrage, leading to its removal in the London upgrade.
EIP-3529: Reduction of Refunds
Implemented in the London Hard Fork (August 2021), EIP-3529 significantly reduced the SSTORE refund to mitigate state bloat and eliminate the gas token business model.
- Key Changes:
- Refund for clearing storage was reduced from 15,000 gas to 4,800 gas.
- The SELFDESTRUCT refund was removed entirely.
- The maximum refund cap was reduced from 50% to 20% of gas used.
- Result: This made gas token arbitrage economically non-viable and reduced the incentive for complex refund-maximizing transactions.
Reentrancy Vector
The deferred nature of the gas refund can be exploited in combination with reentrancy. An attacker can design a call chain where:
- An initial call uses gas to set storage slots.
- A reentrant call triggers the clearing of those slots, queueing up a large refund.
- Execution returns to the original function, which may perform further expensive operations.
Because the refund is applied at the transaction's end, it can make the net gas cost of the entire attack path artificially low, allowing more complex operations to fit within a block's gas limit than otherwise possible. This can bypass gas-based security checks.
Modern Relevance & L2s
While EIP-3529 neutered the most famous exploits, understanding SSTORE refunds remains crucial.
- Auditing: Legacy code may still contain patterns optimized for the old refund schedule.
- Layer 2 Scaling: Optimistic Rollups (like Optimism, Arbitrum) and some zkEVMs implement their own gas metering and state models. While they generally follow Ethereum's lead, auditors must verify how storage operations and potential refunds are handled on each L2 to prevent novel economic attacks.
- State Management: The core principle—incentivizing cleanup—informs state management design in other blockchain systems.
Key Audit Considerations
Smart contract auditors must scrutinize code for refund-related vulnerabilities:
- Gas Left Checks: Functions that check
gasleft()to authorize actions can be bypassed if an attacker manipulates pending refunds to make gas appear sufficient. - Complex Call Chains: Analyze any pattern where storage is written and then later cleared within the same transaction, especially if external calls are involved.
- Assumptions on Gas Cost: Never assume the gas cost of an operation is static; it can be effectively reduced by refunds from other parts of the transaction.
- Legacy Code: Review pre-London upgrade contracts for gas token integration or refund-maximizing logic that may now behave unexpectedly.
Code Example: Refund in Action
A practical demonstration of how the Ethereum Virtual Machine's gas refund mechanism works when clearing storage slots.
The SSTORE refund is a gas incentive mechanism where the EVM refunds a portion of the gas cost when a contract sets a storage slot's value from a non-zero state back to zero. This is demonstrated in a function like function refundDemo() public { storedData = 0; }. If storedData previously held a value like 123, executing this function triggers a refund. The high initial cost for the SSTORE opcode is partially offset, with 15,000 gas added to the refund counter upon the zeroing operation.
This refund is not applied immediately but is tallied throughout the transaction's execution. The total refund is subtracted from the final gas cost after all opcodes have run, but it is capped at 50% of the total gas used. This prevents abuse where excessive refunds could make transactions effectively free. Developers leverage this in patterns like contract self-destruction (selfdestruct), which clears all storage, or in upgradeable proxies that clean old storage layouts, making state cleanup economically viable.
Understanding this mechanism is crucial for gas optimization. For example, a contract managing user balances might pack data into a single slot; clearing that slot when a user exits can yield a net gas saving for the overall transaction. However, since the EIP-3529 reduction in refunds (from 15,000 to 4,800 gas for SSTORE), the economic incentive has lessened, shifting optimization strategies. The refund remains a foundational part of the EVM's state management economics, encouraging efficient long-term state bloat reduction.
Comparison: SSTORE Refunds Before & After EIP-3529
This table compares the key changes to the SSTORE gas refund mechanism introduced by Ethereum Improvement Proposal 3529, which significantly reduced the maximum refund to mitigate gas price manipulation.
| Refund Parameter | Pre-EIP-3529 (Berlin & Earlier) | Post-EIP-3529 (London & Later) |
|---|---|---|
Maximum Refund per Transaction | 50% of transaction gas used | 20% of transaction gas used |
Refund for Clearing a Storage Slot (Non-zero → Zero) | 15,000 gas | 4,800 gas |
Refund for Reducing a Storage Slot Value | 4,800 gas | 4,800 gas |
Net Gas Metering | Refunds applied after execution, could make net gas negative | Refunds capped; net gas cost cannot drop below 1/5 (20%) of transaction gas used |
Primary Attack Vector Mitigated | ||
Impact on Gas Token Viability (e.g., CHI, GST2) | High viability for on-chain gas price manipulation | Severely reduced economic viability |
Frequently Asked Questions (FAQ)
SSTORE refunds are a gas cost reduction mechanism on Ethereum, crucial for understanding state management and gas optimization. These FAQs cover their purpose, mechanics, and evolution.
An SSTORE refund is a gas cost reduction mechanism on the Ethereum Virtual Machine (EVM) that returns a portion of the gas spent when a smart contract clears storage (i.e., sets a storage slot's value from a non-zero value to zero). It is not a direct refund of ETH but a reduction in the total gas consumed by the transaction, lowering the final fee. This mechanism was originally designed to incentivize developers to clean up unused storage and free up state bloat. The refund is applied at the end of a transaction's execution, after all opcodes have run, and cannot exceed 50% of the total gas used by the transaction.
Get In Touch
today.
Our experts will offer a free quote and a 30min call to discuss your project.