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