Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC-7201, which standardizes how struct-based storage namespaces are documented and how their storage locations are derived.

This article translates, summarizes, and expands on the following specification.

More ERC explainers:

Overview

The @custom:storage-location annotation documents where a storage namespace is located in a smart contract. Data layout is important in contracts written in Solidity or Vyper, especially when several components share storage.

The proposal also defines a formula for computing the root of a namespace. The goal is to avoid unintended overlap with other storage regions. This is particularly relevant to ERC-20 and ERC-721 implementations and systems with complex roles and interactions.

For example, a mint function may update token balances and other state. Annotating the struct containing that state helps developers and tools identify its intended storage namespace. The annotation applies to the storage struct, not to the mint function itself.

A documented formula and consistent annotations improve maintainability and make accidental storage collisions easier to prevent.

For more information on storage layout, see:

https://chaldene.net/solidity-private-data#st-toc-h-3

Motivation

Solidity and Vyper use conventional storage layouts that can be modeled as a tree rooted at slot zero. Ordinary variables are laid out according to language rules, while mappings and dynamic arrays use hash-derived locations.

This works well for many contracts, but modular designs and upgradeable contracts pose additional challenges.

In a modular design, one contract can execute code from multiple other contracts using DELEGATECALL. The delegated code shares the calling contract's storage, so developers must coordinate where each module stores its state. Similarly, adding variables during an upgrade must not inadvertently shift existing storage locations.

An alternative is to derive a storage root from a namespace identifier using hashing. Related variables can then be organized using conventional layout rules within a separate namespace. The root formula is designed to avoid collisions with the default storage tree and other namespaces, subject to its cryptographic assumptions and correct implementation.

Such manually positioned storage is not automatically understood as ordinary compiler-assigned state. Standardizing how namespaces are identified helps tools analyze contracts using this pattern.

Specification

Background

A namespace is an ordered collection of variables that can include dynamic arrays and mappings. The variables follow conventional layout rules, but the namespace does not necessarily begin at slot zero.

A namespace ID is a unique string identifying a namespace within a contract. It must not contain whitespace.

A Solidity implementation represents the namespace using a struct annotated with the NatSpec tag @custom:storage-location <FORMULA_ID>:<NAMESPACE_ID>. FORMULA_ID identifies the formula that computes the root, and NAMESPACE_ID identifies the namespace.

Solidity 0.8.20 and later include this annotation in the compiler AST, making it available to tooling. An annotated struct defined outside a contract is not considered a namespace for any contract under this specification.

This convention is particularly useful for large modular or upgradeable contracts whose storage must remain organized over time.

NatSpec (Natural Specification)

NatSpec is Solidity's documentation format for contracts, functions, variables, and their intended behavior.

Comments begin with triple slashes or use a documentation-style block comment. Common tags include:

  • @title — a short contract title.
  • @author — the author.
  • @notice — an explanation for users, such as what a function does.
  • @dev — developer-oriented details, including internal behavior and caveats.
  • @param — documentation for a function parameter.
  • @return — documentation for a return value.

NatSpec is embedded in source code and can be consumed by documentation generators and user interfaces, helping developers and users understand the contract.

Formula

ERC-7201 defines a storage-root calculation identified as erc7201.

text
erc7201(id: string) = keccak256(keccak256(id) - 1) & ~0xff

The formula derives a root from a namespace's unique identifier using keccak256 hashing and alignment. In Solidity, the expression produces a numeric storage slot rather than allocating the storage automatically.

A namespace using this formula is annotated with @custom:storage-location erc7201:<NAMESPACE_ID>. For example, @custom:storage-location erc7201:foobar indicates that the namespace named foobar uses the erc7201 formula.

Future formulas may have their own unique identifiers, such as erc1234. The annotation makes the chosen convention explicit and helps tools analyze storage layout.

Additional explanation

Solidity and Vyper organize data under a conventional storage tree, normally rooted at slot 0. Contracts containing multiple independent components may benefit from additional storage namespaces.

A namespace groups related variables under a separate root. Choosing a root that avoids overlap with the default tree and other namespaces helps prevent accidental overwrites.

Simply using keccak256(id) - 1 is not sufficient for this purpose, because a namespace can occupy multiple sequential slots. The extra hashing step helps separate the namespace tree from the locations derived through ordinary storage layout.

Lroot:=root∣Lroot+n∣keccak256(Lroot)∣keccak256(H(k)⊕Lroot)∣keccak256(Lroot⊕H(k))L_{root} := \mathit{root} \mid L_{root} + n \mid \texttt{keccak256}(L_{root}) \mid \texttt{keccak256}(H(k) \oplus L_{root}) \mid \texttt{keccak256}(L_{root} \oplus H(k))

The formula also aligns the root to a multiple of 256. This anticipates a possible future gas-pricing model associated with Verkle state trees, where groups of 256 slots might be warmed together. It does not imply that such gas savings are already guaranteed.

The following expressions describe how storage locations can be derived from a root.

LrootL_{root}

This denotes the set or grammar of locations derived from the given root.

root{root}

This denotes the root itself, usually zero for conventional storage but potentially different for a namespace.

Lroot+nL_{root} + n

This represents a position relative to the root.

nn

An offset is a natural number in the storage-layout expression, relative to the location denoted by:

LrootL_{root} keccak256(Lroot){keccak256}(L_{root})

This represents a hash-derived location, using keccak256 on the input location:

(Lroot)(L_{root}) keccak256(H(k)⊕Lroot){keccak256}(H(k) \oplus L_{root})

Here,

(H(k))(H(k))

denotes a hash-derived form of the key:

(k)(k)

The operation

⊕\oplus

combines encoded components as defined by the notation in the specification. It should not be interpreted as bitwise XOR unless the formula explicitly specifies XOR; the storage grammar uses concatenation for mapping-related derivations.

keccak256(Lroot⊕H(k)){keccak256}(L_{root} \oplus H(k))

This expression derives another hash-based location using the components of the storage layout.

Together, these expressions model how sequential and hash-derived storage locations can belong to a storage tree. A namespace root must be chosen so its resulting tree does not collide with other intended storage trees.

Naming

This pattern was previously called diamond storage, but that name was often confused with the diamond proxy pattern. Diamond proxies concern extensible or upgradeable execution architecture, whereas namespaced storage concerns the organization of state.

The specification adopts the term namespaced storage to distinguish these independent concepts.

Compatibility

The proposal identifies no backward-compatibility issues. Existing contracts still need to preserve their established storage layout when migrating or upgrading.

Reference implementation

javascript
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;
    }
}

MainStorage

javascript
struct MainStorage {
    uint256 x;
    uint256 y;
}

Overview

A struct containing two uint256 variables, x and y.

Details

The struct represents the example contract's main storage namespace. Its fields contain numeric state used by the contract. Although a struct can also be used in memory in other contexts, the example explicitly accesses persistent storage.

Fields

  • uint256 x — a numeric value.
  • uint256 y — another numeric value.

MAIN_STORAGE_LOCATION

javascript
bytes32 private constant MAIN_STORAGE_LOCATION =
    0x183a6125c38840424c4a85fa12bab2ab606c4b6d0e7cc73c0c06ba5300eab500;

Overview

A constant specifying the root slot of the main storage namespace.

Details

The bytes32 constant contains the precomputed storage root for MainStorage. It identifies a location within the contract's storage, not a separate blockchain account address.


_getMainStorage

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

Overview

Returns a reference to the main storage namespace.

Details

The function uses inline assembly to set a storage reference to MAIN_STORAGE_LOCATION. That namespace contains the x and y fields.

Returns

  • MainStorage storage $ — a reference to the MainStorage struct in persistent storage.

_getXTimesY

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

Overview

Calculates and returns the product of x and y.

Details

The function first obtains the namespace reference through _getMainStorage, reads x and y, and returns their product. It is an internal view function and does not modify state.

Returns

  • uint256 — the product of x and y.

Security

Namespaces are designed to avoid overlap with other namespaces and conventional Solidity or Vyper storage. The formula relies on the cryptographic properties of keccak256 and the intended storage-layout assumptions.

The @custom:storage-location tag documents the namespace. It does not, by itself, enforce the storage location or prevent collisions. The implementation must use the correct root, maintain unique namespace IDs, and preserve compatible struct layouts across upgrades.

Developers should organize independently managed state into suitable namespaces, document their locations with the annotation, and verify that the implementation actually uses the declared layout.

Citation

Francisco Giordano (@frangio), Hadrien Croubois (@Amxx), Ernesto García (@ernestognw), Eric Lau (@ericglau), "ERC-7201: Namespaced Storage Layout," Ethereum Improvement Proposals, no. 7201, June 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7201.

Conclusion

ERC-7201 defines a convention for documenting and deriving namespaced storage locations, helping developers and tools reason about modular and upgradeable contracts.

If you have questions, feel free to contact me on Twitter.

Twitter @cardene777

I also publish material on the following platforms.

https://chaldene.net/