ERC-2157 · Stagnant · ERC · 2019-06-28
ERC-2157: dType Storage Extension - Decentralized Type System for EVM
An optional extension to the dType type registry (ERC-1900) that standardises how each type's instances are stored on-chain, so anyone can find and read public data for a type at a predictable location.
What problem does it solve?
ERC-1900 (dType) is an on-chain registry of data types, such as a "Product" struct defined in a Solidity library, so that projects can share type definitions. It says nothing about where the actual records of a type live. Finding all public data of a type means scanning the chain or relying on block explorers and other centralised caches.
ERC-2157 standardises that last step. Every registered type points to one storage contract with a common CRUD interface, so any app or contract can locate and read the public records of a type deterministically.
How it works
Two contracts are added for each type:
TypeRootContract: the contractAddress field in the type's dType metadata now points here instead of straight to the type library. It holds two addresses:
libraryAddress: the ERC-1900 library defining the type,storageAddress: the contract holding instances of the type.
TypeStorageContract: stores type instances as records, each addressed by a primary identifier computed by the type library's getIdentifier function, and by an increasing index. The standard interface:
| Function | Purpose |
|---|---|
insert(data) / insertBytes(bytes) |
Add a record; returns its identifier |
update(identifier, data) |
Replace a record |
remove(identifier) |
Delete a record |
isStored(identifier) |
Existence check |
getByHash(identifier) / getByIndex(index) |
Read a record |
count() |
Number of records |
LogNew, LogUpdate and LogRemove events track changes. A common interface plus the dType registry is enough to reconstruct a storage contract's ABI. Separating type definitions, logic and data is meant to encourage a functional style of composition.
Sample code
Resolving where a type's data lives and reading a record (ethers v6). The dType registry lookup itself is defined by ERC-1900 and is omitted here:
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));
}
Records come back as the type's struct. Build the full ABI from the type definition in the dType registry to decode getByHash.
Things to know
- Status is Stagnant, as is ERC-1900, which it depends on.
- The spec relies on the then-experimental ABIEncoderV2 to pass structs, which is now the default in Solidity 0.8.
- Access control and mutability rules are explicitly out of scope. The storage stub includes no permission checks, so real deployments must add their own.
- The spec promises test cases and a final implementation "when consensus is reached"; neither was added.