EIPs Explained

ERC-7813 · Final · ERC · 2024-11-08

ERC-7813: Store, Table-Based Introspectable Storage

A storage standard that organizes contract data into typed tables with schemas registered on-chain and standard events for every write, so indexers can replicate any contract's state automatically without custom getters.

What problem does it solve?

In a typical contract, data lives in Solidity variables and mappings. To read it, others need a getter written specifically for it. To index it, someone writes a custom subgraph or indexer that knows that contract's events. Changing the data layout breaks those integrations, and upgrades must carefully preserve the storage layout.

ERC-7813 defines a Store: data is kept in tables whose key and value schemas are themselves stored on-chain, and every write emits one of four standard events. Any generic indexer can then rebuild every table of every Store, and any contract can read any record through a single interface. It standardizes the Store model used by the MUD framework.

How it works

  • Tables are identified by a 32-byte ResourceId: two bytes for the type ("tb" on-chain, "ot" off-chain, which only emits events), then a 30-byte name.
  • Records are addressed by a key tuple (bytes32[]).
  • Each table has a key schema (static types only) and a value schema (up to 28 fields, at most 5 of them dynamic). A Schema packs field types into one bytes32, one byte per field (0x00–0x1F for uint8…uint256, 0x61 address, 0xC4 bytes, 0xC5 string, and so on). Arrays of dynamic types such as string[] are not allowed.
  • Encoding: static fields are tightly packed; dynamic fields are concatenated without lengths, and their lengths go into a single EncodedLengths word. A record is (staticData, encodedLengths, dynamicData).
  • Read interface: getRecord, getField, getFieldLength. Writes are implementation-specific.
  • Events: Store_SetRecord (full overwrite), Store_SpliceStaticData and Store_SpliceDynamicData (partial updates, cheaper than re-emitting the whole record), Store_DeleteRecord. On-chain table contents MUST always equal what replaying these events gives.
  • A special Tables table stores each table's field layout, schemas and key/field names. It registers itself first, so an indexer that knows only this one table ID can decode everything else.

Sample code

The read interface:

// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.24;

type ResourceId is bytes32;
type EncodedLengths is bytes32;

interface IStore {
    function getRecord(ResourceId tableId, bytes32[] calldata keyTuple)
        external view
        returns (bytes memory staticData, EncodedLengths encodedLengths, bytes memory dynamicData);

    function getField(ResourceId tableId, bytes32[] calldata keyTuple, uint8 fieldIndex)
        external view returns (bytes memory data);

    function getFieldLength(ResourceId tableId, bytes32[] memory keyTuple, uint8 fieldIndex)
        external view returns (uint256);
}

Following a Store from TypeScript (ethers v6):

const store = new Contract(STORE, [
  "event Store_SetRecord(bytes32 indexed tableId, bytes32[] keyTuple, bytes staticData, bytes32 encodedLengths, bytes dynamicData)",
  "event Store_DeleteRecord(bytes32 indexed tableId, bytes32[] keyTuple)",
], provider);

store.on("Store_SetRecord", (tableId, keyTuple, staticData, encodedLengths, dynamicData) => {
  db.upsert(tableId, keyTuple, { staticData, encodedLengths, dynamicData });
});

Things to know

  • Status: Final.
  • The standard only defines reads. Each implementation must add its own access control for writes.
  • Everything in a Store is readable by any other contract through getRecord/getField, unlike ordinary contract storage, which other contracts cannot read without a getter. Do not treat any table as private.
  • Off-chain tables ("ot") are a cheap way to publish data via events only; their content cannot be read on-chain.
  • The Tables table ID is fixed: 0x746273746f72650000000000000000005461626c657300000000000000000000 ("tb" + "store" + "Tables").

#storage #indexing #tables #events #mud

Details

Authors
alvarius (@alvrs), dk1a (@dk1a), frolic (@frolic), ludens (@ludns), vdrg (@vdrg), yonada <[email protected]>
Status
Final
Type
Standards Track / ERC
Created
2024-11-08

Machine-readable: Markdown · JSON