---
eip: 7201
label: ERC-7201
title: Namespaced Storage Layout
status: Final
type: Standards Track
category: ERC
created: 2023-06-20
official: https://eips.ethereum.org/EIPS/eip-7201
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7201.md
canonical: https://eips.huatuo.xyz/eip-7201/
lang: en
tldr: "A convention for placing a contract's state in hashed, collision-free storage namespaces and documenting them with a NatSpec tag, so upgradeable and modular contracts can grow safely and tools can find their data."
tags: [storage, upgradeable, proxy, solidity, tooling]
---

# ERC-7201: Namespaced Storage Layout

> Conventions for the storage location of structs in the namespaced storage pattern.

**TL;DR:** A convention for placing a contract's state in hashed, collision-free storage namespaces and documenting them with a NatSpec tag, so upgradeable and modular contracts can grow safely and tools can find their data.

## What problem does it solve?

Solidity and Vyper lay out state variables sequentially from slot 0. That works for a single contract, but breaks down in two common patterns:

- **Upgradeable contracts**: adding or reordering variables in a new implementation can shift existing ones and corrupt state.
- **Modular contracts** that `DELEGATECALL` into several pieces of code: all of them share one storage space and must coordinate carefully.

Developers solved this by putting groups of variables at pseudorandom, hash-derived slots (sometimes called "diamond storage"). But every project used its own formula, and because those variables are not ordinary state variables, compilers, static analyzers and block explorers could not see them.

ERC-7201 standardizes both the formula and a way to annotate it in source code.

## How it works

- A **namespace** is a Solidity `struct` whose fields are laid out with the usual rules, but rooted at a chosen slot instead of slot 0.
- The struct is annotated with `@custom:storage-location <formula>:<id>`, for example `@custom:storage-location erc7201:example.main`. The namespace id should contain no whitespace.
- The `erc7201` formula is:

```
erc7201(id) = keccak256(keccak256(id) - 1) & ~0xff
```

Why this shape? `keccak256(id) - 1` is a location the default layout never uses. Hashing it a second time ensures that a multi-slot namespace does not run into slots the default layout could use. The final `& ~0xff` aligns the root to a multiple of 256, anticipating possible gas changes where groups of 256 slots become warm together.

The annotation has no effect on compilation; it is documentation that tooling can read. Solidity includes it in the AST from 0.8.20, which the spec recommends as the minimum version.

## Sample code

Using a namespace in Solidity (from the spec's reference implementation):

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Example {
    /// @custom:storage-location erc7201:example.main
    struct MainStorage {
        uint256 x;
        uint256 y;
    }

    // keccak256(abi.encode(uint256(keccak256("example.main")) - 1)) & ~bytes32(uint256(0xff))
    bytes32 private constant MAIN_STORAGE_LOCATION =
        0x183a6125c38840424c4a85fa12bab2ab606c4b6d0e7cc73c0c06ba5300eab500;

    function _getMainStorage() private pure returns (MainStorage storage $) {
        assembly {
            $.slot := MAIN_STORAGE_LOCATION
        }
    }

    function _getXTimesY() internal view returns (uint256) {
        MainStorage storage $ = _getMainStorage();
        return $.x * $.y;
    }
}
```

Computing a location off-chain (ethers v6):

```ts
import { AbiCoder, keccak256, toBeHex, toBigInt, toUtf8Bytes } from "ethers";

function erc7201(id: string): string {
  const inner = toBigInt(keccak256(toUtf8Bytes(id))) - 1n;
  const outer = toBigInt(keccak256(AbiCoder.defaultAbiCoder().encode(["uint256"], [inner])));
  return toBeHex(outer & ~0xffn, 32);
}

console.log(erc7201("example.main"));
```

## Things to know

- Status is Final. OpenZeppelin Contracts 5.x uses ERC-7201 namespaces for its upgradeable contracts.
- The compiler does not check the annotation. You are responsible for actually using the slot you documented; a wrong constant silently reads and writes the wrong storage.
- Within a namespace, the normal upgrade rule still applies: append new struct fields at the end, never reorder or remove existing ones.
- The pattern is independent of any proxy type. It works with [ERC-1967](/eip-1967/) proxies, UUPS ([ERC-1822](/eip-1822/)) and diamonds ([ERC-2535](/eip-2535/)), and the spec deliberately avoids the name "diamond storage" to keep it separate from the diamond proxy pattern.


---

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