#[cfg(feature = "decode")]
use codec::Decode;
#[cfg(feature = "serde_full")]
use serde::Serialize;
use super::RuntimeMetadataPrefixed;
use codec::Encode;
use scale_info::prelude::vec::Vec;
use scale_info::{
form::{Form, MetaForm, PortableForm},
IntoPortable, MetaType, PortableRegistry, Registry,
};
pub const META_RESERVED: u32 = 0x6174656d; pub type RuntimeMetadataLastVersion = RuntimeMetadataV14;
impl From<RuntimeMetadataLastVersion> for super::RuntimeMetadataPrefixed {
fn from(metadata: RuntimeMetadataLastVersion) -> RuntimeMetadataPrefixed {
RuntimeMetadataPrefixed(META_RESERVED, super::RuntimeMetadata::V14(metadata))
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub struct RuntimeMetadataV14 {
pub types: PortableRegistry,
pub pallets: Vec<PalletMetadata<PortableForm>>,
pub extrinsic: ExtrinsicMetadata<PortableForm>,
pub ty: <PortableForm as Form>::Type,
}
impl RuntimeMetadataV14 {
pub fn new(
pallets: Vec<PalletMetadata>,
extrinsic: ExtrinsicMetadata,
runtime_type: MetaType,
) -> Self {
let mut registry = Registry::new();
let pallets = registry.map_into_portable(pallets);
let extrinsic = extrinsic.into_portable(&mut registry);
let ty = registry.register_type(&runtime_type);
Self {
types: registry.into(),
pallets,
extrinsic,
ty,
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct ExtrinsicMetadata<T: Form = MetaForm> {
pub ty: T::Type,
pub version: u8,
pub signed_extensions: Vec<SignedExtensionMetadata<T>>,
}
impl IntoPortable for ExtrinsicMetadata {
type Output = ExtrinsicMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ExtrinsicMetadata {
ty: registry.register_type(&self.ty),
version: self.version,
signed_extensions: registry.map_into_portable(self.signed_extensions),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct SignedExtensionMetadata<T: Form = MetaForm> {
pub identifier: T::String,
pub ty: T::Type,
pub additional_signed: T::Type,
}
impl IntoPortable for SignedExtensionMetadata {
type Output = SignedExtensionMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
SignedExtensionMetadata {
identifier: self.identifier.into_portable(registry),
ty: registry.register_type(&self.ty),
additional_signed: registry.register_type(&self.additional_signed),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletMetadata<T: Form = MetaForm> {
pub name: T::String,
pub storage: Option<PalletStorageMetadata<T>>,
pub calls: Option<PalletCallMetadata<T>>,
pub event: Option<PalletEventMetadata<T>>,
pub constants: Vec<PalletConstantMetadata<T>>,
pub error: Option<PalletErrorMetadata<T>>,
pub index: u8,
}
impl IntoPortable for PalletMetadata {
type Output = PalletMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletMetadata {
name: self.name.into_portable(registry),
storage: self.storage.map(|storage| storage.into_portable(registry)),
calls: self.calls.map(|calls| calls.into_portable(registry)),
event: self.event.map(|event| event.into_portable(registry)),
constants: registry.map_into_portable(self.constants),
error: self.error.map(|error| error.into_portable(registry)),
index: self.index,
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletStorageMetadata<T: Form = MetaForm> {
pub prefix: T::String,
pub entries: Vec<StorageEntryMetadata<T>>,
}
impl IntoPortable for PalletStorageMetadata {
type Output = PalletStorageMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletStorageMetadata {
prefix: self.prefix.into_portable(registry),
entries: registry.map_into_portable(self.entries),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct StorageEntryMetadata<T: Form = MetaForm> {
pub name: T::String,
pub modifier: StorageEntryModifier,
pub ty: StorageEntryType<T>,
pub default: Vec<u8>,
pub docs: Vec<T::String>,
}
impl IntoPortable for StorageEntryMetadata {
type Output = StorageEntryMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
StorageEntryMetadata {
name: self.name.into_portable(registry),
modifier: self.modifier,
ty: self.ty.into_portable(registry),
default: self.default,
docs: registry.map_into_portable(self.docs),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub enum StorageEntryModifier {
Optional,
Default,
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub enum StorageHasher {
Blake2_128,
Blake2_256,
Blake2_128Concat,
Twox128,
Twox256,
Twox64Concat,
Identity,
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub enum StorageEntryType<T: Form = MetaForm> {
Plain(T::Type),
Map {
hashers: Vec<StorageHasher>,
key: T::Type,
value: T::Type,
},
}
impl IntoPortable for StorageEntryType {
type Output = StorageEntryType<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::Plain(plain) => StorageEntryType::Plain(registry.register_type(&plain)),
Self::Map {
hashers,
key,
value,
} => StorageEntryType::Map {
hashers,
key: registry.register_type(&key),
value: registry.register_type(&value),
},
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletCallMetadata<T: Form = MetaForm> {
pub ty: T::Type,
}
impl IntoPortable for PalletCallMetadata {
type Output = PalletCallMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletCallMetadata {
ty: registry.register_type(&self.ty),
}
}
}
impl From<MetaType> for PalletCallMetadata {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub struct PalletEventMetadata<T: Form = MetaForm> {
pub ty: T::Type,
}
impl IntoPortable for PalletEventMetadata {
type Output = PalletEventMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletEventMetadata {
ty: registry.register_type(&self.ty),
}
}
}
impl From<MetaType> for PalletEventMetadata {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(
feature = "serde_full",
serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
)]
pub struct PalletConstantMetadata<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
pub value: Vec<u8>,
pub docs: Vec<T::String>,
}
impl IntoPortable for PalletConstantMetadata {
type Output = PalletConstantMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletConstantMetadata {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
value: self.value,
docs: registry.map_into_portable(self.docs),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
#[cfg_attr(feature = "serde_full", serde(bound(serialize = "T::Type: Serialize")))]
pub struct PalletErrorMetadata<T: Form = MetaForm> {
pub ty: T::Type,
}
impl IntoPortable for PalletErrorMetadata {
type Output = PalletErrorMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletErrorMetadata {
ty: registry.register_type(&self.ty),
}
}
}
impl From<MetaType> for PalletErrorMetadata {
fn from(ty: MetaType) -> Self {
Self { ty }
}
}