sp_metadata_ir/
unstable.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 V16 metadata.
19
20use crate::{
21	DeprecationInfoIR, DeprecationStatusIR, OuterEnumsIR, PalletAssociatedTypeMetadataIR,
22	PalletCallMetadataIR, PalletConstantMetadataIR, PalletErrorMetadataIR, PalletEventMetadataIR,
23	PalletStorageMetadataIR, PalletViewFunctionMetadataIR, PalletViewFunctionParamMetadataIR,
24	StorageEntryMetadataIR,
25};
26
27use super::types::{
28	ExtrinsicMetadataIR, MetadataIR, PalletMetadataIR, RuntimeApiMetadataIR,
29	RuntimeApiMethodMetadataIR, RuntimeApiMethodParamMetadataIR, TransactionExtensionMetadataIR,
30};
31
32use frame_metadata::v16::{
33	CustomMetadata, DeprecationInfo, DeprecationStatus, ExtrinsicMetadata, OuterEnums,
34	PalletAssociatedTypeMetadata, PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata,
35	PalletEventMetadata, PalletMetadata, PalletStorageMetadata, PalletViewFunctionMetadata,
36	PalletViewFunctionParamMetadata, RuntimeApiMetadata, RuntimeApiMethodMetadata,
37	RuntimeApiMethodParamMetadata, RuntimeMetadataV16, StorageEntryMetadata,
38	TransactionExtensionMetadata,
39};
40
41impl From<MetadataIR> for RuntimeMetadataV16 {
42	fn from(ir: MetadataIR) -> Self {
43		RuntimeMetadataV16::new(
44			ir.pallets.into_iter().map(Into::into).collect(),
45			ir.extrinsic.into(),
46			ir.apis.into_iter().map(Into::into).collect(),
47			ir.outer_enums.into(),
48			// Substrate does not collect yet the custom metadata fields.
49			// This allows us to extend the V16 easily.
50			CustomMetadata { map: Default::default() },
51		)
52	}
53}
54
55impl From<RuntimeApiMetadataIR> for RuntimeApiMetadata {
56	fn from(ir: RuntimeApiMetadataIR) -> Self {
57		RuntimeApiMetadata {
58			name: ir.name,
59			methods: ir.methods.into_iter().map(Into::into).collect(),
60			docs: ir.docs,
61			deprecation_info: ir.deprecation_info.into(),
62			version: ir.version.into(),
63		}
64	}
65}
66
67impl From<RuntimeApiMethodMetadataIR> for RuntimeApiMethodMetadata {
68	fn from(ir: RuntimeApiMethodMetadataIR) -> Self {
69		RuntimeApiMethodMetadata {
70			name: ir.name,
71			inputs: ir.inputs.into_iter().map(Into::into).collect(),
72			output: ir.output,
73			docs: ir.docs,
74			deprecation_info: ir.deprecation_info.into(),
75		}
76	}
77}
78
79impl From<RuntimeApiMethodParamMetadataIR> for RuntimeApiMethodParamMetadata {
80	fn from(ir: RuntimeApiMethodParamMetadataIR) -> Self {
81		RuntimeApiMethodParamMetadata { name: ir.name, ty: ir.ty }
82	}
83}
84
85impl From<PalletMetadataIR> for PalletMetadata {
86	fn from(ir: PalletMetadataIR) -> Self {
87		PalletMetadata {
88			name: ir.name,
89			storage: ir.storage.map(Into::into),
90			calls: ir.calls.map(Into::into),
91			view_functions: ir.view_functions.into_iter().map(Into::into).collect(),
92			event: ir.event.map(Into::into),
93			constants: ir.constants.into_iter().map(Into::into).collect(),
94			error: ir.error.map(Into::into),
95			index: ir.index,
96			docs: ir.docs,
97			associated_types: ir.associated_types.into_iter().map(Into::into).collect(),
98			deprecation_info: ir.deprecation_info.into(),
99		}
100	}
101}
102
103impl From<PalletStorageMetadataIR> for PalletStorageMetadata {
104	fn from(ir: PalletStorageMetadataIR) -> Self {
105		PalletStorageMetadata {
106			prefix: ir.prefix,
107			entries: ir.entries.into_iter().map(Into::into).collect(),
108		}
109	}
110}
111
112impl From<StorageEntryMetadataIR> for StorageEntryMetadata {
113	fn from(ir: StorageEntryMetadataIR) -> Self {
114		StorageEntryMetadata {
115			name: ir.name,
116			modifier: ir.modifier.into(),
117			ty: ir.ty.into(),
118			default: ir.default,
119			docs: ir.docs,
120			deprecation_info: ir.deprecation_info.into(),
121		}
122	}
123}
124
125impl From<PalletAssociatedTypeMetadataIR> for PalletAssociatedTypeMetadata {
126	fn from(ir: PalletAssociatedTypeMetadataIR) -> Self {
127		PalletAssociatedTypeMetadata { name: ir.name, ty: ir.ty, docs: ir.docs }
128	}
129}
130
131impl From<PalletErrorMetadataIR> for PalletErrorMetadata {
132	fn from(ir: PalletErrorMetadataIR) -> Self {
133		PalletErrorMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
134	}
135}
136
137impl From<PalletEventMetadataIR> for PalletEventMetadata {
138	fn from(ir: PalletEventMetadataIR) -> Self {
139		PalletEventMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
140	}
141}
142
143impl From<PalletCallMetadataIR> for PalletCallMetadata {
144	fn from(ir: PalletCallMetadataIR) -> Self {
145		PalletCallMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
146	}
147}
148
149impl From<PalletViewFunctionMetadataIR> for PalletViewFunctionMetadata {
150	fn from(ir: PalletViewFunctionMetadataIR) -> Self {
151		PalletViewFunctionMetadata {
152			name: ir.name,
153			id: ir.id,
154			inputs: ir.inputs.into_iter().map(Into::into).collect(),
155			output: ir.output,
156			docs: ir.docs.into_iter().map(Into::into).collect(),
157			deprecation_info: ir.deprecation_info.into(),
158		}
159	}
160}
161
162impl From<PalletViewFunctionParamMetadataIR> for PalletViewFunctionParamMetadata {
163	fn from(ir: PalletViewFunctionParamMetadataIR) -> Self {
164		PalletViewFunctionParamMetadata { name: ir.name, ty: ir.ty }
165	}
166}
167
168impl From<PalletConstantMetadataIR> for PalletConstantMetadata {
169	fn from(ir: PalletConstantMetadataIR) -> Self {
170		PalletConstantMetadata {
171			name: ir.name,
172			ty: ir.ty,
173			value: ir.value,
174			docs: ir.docs,
175			deprecation_info: ir.deprecation_info.into(),
176		}
177	}
178}
179
180impl From<TransactionExtensionMetadataIR> for TransactionExtensionMetadata {
181	fn from(ir: TransactionExtensionMetadataIR) -> Self {
182		TransactionExtensionMetadata { identifier: ir.identifier, ty: ir.ty, implicit: ir.implicit }
183	}
184}
185
186impl From<ExtrinsicMetadataIR> for ExtrinsicMetadata {
187	fn from(ir: ExtrinsicMetadataIR) -> Self {
188		// Assume version 0 for all extensions.
189		let indexes = (0..ir.extensions.len()).map(|index| index as u32).collect();
190		let transaction_extensions_by_version = [(0, indexes)].iter().cloned().collect();
191
192		ExtrinsicMetadata {
193			versions: ir.versions,
194			address_ty: ir.address_ty,
195			signature_ty: ir.signature_ty,
196			transaction_extensions_by_version,
197			transaction_extensions: ir.extensions.into_iter().map(Into::into).collect(),
198		}
199	}
200}
201
202impl From<OuterEnumsIR> for OuterEnums {
203	fn from(ir: OuterEnumsIR) -> Self {
204		OuterEnums {
205			call_enum_ty: ir.call_enum_ty,
206			event_enum_ty: ir.event_enum_ty,
207			error_enum_ty: ir.error_enum_ty,
208		}
209	}
210}
211
212impl From<DeprecationStatusIR> for DeprecationStatus {
213	fn from(ir: DeprecationStatusIR) -> Self {
214		match ir {
215			DeprecationStatusIR::NotDeprecated => DeprecationStatus::NotDeprecated,
216			DeprecationStatusIR::DeprecatedWithoutNote => DeprecationStatus::DeprecatedWithoutNote,
217			DeprecationStatusIR::Deprecated { since, note } =>
218				DeprecationStatus::Deprecated { since, note },
219		}
220	}
221}
222
223impl From<DeprecationInfoIR> for DeprecationInfo {
224	fn from(ir: DeprecationInfoIR) -> Self {
225		match ir {
226			DeprecationInfoIR::NotDeprecated => DeprecationInfo::NotDeprecated,
227			DeprecationInfoIR::ItemDeprecated(status) =>
228				DeprecationInfo::ItemDeprecated(status.into()),
229			DeprecationInfoIR::VariantsDeprecated(btree) => DeprecationInfo::VariantsDeprecated(
230				btree.into_iter().map(|(key, value)| (key.0, value.into())).collect(),
231			),
232		}
233	}
234}