ERC-8042 · Final · ERC · 2025-10-11
ERC-8042: Diamond Storage
Formalises the original "diamond storage" pattern: put a struct at the storage slot keccak256("some.readable.id"), with identifiers restricted to printable ASCII.
What problem does it solve?
Proxies, diamonds (ERC-2535) and libraries that run via DELEGATECALL all share one contract's storage. If two pieces of code both use slot 0, they overwrite each other. Since Solidity 0.6.4 (March 2020) a struct can be placed at any storage slot, and the "diamond storage" pattern became the common fix: give each module its own struct at keccak256("a.unique.string").
ERC-7201 later standardised namespaced storage, but with a different formula, so the many contracts already using plain diamond storage were not covered by any standard. ERC-8042 documents that original, simpler formula and the rules that keep it safe.
How it works
- A diamond storage identifier is a string of printable ASCII only (
0x20–0x7E). - The struct's location is
keccak256(identifier). Nothing more. - Recommendations: use compile-time Solidity string literals (the compiler enforces ASCII), choose unique human-readable names such as
"myproject.erc721.registry", avoid spaces, and never useunicode"…"literals or\x/\uescapes. - Tooling can annotate the struct with the ERC-7201 NatSpec tag using a new formula id:
@custom:storage-location erc8042:<id>.
Compared with ERC-7201:
| ERC-8042 | ERC-7201 | |
|---|---|---|
| Formula | keccak256(id) |
keccak256(abi.encode(uint256(keccak256(id)) - 1)) & ~0xff |
| Identifier rules | Printable ASCII, readable | No whitespace, otherwise free |
The ASCII rule is what keeps the simple formula safe. Solidity's own mapping and array slots hash inputs containing zero bytes, which a printable-ASCII string cannot reproduce, so diamond storage locations cannot be crafted to collide with them.
Sample code
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
library CounterStorage {
bytes32 constant POSITION = keccak256("myproject.counter.storage");
/// @custom:storage-location erc8042:myproject.counter.storage
struct Layout {
uint256 count;
mapping(address => uint256) perUser;
}
function layout() internal pure returns (Layout storage s) {
bytes32 position = POSITION;
assembly { s.slot := position }
}
}
contract CounterFacet {
function increment() external {
CounterStorage.Layout storage s = CounterStorage.layout();
s.count += 1;
s.perUser[msg.sender] += 1;
}
function count() external view returns (uint256) {
return CounterStorage.layout().count;
}
}
Things to know
- Status: Final.
- Uniqueness is your responsibility: two modules using the same string share the same storage. Prefix identifiers with your project or standard name.
- Both ERC-8042 and ERC-7201 are valid choices. ERC-7201 tolerates arbitrary or machine-generated namespace ids; ERC-8042 favours readable strings and matches existing diamond deployments.
- Never change an identifier or reorder struct fields in an upgrade. Appending new fields at the end is the usual safe practice.
- The pattern is not limited to proxies. Any contract can use it to organise storage.