sp_metadata_ir/
v15.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Convert the IR to V15 metadata.
19
20use crate::OuterEnumsIR;
21
22use super::types::{
23	ExtrinsicMetadataIR, MetadataIR, PalletMetadataIR, RuntimeApiMetadataIR,
24	RuntimeApiMethodMetadataIR, RuntimeApiMethodParamMetadataIR, TransactionExtensionMetadataIR,
25};
26
27use frame_metadata::v15::{
28	CustomMetadata, ExtrinsicMetadata, OuterEnums, PalletMetadata, RuntimeApiMetadata,
29	RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV15,
30	SignedExtensionMetadata,
31};
32
33impl From<MetadataIR> for RuntimeMetadataV15 {
34	fn from(ir: MetadataIR) -> Self {
35		RuntimeMetadataV15::new(
36			ir.pallets.into_iter().map(Into::into).collect(),
37			ir.extrinsic.into(),
38			ir.ty,
39			ir.apis.into_iter().map(Into::into).collect(),
40			ir.outer_enums.into(),
41			// Substrate does not collect yet the custom metadata fields.
42			// This allows us to extend the V15 easily.
43			CustomMetadata { map: Default::default() },
44		)
45	}
46}
47
48impl From<RuntimeApiMetadataIR> for RuntimeApiMetadata {
49	fn from(ir: RuntimeApiMetadataIR) -> Self {
50		RuntimeApiMetadata {
51			name: ir.name,
52			methods: ir.methods.into_iter().map(Into::into).collect(),
53			docs: ir.docs,
54		}
55	}
56}
57
58impl From<RuntimeApiMethodMetadataIR> for RuntimeApiMethodMetadata {
59	fn from(ir: RuntimeApiMethodMetadataIR) -> Self {
60		RuntimeApiMethodMetadata {
61			name: ir.name,
62			inputs: ir.inputs.into_iter().map(Into::into).collect(),
63			output: ir.output,
64			docs: ir.docs,
65		}
66	}
67}
68
69impl From<RuntimeApiMethodParamMetadataIR> for RuntimeApiMethodParamMetadata {
70	fn from(ir: RuntimeApiMethodParamMetadataIR) -> Self {
71		RuntimeApiMethodParamMetadata { name: ir.name, ty: ir.ty }
72	}
73}
74
75impl From<PalletMetadataIR> for PalletMetadata {
76	fn from(ir: PalletMetadataIR) -> Self {
77		PalletMetadata {
78			name: ir.name,
79			storage: ir.storage.map(Into::into),
80			calls: ir.calls.map(Into::into),
81			event: ir.event.map(Into::into),
82			constants: ir.constants.into_iter().map(Into::into).collect(),
83			error: ir.error.map(Into::into),
84			index: ir.index,
85			docs: ir.docs,
86		}
87	}
88}
89
90impl From<TransactionExtensionMetadataIR> for SignedExtensionMetadata {
91	fn from(ir: TransactionExtensionMetadataIR) -> Self {
92		SignedExtensionMetadata {
93			identifier: ir.identifier,
94			ty: ir.ty,
95			additional_signed: ir.implicit,
96		}
97	}
98}
99
100impl From<ExtrinsicMetadataIR> for ExtrinsicMetadata {
101	fn from(ir: ExtrinsicMetadataIR) -> Self {
102		ExtrinsicMetadata {
103			version: *ir.versions.iter().min().expect("Metadata V15 supports only one version"),
104			address_ty: ir.address_ty,
105			call_ty: ir.call_ty,
106			signature_ty: ir.signature_ty,
107			extra_ty: ir.extra_ty,
108			signed_extensions: ir.extensions.into_iter().map(Into::into).collect(),
109		}
110	}
111}
112
113impl From<OuterEnumsIR> for OuterEnums {
114	fn from(ir: OuterEnumsIR) -> Self {
115		OuterEnums {
116			call_enum_ty: ir.call_enum_ty,
117			event_enum_ty: ir.event_enum_ty,
118			error_enum_ty: ir.error_enum_ty,
119		}
120	}
121}