use codec::{Compact, Encode};
use scale_info::{
form::{Form, MetaForm, PortableForm},
prelude::{collections::BTreeMap, vec::Vec},
IntoPortable, Registry,
};
pub struct MetadataIR<T: Form = MetaForm> {
pub pallets: Vec<PalletMetadataIR<T>>,
pub extrinsic: ExtrinsicMetadataIR<T>,
pub ty: T::Type,
pub apis: Vec<RuntimeApiMetadataIR<T>>,
pub outer_enums: OuterEnumsIR<T>,
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct RuntimeApiMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub methods: Vec<RuntimeApiMethodMetadataIR<T>>,
pub docs: Vec<T::String>,
pub deprecation_info: DeprecationStatusIR<T>,
}
impl IntoPortable for RuntimeApiMetadataIR {
type Output = RuntimeApiMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
RuntimeApiMetadataIR {
name: self.name.into_portable(registry),
methods: registry.map_into_portable(self.methods),
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct RuntimeApiMethodMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub inputs: Vec<RuntimeApiMethodParamMetadataIR<T>>,
pub output: T::Type,
pub docs: Vec<T::String>,
pub deprecation_info: DeprecationStatusIR<T>,
}
impl IntoPortable for RuntimeApiMethodMetadataIR {
type Output = RuntimeApiMethodMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
RuntimeApiMethodMetadataIR {
name: self.name.into_portable(registry),
inputs: registry.map_into_portable(self.inputs),
output: registry.register_type(&self.output),
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct RuntimeApiMethodParamMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
}
impl IntoPortable for RuntimeApiMethodParamMetadataIR {
type Output = RuntimeApiMethodParamMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
RuntimeApiMethodParamMetadataIR {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub storage: Option<PalletStorageMetadataIR<T>>,
pub calls: Option<PalletCallMetadataIR<T>>,
pub event: Option<PalletEventMetadataIR<T>>,
pub constants: Vec<PalletConstantMetadataIR<T>>,
pub error: Option<PalletErrorMetadataIR<T>>,
pub associated_types: Vec<PalletAssociatedTypeMetadataIR<T>>,
pub index: u8,
pub docs: Vec<T::String>,
pub deprecation_info: DeprecationStatusIR<T>,
}
impl IntoPortable for PalletMetadataIR {
type Output = PalletMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletMetadataIR {
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)),
associated_types: registry.map_into_portable(self.associated_types),
index: self.index,
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct ExtrinsicMetadataIR<T: Form = MetaForm> {
pub ty: T::Type,
pub version: u8,
pub address_ty: T::Type,
pub call_ty: T::Type,
pub signature_ty: T::Type,
pub extra_ty: T::Type,
pub extensions: Vec<TransactionExtensionMetadataIR<T>>,
}
impl IntoPortable for ExtrinsicMetadataIR {
type Output = ExtrinsicMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ExtrinsicMetadataIR {
ty: registry.register_type(&self.ty),
version: self.version,
address_ty: registry.register_type(&self.address_ty),
call_ty: registry.register_type(&self.call_ty),
signature_ty: registry.register_type(&self.signature_ty),
extra_ty: registry.register_type(&self.extra_ty),
extensions: registry.map_into_portable(self.extensions),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletAssociatedTypeMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
pub docs: Vec<T::String>,
}
impl IntoPortable for PalletAssociatedTypeMetadataIR {
type Output = PalletAssociatedTypeMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletAssociatedTypeMetadataIR {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
docs: registry.map_into_portable(self.docs),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct TransactionExtensionMetadataIR<T: Form = MetaForm> {
pub identifier: T::String,
pub ty: T::Type,
pub implicit: T::Type,
}
impl IntoPortable for TransactionExtensionMetadataIR {
type Output = TransactionExtensionMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TransactionExtensionMetadataIR {
identifier: self.identifier.into_portable(registry),
ty: registry.register_type(&self.ty),
implicit: registry.register_type(&self.implicit),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletStorageMetadataIR<T: Form = MetaForm> {
pub prefix: T::String,
pub entries: Vec<StorageEntryMetadataIR<T>>,
}
impl IntoPortable for PalletStorageMetadataIR {
type Output = PalletStorageMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletStorageMetadataIR {
prefix: self.prefix.into_portable(registry),
entries: registry.map_into_portable(self.entries),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct StorageEntryMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub modifier: StorageEntryModifierIR,
pub ty: StorageEntryTypeIR<T>,
pub default: Vec<u8>,
pub docs: Vec<T::String>,
pub deprecation_info: DeprecationStatusIR<T>,
}
impl IntoPortable for StorageEntryMetadataIR {
type Output = StorageEntryMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
StorageEntryMetadataIR {
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),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub enum StorageEntryModifierIR {
Optional,
Default,
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub enum StorageHasherIR {
Blake2_128,
Blake2_256,
Blake2_128Concat,
Twox128,
Twox256,
Twox64Concat,
Identity,
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub enum StorageEntryTypeIR<T: Form = MetaForm> {
Plain(T::Type),
Map {
hashers: Vec<StorageHasherIR>,
key: T::Type,
value: T::Type,
},
}
impl IntoPortable for StorageEntryTypeIR {
type Output = StorageEntryTypeIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::Plain(plain) => StorageEntryTypeIR::Plain(registry.register_type(&plain)),
Self::Map { hashers, key, value } => StorageEntryTypeIR::Map {
hashers,
key: registry.register_type(&key),
value: registry.register_type(&value),
},
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletCallMetadataIR<T: Form = MetaForm> {
pub ty: T::Type,
pub deprecation_info: DeprecationInfoIR<T>,
}
impl IntoPortable for PalletCallMetadataIR {
type Output = PalletCallMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletCallMetadataIR {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletEventMetadataIR<T: Form = MetaForm> {
pub ty: T::Type,
pub deprecation_info: DeprecationInfoIR<T>,
}
impl IntoPortable for PalletEventMetadataIR {
type Output = PalletEventMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletEventMetadataIR {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletConstantMetadataIR<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
pub value: Vec<u8>,
pub docs: Vec<T::String>,
pub deprecation_info: DeprecationStatusIR<T>,
}
impl IntoPortable for PalletConstantMetadataIR {
type Output = PalletConstantMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletConstantMetadataIR {
name: self.name.into_portable(registry),
ty: registry.register_type(&self.ty),
value: self.value,
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct PalletErrorMetadataIR<T: Form = MetaForm> {
pub ty: T::Type,
pub deprecation_info: DeprecationInfoIR<T>,
}
impl IntoPortable for PalletErrorMetadataIR {
type Output = PalletErrorMetadataIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletErrorMetadataIR {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub struct OuterEnumsIR<T: Form = MetaForm> {
pub call_enum_ty: T::Type,
pub event_enum_ty: T::Type,
pub error_enum_ty: T::Type,
}
impl IntoPortable for OuterEnumsIR {
type Output = OuterEnumsIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
OuterEnumsIR {
call_enum_ty: registry.register_type(&self.call_enum_ty),
event_enum_ty: registry.register_type(&self.event_enum_ty),
error_enum_ty: registry.register_type(&self.error_enum_ty),
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub enum DeprecationStatusIR<T: Form = MetaForm> {
NotDeprecated,
DeprecatedWithoutNote,
Deprecated {
note: T::String,
since: Option<T::String>,
},
}
impl IntoPortable for DeprecationStatusIR {
type Output = DeprecationStatusIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::Deprecated { note, since } => {
let note = note.into_portable(registry);
let since = since.map(|x| x.into_portable(registry));
DeprecationStatusIR::Deprecated { note, since }
},
Self::DeprecatedWithoutNote => DeprecationStatusIR::DeprecatedWithoutNote,
Self::NotDeprecated => DeprecationStatusIR::NotDeprecated,
}
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
pub enum DeprecationInfoIR<T: Form = MetaForm> {
NotDeprecated,
ItemDeprecated(DeprecationStatusIR<T>),
VariantsDeprecated(BTreeMap<Compact<u8>, DeprecationStatusIR<T>>),
}
impl IntoPortable for DeprecationInfoIR {
type Output = DeprecationInfoIR<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::VariantsDeprecated(entries) => {
let entries =
entries.into_iter().map(|(k, entry)| (k, entry.into_portable(registry)));
DeprecationInfoIR::VariantsDeprecated(entries.collect())
},
Self::ItemDeprecated(deprecation) =>
DeprecationInfoIR::ItemDeprecated(deprecation.into_portable(registry)),
Self::NotDeprecated => DeprecationInfoIR::NotDeprecated,
}
}
}