---
eip: 1087
label: EIP-1087
title: Net gas metering for SSTORE operations
status: Stagnant
type: Standards Track
category: Core
created: 2018-05-17
official: https://eips.ethereum.org/EIPS/eip-1087
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1087.md
canonical: https://eips.huatuo.xyz/eip-1087/
lang: en
tldr: Proposed charging SSTORE by the net change to a storage slot over a whole transaction, so writing a slot several times or restoring its original value no longer costs full price.
tags: [gas, sstore, storage, core, evm]
---

# EIP-1087: Net gas metering for SSTORE operations

> This EIP proposes a change to how gas is charged for EVM SSTORE operations, in order to reduce excessive gas costs in situations where these are unwarranted, and to enable new use-cases for contract storage.

**TL;DR:** Proposed charging SSTORE by the net change to a storage slot over a whole transaction, so writing a slot several times or restoring its original value no longer costs full price.

## What problem does it solve?

When this EIP was written in 2018, every `SSTORE` was priced as if it were the only write to that slot: 20,000 gas to go from zero to non-zero, 5,000 gas for any other change, and a 10,000 gas refund for clearing a slot. That is fair for a single write, but wildly overcharges sequences of writes that touch the same slot.

The spec gives three examples:

- Set a fresh slot to 1 and back to 0: charged 15,000 gas, though nothing ends up on disk.
- Increment a fresh slot five times: charged 45,000 gas, though only one final value is written.
- Transfer A → B, then B → C within one transaction: 20,000 gas for four balance updates.

This made patterns like reentrancy locks (set a flag, then clear it) and passing context between calls in the same transaction needlessly expensive.

## How it works

The client keeps a per-transaction **dirty map** of storage slots that have been changed. It follows normal call scoping: if a call frame reverts, its dirty-map changes are discarded too.

- Writing the value a slot already holds: **200 gas**.
- First change to a slot in the transaction: **20,000 gas** if it was zero, otherwise **5,000 gas**, and the slot is marked dirty.
- Any further write to a dirty slot: **200 gas**.
- At the end of the transaction, for each dirty slot:
  - was 0 and is 0 again → refund 19,800
  - was non-zero and is back to its original value → refund 4,800
  - was non-zero and is now 0 → refund 15,000

Under these rules the examples above cost 400, 21,000 and 10,400 gas respectively. A transaction that writes a slot only once pays exactly what it did before, so no contract gets more expensive.

## Sample code

Pseudocode for the charging rule (original value = value at transaction start):

```python
def sstore_cost(slot, new_value, current, dirty):
    if new_value == current:
        return 200
    if slot in dirty:
        return 200
    dirty.add(slot)
    return 20000 if current == 0 else 5000

def end_of_tx_refund(dirty, original, final):
    refund = 0
    for slot in dirty:
        o, f = original[slot], final[slot]
        if o == 0 and f == 0:
            refund += 19800
        elif o != 0 and f == o:
            refund += 4800
        elif o != 0 and f == 0:
            refund += 15000
    return refund
```

The kind of contract code that benefits, a simple reentrancy lock:

```solidity
uint256 private locked = 1;

modifier nonReentrant() {
    require(locked == 1, "reentrant");
    locked = 2;   // first change: 5,000 gas
    _;
    locked = 1;   // back to original: cheap, plus a refund at tx end
}
```

## Things to know

- **Status: Stagnant.** EIP-1087 itself was never activated. Its successor [EIP-1283](/eip-1283/) achieved the same net-metering idea *without* a dirty map by comparing the original, current and new values of a slot.
- EIP-1283 was scheduled for Constantinople but pulled before activation because cheaper `SSTORE` let code run within the 2,300-gas stipend given to `transfer`/`send`, opening a reentrancy risk. The idea finally shipped in Istanbul as [EIP-2200](/eip-2200/), which adds a guard that `SSTORE` fails when gas left is at or below the stipend (an idea from [EIP-1706](/eip-1706/)).
- Storage pricing has since changed again with cold/warm access costs ([EIP-2929](/eip-2929/)) and reduced refunds ([EIP-3529](/eip-3529/)), so the exact numbers here are historical.


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-1087
