---
eip: 2157
label: ERC-2157
title: dType Storage Extension - Decentralized Type System for EVM
status: Stagnant
type: Standards Track
category: ERC
created: 2019-06-28
requires: 1900
official: https://eips.ethereum.org/EIPS/eip-2157
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-2157.md
canonical: https://eips.huatuo.xyz/zh/eip-2157/
lang: zh
tldr: dType 类型注册表（ERC-1900）的可选扩展，规范了每种类型的实例如何存储在链上，让任何人都能在可预测的位置找到并读取某个类型的公开数据。
tags: [dtype, types, storage, data, erc]
---

# ERC-2157: dType Storage Extension - Decentralized Type System for EVM

> This ERC is an extension of ERC-1900, proposing an optional storage extension for dType, a decentralized type system, specifying a general ABI for all storage contracts that contain type instances.

**TL;DR:** dType 类型注册表（ERC-1900）的可选扩展，规范了每种类型的实例如何存储在链上，让任何人都能在可预测的位置找到并读取某个类型的公开数据。

## 它解决什么问题？

[ERC-1900](/eip-1900/)（dType）是一个链上数据类型注册表，例如在 Solidity 库中定义的"Product"结构体，让各项目可以共享类型定义。但它没有说明**某个类型的实际记录存放在哪里**。要找到某个类型的所有公开数据，就得扫描整条链，或者依赖区块浏览器等中心化缓存。

ERC-2157 规范了最后这一步。每个已注册的类型都指向一个带有通用 CRUD 接口的存储合约，这样任何应用或合约都能确定性地定位并读取某个类型的公开记录。

## 它如何运作

每个类型会新增两个合约：

**`TypeRootContract`**：类型的 dType 元数据中的 `contractAddress` 字段现在指向它，而不是直接指向类型库。它保存两个地址：

- `libraryAddress`：定义该类型的 ERC-1900 库，
- `storageAddress`：保存该类型实例的合约。

**`TypeStorageContract`**：把类型实例存储为记录，每条记录通过由类型库的 `getIdentifier` 函数计算出的主标识符和一个递增的索引来寻址。标准接口如下：

| 函数 | 用途 |
| --- | --- |
| `insert(data)` / `insertBytes(bytes)` | 添加记录；返回其标识符 |
| `update(identifier, data)` | 替换记录 |
| `remove(identifier)` | 删除记录 |
| `isStored(identifier)` | 检查是否存在 |
| `getByHash(identifier)` / `getByIndex(index)` | 读取记录 |
| `count()` | 记录数量 |

`LogNew`、`LogUpdate` 和 `LogRemove` 事件用于跟踪变更。通用接口加上 dType 注册表，就足以重建存储合约的 ABI。把类型定义、逻辑和数据分离，旨在鼓励函数式的组合风格。

## 示例代码

解析某个类型的数据存放位置并读取一条记录（ethers v6）。dType 注册表本身的查询由 ERC-1900 定义，这里省略：

```ts
import { Contract, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);

// contractAddress taken from the type's ERC-1900 metadata
const root = new Contract(
  process.env.TYPE_ROOT!,
  ["function storageAddress() view returns (address)", "function libraryAddress() view returns (address)"],
  provider,
);

const storage = new Contract(
  await root.storageAddress(),
  [
    "function count() view returns (uint256)",
    "function typeIndex(uint256) view returns (bytes32)",
    "function isStored(bytes32) view returns (bool)",
  ],
  provider,
);

const n: bigint = await storage.count();
for (let i = 0n; i < n; i++) {
  const id = await storage.typeIndex(i); // public array getter from the stub
  console.log(i, id, await storage.isStored(id));
}
```

记录会以该类型的结构体形式返回。请根据 dType 注册表中的类型定义构建完整的 ABI，以解码 `getByHash`。

## 需要注意

- 状态为 **Stagnant**，它所依赖的 ERC-1900 也是如此。
- 规范依赖当时仍属实验性的 ABIEncoderV2 来传递结构体，该功能现在已是 Solidity 0.8 的默认设置。
- 访问控制和可变性规则被明确排除在范围之外。存储合约的桩代码不包含任何权限检查，因此实际部署必须自行添加。
- 规范承诺会在"达成共识时"提供测试用例和最终实现，但两者都没有补上。


---

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