---
eip: 7813
label: ERC-7813
title: Store, Table-Based Introspectable Storage
status: Final
type: Standards Track
category: ERC
created: 2024-11-08
official: https://eips.ethereum.org/EIPS/eip-7813
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7813.md
canonical: https://eips.huatuo.xyz/zh/eip-7813/
lang: zh
tldr: 一种存储标准：把合约数据组织成带类型的表，表的 schema 在链上注册，每次写入都触发标准事件，这样索引器无需自定义 getter 就能自动复制任何合约的状态。
tags: [storage, indexing, tables, events, mud]
---

# ERC-7813: Store, Table-Based Introspectable Storage

> On-chain tables for automatic indexing and introspectable state

**TL;DR:** 一种存储标准：把合约数据组织成带类型的表，表的 schema 在链上注册，每次写入都触发标准事件，这样索引器无需自定义 getter 就能自动复制任何合约的状态。

## 它解决什么问题？

在典型的合约中，数据保存在 Solidity 变量和 mapping 里。要读取它，别人需要一个专门为此编写的 getter。要为它建立索引，就得有人编写了解该合约事件的自定义 subgraph 或索引器。一旦数据布局改变，这些集成就会失效，而升级时也必须小心地保持存储布局不变。

ERC-7813 定义了一个 **Store**：数据保存在表中，表的键和值 schema 本身也存储在链上，每次写入都会触发四种标准事件之一。这样，任何通用索引器都能重建任何 Store 的所有表，任何合约也能通过同一个接口读取任何记录。它标准化了 MUD 框架所使用的 Store 模型。

## 它如何运作

- **表**由一个 32 字节的 `ResourceId` 标识：前两个字节表示类型（链上为 `"tb"`，链下为 `"ot"`，后者只触发事件），其后是 30 字节的名称。
- **记录**由键元组（`bytes32[]`）寻址。
- 每个表都有一个**键 schema**（只能是静态类型）和一个**值 schema**（最多 28 个字段，其中动态字段最多 5 个）。`Schema` 把字段类型打包进一个 `bytes32`，每个字段一个字节（`uint8`…`uint256` 用 `0x00`–`0x1F`，`address` 用 `0x61`，`bytes` 用 `0xC4`，`string` 用 `0xC5`，等等）。不允许使用 `string[]` 这类动态类型的数组。
- **编码**：静态字段紧凑打包；动态字段不带长度地拼接在一起，它们的长度放进同一个 `EncodedLengths` 字中。一条记录是 `(staticData, encodedLengths, dynamicData)`。
- **读取接口**：`getRecord`、`getField`、`getFieldLength`。写入方式由实现自行决定。
- **事件**：`Store_SetRecord`（整条覆盖）、`Store_SpliceStaticData` 和 `Store_SpliceDynamicData`（部分更新，比重新触发整条记录更便宜）、`Store_DeleteRecord`。链上表的内容必须（MUST）始终等于重放这些事件所得到的结果。
- 一个特殊的 **`Tables`** 表存储每个表的字段布局、schema 以及键名/字段名。它最先注册自己，所以只知道这一个表 ID 的索引器就能解码其他所有内容。

## 示例代码

读取接口：

```solidity
// 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);
}
```

用 TypeScript（ethers v6）跟踪一个 Store：

```ts
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 });
});
```

## 需要注意

- **状态：Final。**
- 该标准只定义读取。每个实现都必须为写入添加自己的访问控制。
- Store 中的所有数据都可以被任何其他合约通过 `getRecord`/`getField` 读取，这与普通合约存储不同（其他合约没有 getter 就无法读取）。不要把任何表当作私有的。
- 链下表（`"ot"`）是仅通过事件发布数据的廉价方式；其内容无法在链上读取。
- `Tables` 表的 ID 是固定的：`0x746273746f72650000000000000000005461626c657300000000000000000000`（`"tb"` + `"store"` + `"Tables"`）。


---

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