pub struct ChainTypeRegistry { /* private fields */ }Expand description
A registry of types that’s been defined for a specific chain.
Use ChainTypeRegistry::for_spec_version() to get back something that implements
scale_type_resolver::TypeResolver. Use serde to deserialize something into this
struct (the deserialization logic is tuned to work best with serde_json, but any self
describing format should work so long as it’s the right shape).
§Example
use scale_info_legacy::ChainTypeRegistry;
let json = serde_json::json!({
// Types that are present globally, regardless of spec version:
"global": {
// Types here exist across all pallets.
"types": {
// A simple type alias:
"Foo": "u8",
// A tuple:
"TupleOf": "(bool, Vec<String>)",
// An unnamed struct (basically a tuple like "(bool, Vec<String>)" but with a path):
"UnnamedStructOf": ["bool", "Vec<String>"],
// A struct with 2 fields, a and b, and a generic type.
"StructOf<T>": {
"a": "bool",
"b": "T"
},
// The simplest way to list enum variants when none have associated data:
"EnumWithoutData": {
"_enum": ["One", "Two", "Three"]
},
// We can also use a struct form to specify the data that each variant has:
"EnumWithData": {
"_enum": {
"A": "u64",
"B": ["bool", "char"],
"C": { "field_a": "String", "field_b": "bool" }
}
},
// We can be very explicit if we want to specify the enum variant indexes:
"EnumWithExplicitDetails": {
"_enum": [
{ "name": "A", "index": 0, "fields": "u64" },
{ "name": "B", "index": 1, "fields": ["bool", "char"] },
{ "name": "C", "index": 2, "fields": { "field_a": "String", "field_b": "bool" } }
]
}
},
// Any type in palletTypes only exists within a certain pallet.
"palletTypes": {
// The Balance type only exists in the balances pallet.
"balances": {
"Balance": "u64"
},
// Fee and AssetsEnum only exist in the assets pallet.
"assets": {
"Fee": "u32",
"AssetsEnum": {
"_enum": ["One", "Two", "Three"]
}
}
},
// We can also define runtime APIs. For each runtime API we point to
// types declared elsewhere, rather than defining any new types.
"runtimeApis": {
"Metadata": {
"metadata_versions": {
"inputs": [],
"output": "Vec<u32>"
},
"metadata_at_version": {
"inputs": ["u32"],
"output": "Option<Vec<u8>>"
}
}
},
},
// We can define types that are only relevant in a specific spec range.
// We can have overlaps here; later definitions trump earlier ones if so.
"forSpec": [
{
// From 0-1000 (inclusive), we'll use these types.
"range": [null, 1000],
"types": {
"Foo": "u64",
"UnnamedStructOf": ["bool", "Vec<String>"],
"StructOf<T>": { "a": "bool", "b": "T" },
},
"palletTypes": {
"balances": {
"Balance": "u128"
},
}
},
{
// From 1001-2000 (inclusive), we'll use these types.
"range": [1001, 2000],
"types": {
"Foo": "String",
"UnnamedStructOf": ["bool", "Vec<String>"],
"StructOf<T>": { "a": "bool", "b": "T" },
},
// Runtime APIs can also be defined per spec range.
"runtimeApis": {
"Core": {
"version": {
"inputs": [],
"output": "RuntimeVersion"
}
}
}
}
]
});
let tys: ChainTypeRegistry = serde_json::from_value(json).unwrap();
let resolver = tys.for_spec_version(12345);Implementations§
Source§impl ChainTypeRegistry
impl ChainTypeRegistry
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Create a new empty ChainTypeRegistry. This does not allocate and can be used as a placeholder
when no type information is available.
Sourcepub fn for_spec_version(&self, spec_version: u64) -> TypeRegistrySet<'_>
pub fn for_spec_version(&self, spec_version: u64) -> TypeRegistrySet<'_>
Hand back a TypeRegistrySet that is able to resolve types for the given spec version.
Sourcepub fn spec_version_ranges(&self) -> impl Iterator<Item = (u64, u64)> + use<'_>
pub fn spec_version_ranges(&self) -> impl Iterator<Item = (u64, u64)> + use<'_>
Return the specific spec version ranges that we’ve defined types for.
Sourcepub fn extend(&mut self, other: ChainTypeRegistry)
pub fn extend(&mut self, other: ChainTypeRegistry)
Extend this chain type registry with the one provided. In case of any matches, the provided types will overwrite the existing ones.