sp_metadata_ir/
v14.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 V14 metadata.
19
20use super::types::{
21	ExtrinsicMetadataIR, MetadataIR, PalletCallMetadataIR, PalletConstantMetadataIR,
22	PalletErrorMetadataIR, PalletEventMetadataIR, PalletMetadataIR, PalletStorageMetadataIR,
23	StorageEntryMetadataIR, StorageEntryModifierIR, StorageEntryTypeIR, StorageHasherIR,
24	TransactionExtensionMetadataIR,
25};
26
27use frame_metadata::v14::{
28	ExtrinsicMetadata, PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata,
29	PalletEventMetadata, PalletMetadata, PalletStorageMetadata, RuntimeMetadataV14,
30	SignedExtensionMetadata, StorageEntryMetadata, StorageEntryModifier, StorageEntryType,
31	StorageHasher,
32};
33
34impl From<MetadataIR> for RuntimeMetadataV14 {
35	fn from(ir: MetadataIR) -> Self {
36		RuntimeMetadataV14::new(
37			ir.pallets.into_iter().map(Into::into).collect(),
38			ir.extrinsic.into(),
39			ir.ty,
40		)
41	}
42}
43
44impl From<PalletMetadataIR> for PalletMetadata {
45	fn from(ir: PalletMetadataIR) -> Self {
46		PalletMetadata {
47			name: ir.name,
48			storage: ir.storage.map(Into::into),
49			calls: ir.calls.map(Into::into),
50			event: ir.event.map(Into::into),
51			constants: ir.constants.into_iter().map(Into::into).collect(),
52			error: ir.error.map(Into::into),
53			index: ir.index,
54			// Note: ir.docs not part of v14.
55		}
56	}
57}
58
59impl From<StorageEntryModifierIR> for StorageEntryModifier {
60	fn from(ir: StorageEntryModifierIR) -> Self {
61		match ir {
62			StorageEntryModifierIR::Optional => StorageEntryModifier::Optional,
63			StorageEntryModifierIR::Default => StorageEntryModifier::Default,
64		}
65	}
66}
67
68impl From<StorageHasherIR> for StorageHasher {
69	fn from(ir: StorageHasherIR) -> Self {
70		match ir {
71			StorageHasherIR::Blake2_128 => StorageHasher::Blake2_128,
72			StorageHasherIR::Blake2_256 => StorageHasher::Blake2_256,
73			StorageHasherIR::Blake2_128Concat => StorageHasher::Blake2_128Concat,
74			StorageHasherIR::Twox128 => StorageHasher::Twox128,
75			StorageHasherIR::Twox256 => StorageHasher::Twox256,
76			StorageHasherIR::Twox64Concat => StorageHasher::Twox64Concat,
77			StorageHasherIR::Identity => StorageHasher::Identity,
78		}
79	}
80}
81
82impl From<StorageEntryTypeIR> for StorageEntryType {
83	fn from(ir: StorageEntryTypeIR) -> Self {
84		match ir {
85			StorageEntryTypeIR::Plain(ty) => StorageEntryType::Plain(ty),
86			StorageEntryTypeIR::Map { hashers, key, value } => StorageEntryType::Map {
87				hashers: hashers.into_iter().map(Into::into).collect(),
88				key,
89				value,
90			},
91		}
92	}
93}
94
95impl From<StorageEntryMetadataIR> for StorageEntryMetadata {
96	fn from(ir: StorageEntryMetadataIR) -> Self {
97		StorageEntryMetadata {
98			name: ir.name,
99			modifier: ir.modifier.into(),
100			ty: ir.ty.into(),
101			default: ir.default,
102			docs: ir.docs,
103		}
104	}
105}
106
107impl From<PalletStorageMetadataIR> for PalletStorageMetadata {
108	fn from(ir: PalletStorageMetadataIR) -> Self {
109		PalletStorageMetadata {
110			prefix: ir.prefix,
111			entries: ir.entries.into_iter().map(Into::into).collect(),
112		}
113	}
114}
115
116impl From<PalletCallMetadataIR> for PalletCallMetadata {
117	fn from(ir: PalletCallMetadataIR) -> Self {
118		PalletCallMetadata { ty: ir.ty }
119	}
120}
121
122impl From<PalletEventMetadataIR> for PalletEventMetadata {
123	fn from(ir: PalletEventMetadataIR) -> Self {
124		PalletEventMetadata { ty: ir.ty }
125	}
126}
127
128impl From<PalletConstantMetadataIR> for PalletConstantMetadata {
129	fn from(ir: PalletConstantMetadataIR) -> Self {
130		PalletConstantMetadata { name: ir.name, ty: ir.ty, value: ir.value, docs: ir.docs }
131	}
132}
133
134impl From<PalletErrorMetadataIR> for PalletErrorMetadata {
135	fn from(ir: PalletErrorMetadataIR) -> Self {
136		PalletErrorMetadata { ty: ir.ty }
137	}
138}
139
140impl From<TransactionExtensionMetadataIR> for SignedExtensionMetadata {
141	fn from(ir: TransactionExtensionMetadataIR) -> Self {
142		SignedExtensionMetadata {
143			identifier: ir.identifier,
144			ty: ir.ty,
145			additional_signed: ir.implicit,
146		}
147	}
148}
149
150impl From<ExtrinsicMetadataIR> for ExtrinsicMetadata {
151	fn from(ir: ExtrinsicMetadataIR) -> Self {
152		let lowest_supported_version =
153			ir.versions.iter().min().expect("Metadata V14 supports one version; qed");
154
155		ExtrinsicMetadata {
156			ty: ir.ty,
157			version: *lowest_supported_version,
158			signed_extensions: ir.extensions.into_iter().map(Into::into).collect(),
159		}
160	}
161}