referrerpolicy=no-referrer-when-downgrade

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