ERC-1900 dType on-chain type registry (dType - Decentralized Type System for EVM)

Stagnant · Names & metadata · created 2019-03-28

dType is a proposal that registers the names and structures of Solidity struct types in an on-chain registry, allowing multiple contracts and off-chain tools to share the same data format.

Why it is needed

On Ethereum, many projects represent similar data using their own Solidity structs, but if the field names and order differ between projects even when the meaning is the same, contracts cannot directly read each other's data. Block explorers and development tools also have to fetch each contract's ABI individually to interpret its types, and processing logic such as map, filter, and reduce ends up being reimplemented redundantly across projects. There is also no common convention for structuring or destructuring data in low-level calls or assembly code, making it hard to handle safely without importing the interface. Without a common store where type definitions and metadata are registered on-chain in a standardized form, this duplication and inconsistency only grows as the ecosystem expands.

What to implement

In depth

Type Definition: TypeLibrary

In dType, a type is defined as a Solidity library. The library contains a nominal struct representing the type along with its functions. isInstanceOf checks whether a given value is an instance of that type, and can also define additional rules such as allowed ranges for individual fields, like a uint16 amount. The type library also provides higher-order functions (HOFs) such as map, filter, and reduce. structureBytes and destructureBytes are functions that compose and decompose a type into bytes, used for handling a type or performing type checks in low-level calls or assembly code where the interface is not imported. The example myBalanceLib implements these functions for the myBalance struct, which has accountName and amount fields.

Type Metadata: dType Registry

The type metadata registered in the registry consists of five fields: name, typeChoice, contractAddress, source, and types. name is the type name used in Solidity, and may include a human-readable part and a version number. typeChoice is an enum representing one of BaseType, PayableFunction, StateFunction, ViewFunction, PureFunction, or Event. In this proposal, contractAddress points to the address of the type library (TypeRootContract). source is a bytes32 Swarm hash pointing to the location of the type library's and the contract's source code. types is an array holding the first-depth subtypes, where each entry has name, label, and dimensions (a string[] representing array dimensions).

Composition of composite types

A new type can be created using other already-registered types as components, and this relationship always forms a directed acyclic graph (DAG). In the example, myBalance consists of two fields, accountName (string) and amount (uint256), and myToken includes myBalance again via its token (address) and balance (myBalance) fields. The resulting final data format of the composed myToken is (address,(string,uint256)), and its labeled form is (address token, (string accountName, uint256 amount)). Since the types metadata only records the first-depth subtypes, reconstructing the full ABI structure requires looking up the subtypes' metadata again in the registry.

On-chain storage structure

The registry represents metadata using the dTypes struct (name, label, dimensions) and the dType struct (typeChoice, contractAddress, source, name, dTypes[] types). For storage, a Type struct (dType data, uint256 index) is used to separate the type metadata from storage-specific data, and CRUD operations are supported through two data structures: mapping(bytes32 => Type) typeStruct and bytes32[] typeIndex.

Identifier rules

The default identifier for each type is defined as keccak256(abi.encodePacked(name)). If the system is later extended to other programming languages, the identifier can be defined to also include the language, as in keccak256(abi.encodePacked(language, name)). The proposal also suggests initially prohibiting the registration of types under single-word English names, to prevent name squatting.

Registry interface and compatibility

The registry interface registers new types with insert, removes them with remove, queries the registered count with count, derives an identifier from a name with getTypeIdentifier, retrieves full metadata with get and getByIdentifier, and checks registration status with isRegistered. Registration, modification, and removal are recorded via the LogNew, LogUpdate, and LogRemove events. However, for the sake of backward compatibility, the proposal recommends not supporting update of already-registered types, and notes that while remove can also be used for storage cleanup, it may be left out of the interface until a permission scheme, validation, and governance process are in place to prevent side effects.

Governance and proposal status

The governance protocol and permission mechanisms for the registry's create, update, and delete (CRUD) operations are outside the scope of this proposal, and consensus on adding new types or removing unused ones is left to the governance system. This proposal is currently in Stagnant status, an early-stage proposal that has not been advanced further.

Original ERC · ← ERCs