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
event LogNew(bytes32 indexed identifier, uint256 indexed index);: An event that records the identifier and index when a new type is registered.event LogUpdate(bytes32 indexed identifier, uint256 indexed index);: An event emitted when type metadata is updated.event LogRemove(bytes32 indexed identifier, uint256 indexed index);: An event emitted when a type is removed from the registry.function insert(dTypeLib.dType calldata data) external returns (bytes32 identifier);: Registers new type metadata in the registry and returns the identifier.function remove(bytes32 identifier) external returns(uint256 index);: Removes the type specified by its identifier from the registry.function count() external view returns(uint256 counter);: Returns the total number of types registered in the registry.function getTypeIdentifier(string memory name) pure external returns (bytes32 identifier);: Computes and returns the identifier from a type name.function getByIdentifier(bytes32 identifier) view external returns(dTypeLib.dType memory dtype);: Looks up a type's full metadata by its identifier.function get(string memory name) view external returns(dTypeLib.dType memory dtype);: Looks up the full metadata by type name.function isRegistered(bytes32 identifier) view external returns(bool registered);: Checks whether an identifier is registered in the registry.function structureBytes(bytes memory data) pure public returns(myBalance memory balance): Restores byte data into that type's struct form.function destructureBytes(myBalance memory balance) pure public returns(bytes memory data): Destructures a struct value into bytes.function isInstanceOf(myBalance memory balance) pure public returns(bool isInstance): Checks whether a given value is an instance of that type.
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.