#[cfg(feature = "decode")]
use codec::Decode;
#[cfg(feature = "serde_full")]
use serde::Serialize;
use super::{RuntimeMetadataPrefixed, META_RESERVED};
use codec::{Compact, Encode};
use scale_info::{
form::{Form, MetaForm, PortableForm},
prelude::{collections::BTreeMap, vec::Vec},
IntoPortable, PortableRegistry, Registry,
};
pub use super::v14::{StorageEntryModifier, StorageEntryType, StorageHasher};
pub use super::v15::{CustomMetadata, CustomValueMetadata, OuterEnums};
pub type FunctionParamMetadata<T> = super::v15::RuntimeApiMethodParamMetadata<T>;
pub type RuntimeMetadataLastVersion = RuntimeMetadataV16;
impl From<RuntimeMetadataLastVersion> for super::RuntimeMetadataPrefixed {
fn from(metadata: RuntimeMetadataLastVersion) -> RuntimeMetadataPrefixed {
RuntimeMetadataPrefixed(META_RESERVED, super::RuntimeMetadata::V16(metadata))
}
}
#[derive(Clone, PartialEq, Eq, Encode, Debug)]
#[cfg_attr(feature = "decode", derive(Decode))]
#[cfg_attr(feature = "serde_full", derive(Serialize))]
pub struct RuntimeMetadataV16 {
pub types: PortableRegistry,
pub pallets: Vec<PalletMetadata<PortableForm>>,
pub extrinsic: ExtrinsicMetadata<PortableForm>,
pub apis: Vec<RuntimeApiMetadata<PortableForm>>,
pub outer_enums: OuterEnums<PortableForm>,
pub custom: CustomMetadata<PortableForm>,
}
impl RuntimeMetadataV16 {
pub fn new(
pallets: Vec<PalletMetadata>,
extrinsic: ExtrinsicMetadata,
apis: Vec<RuntimeApiMetadata>,
outer_enums: OuterEnums,
custom: CustomMetadata,
) -> Self {
let mut registry = Registry::new();
let extrinsic = extrinsic.into_portable(&mut registry);
let pallets = registry.map_into_portable(pallets);
let apis = registry.map_into_portable(apis);
let outer_enums = outer_enums.into_portable(&mut registry);
let custom = custom.into_portable(&mut registry);
Self {
types: registry.into(),
pallets,
extrinsic,
apis,
outer_enums,
custom,
}
}
}
#[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 RuntimeApiMetadata<T: Form = MetaForm> {
pub name: T::String,
pub methods: Vec<RuntimeApiMethodMetadata<T>>,
pub docs: Vec<T::String>,
pub version: Compact<u32>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
impl IntoPortable for RuntimeApiMetadata {
type Output = RuntimeApiMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
RuntimeApiMetadata {
name: self.name.into_portable(registry),
methods: registry.map_into_portable(self.methods),
docs: registry.map_into_portable(self.docs),
version: self.version,
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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 RuntimeApiMethodMetadata<T: Form = MetaForm> {
pub name: T::String,
pub inputs: Vec<FunctionParamMetadata<T>>,
pub output: T::Type,
pub docs: Vec<T::String>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
impl IntoPortable for RuntimeApiMethodMetadata {
type Output = RuntimeApiMethodMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
RuntimeApiMethodMetadata {
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)]
#[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 versions: Vec<u8>,
pub address_ty: T::Type,
pub call_ty: T::Type,
pub signature_ty: T::Type,
pub transaction_extensions_by_version: BTreeMap<u8, Vec<Compact<u32>>>,
pub transaction_extensions: Vec<TransactionExtensionMetadata<T>>,
}
impl IntoPortable for ExtrinsicMetadata {
type Output = ExtrinsicMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ExtrinsicMetadata {
versions: self.versions,
address_ty: registry.register_type(&self.address_ty),
call_ty: registry.register_type(&self.call_ty),
signature_ty: registry.register_type(&self.signature_ty),
transaction_extensions_by_version: self.transaction_extensions_by_version,
transaction_extensions: registry.map_into_portable(self.transaction_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 TransactionExtensionMetadata<T: Form = MetaForm> {
pub identifier: T::String,
pub ty: T::Type,
pub implicit: T::Type,
}
impl IntoPortable for TransactionExtensionMetadata {
type Output = TransactionExtensionMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
TransactionExtensionMetadata {
identifier: self.identifier.into_portable(registry),
ty: registry.register_type(&self.ty),
implicit: registry.register_type(&self.implicit),
}
}
}
#[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 associated_types: Vec<PalletAssociatedTypeMetadata<T>>,
pub view_functions: Vec<PalletViewFunctionMetadata<T>>,
pub index: u8,
pub docs: Vec<T::String>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
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)),
associated_types: registry.map_into_portable(self.associated_types),
view_functions: registry.map_into_portable(self.view_functions),
index: self.index,
docs: registry.map_into_portable(self.docs),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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,
pub deprecation_info: EnumDeprecationInfo<T>,
}
impl IntoPortable for PalletCallMetadata {
type Output = PalletCallMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletCallMetadata {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
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),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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 PalletEventMetadata<T: Form = MetaForm> {
pub ty: T::Type,
pub deprecation_info: EnumDeprecationInfo<T>,
}
impl IntoPortable for PalletEventMetadata {
type Output = PalletEventMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletEventMetadata {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
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),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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 PalletErrorMetadata<T: Form = MetaForm> {
pub ty: T::Type,
pub deprecation_info: EnumDeprecationInfo<T>,
}
impl IntoPortable for PalletErrorMetadata {
type Output = PalletErrorMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletErrorMetadata {
ty: registry.register_type(&self.ty),
deprecation_info: self.deprecation_info.into_portable(registry),
}
}
}
#[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 PalletAssociatedTypeMetadata<T: Form = MetaForm> {
pub name: T::String,
pub ty: T::Type,
pub docs: Vec<T::String>,
}
impl IntoPortable for PalletAssociatedTypeMetadata {
type Output = PalletAssociatedTypeMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletAssociatedTypeMetadata {
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)]
#[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 PalletViewFunctionMetadata<T: Form = MetaForm> {
pub id: [u8; 32],
pub name: T::String,
pub inputs: Vec<FunctionParamMetadata<T>>,
pub output: T::Type,
pub docs: Vec<T::String>,
pub deprecation_info: ItemDeprecationInfo<T>,
}
impl IntoPortable for PalletViewFunctionMetadata {
type Output = PalletViewFunctionMetadata<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
PalletViewFunctionMetadata {
id: self.id,
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)]
#[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 ItemDeprecationInfo<T: Form = MetaForm> {
NotDeprecated,
DeprecatedWithoutNote,
Deprecated {
note: T::String,
since: Option<T::String>,
},
}
impl IntoPortable for ItemDeprecationInfo {
type Output = ItemDeprecationInfo<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Self::NotDeprecated => ItemDeprecationInfo::NotDeprecated,
Self::DeprecatedWithoutNote => ItemDeprecationInfo::DeprecatedWithoutNote,
Self::Deprecated { note, since } => {
let note = note.into_portable(registry);
let since = since.map(|x| x.into_portable(registry));
ItemDeprecationInfo::Deprecated { note, since }
}
}
}
}
#[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 EnumDeprecationInfo<T: Form = MetaForm>(pub BTreeMap<u8, VariantDeprecationInfo<T>>);
impl<T: Form> EnumDeprecationInfo<T> {
pub fn nothing_deprecated() -> Self {
Self(BTreeMap::new())
}
pub fn has_deprecated_variants(&self) -> bool {
!self.0.is_empty()
}
pub fn is_variant_deprecated(&self, variant_index: u8) -> bool {
self.0.contains_key(&variant_index)
}
}
impl IntoPortable for EnumDeprecationInfo {
type Output = EnumDeprecationInfo<PortableForm>;
fn into_portable(self, registry: &mut Registry) -> Self::Output {
let entries = self
.0
.into_iter()
.map(|(k, entry)| (k, entry.into_portable(registry)));
EnumDeprecationInfo(entries.collect())
}
}
#[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 VariantDeprecationInfo<T: Form = MetaForm> {
#[codec(index = 1)]
DeprecatedWithoutNote,
#[codec(index = 2)]
Deprecated {
note: T::String,
since: Option<T::String>,
},
}
impl IntoPortable for VariantDeprecationInfo {
type Output = VariantDeprecationInfo<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));
VariantDeprecationInfo::Deprecated { note, since }
}
Self::DeprecatedWithoutNote => VariantDeprecationInfo::DeprecatedWithoutNote,
}
}
}