referrerpolicy=no-referrer-when-downgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Convert the IR to V16 metadata.

use crate::{
	DeprecationInfoIR, DeprecationStatusIR, OuterEnumsIR, PalletAssociatedTypeMetadataIR,
	PalletCallMetadataIR, PalletConstantMetadataIR, PalletErrorMetadataIR, PalletEventMetadataIR,
	PalletStorageMetadataIR, StorageEntryMetadataIR,
};

use super::types::{
	ExtrinsicMetadataIR, MetadataIR, PalletMetadataIR, RuntimeApiMetadataIR,
	RuntimeApiMethodMetadataIR, RuntimeApiMethodParamMetadataIR, TransactionExtensionMetadataIR,
};

use frame_metadata::v16::{
	CustomMetadata, DeprecationInfo, DeprecationStatus, ExtrinsicMetadata, OuterEnums,
	PalletAssociatedTypeMetadata, PalletCallMetadata, PalletConstantMetadata, PalletErrorMetadata,
	PalletEventMetadata, PalletMetadata, PalletStorageMetadata, RuntimeApiMetadata,
	RuntimeApiMethodMetadata, RuntimeApiMethodParamMetadata, RuntimeMetadataV16,
	StorageEntryMetadata, TransactionExtensionMetadata,
};

impl From<MetadataIR> for RuntimeMetadataV16 {
	fn from(ir: MetadataIR) -> Self {
		RuntimeMetadataV16::new(
			ir.pallets.into_iter().map(Into::into).collect(),
			ir.extrinsic.into(),
			ir.apis.into_iter().map(Into::into).collect(),
			ir.outer_enums.into(),
			// Substrate does not collect yet the custom metadata fields.
			// This allows us to extend the V16 easily.
			CustomMetadata { map: Default::default() },
		)
	}
}

impl From<RuntimeApiMetadataIR> for RuntimeApiMetadata {
	fn from(ir: RuntimeApiMetadataIR) -> Self {
		RuntimeApiMetadata {
			name: ir.name,
			methods: ir.methods.into_iter().map(Into::into).collect(),
			docs: ir.docs,
			deprecation_info: ir.deprecation_info.into(),
		}
	}
}

impl From<RuntimeApiMethodMetadataIR> for RuntimeApiMethodMetadata {
	fn from(ir: RuntimeApiMethodMetadataIR) -> Self {
		RuntimeApiMethodMetadata {
			name: ir.name,
			inputs: ir.inputs.into_iter().map(Into::into).collect(),
			output: ir.output,
			docs: ir.docs,
			deprecation_info: ir.deprecation_info.into(),
		}
	}
}

impl From<RuntimeApiMethodParamMetadataIR> for RuntimeApiMethodParamMetadata {
	fn from(ir: RuntimeApiMethodParamMetadataIR) -> Self {
		RuntimeApiMethodParamMetadata { name: ir.name, ty: ir.ty }
	}
}

impl From<PalletMetadataIR> for PalletMetadata {
	fn from(ir: PalletMetadataIR) -> Self {
		PalletMetadata {
			name: ir.name,
			storage: ir.storage.map(Into::into),
			calls: ir.calls.map(Into::into),
			event: ir.event.map(Into::into),
			constants: ir.constants.into_iter().map(Into::into).collect(),
			error: ir.error.map(Into::into),
			index: ir.index,
			docs: ir.docs,
			associated_types: ir.associated_types.into_iter().map(Into::into).collect(),
			deprecation_info: ir.deprecation_info.into(),
		}
	}
}

impl From<PalletStorageMetadataIR> for PalletStorageMetadata {
	fn from(ir: PalletStorageMetadataIR) -> Self {
		PalletStorageMetadata {
			prefix: ir.prefix,
			entries: ir.entries.into_iter().map(Into::into).collect(),
		}
	}
}

impl From<StorageEntryMetadataIR> for StorageEntryMetadata {
	fn from(ir: StorageEntryMetadataIR) -> Self {
		StorageEntryMetadata {
			name: ir.name,
			modifier: ir.modifier.into(),
			ty: ir.ty.into(),
			default: ir.default,
			docs: ir.docs,
			deprecation_info: ir.deprecation_info.into(),
		}
	}
}

impl From<PalletAssociatedTypeMetadataIR> for PalletAssociatedTypeMetadata {
	fn from(ir: PalletAssociatedTypeMetadataIR) -> Self {
		PalletAssociatedTypeMetadata { name: ir.name, ty: ir.ty, docs: ir.docs }
	}
}

impl From<PalletErrorMetadataIR> for PalletErrorMetadata {
	fn from(ir: PalletErrorMetadataIR) -> Self {
		PalletErrorMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
	}
}

impl From<PalletEventMetadataIR> for PalletEventMetadata {
	fn from(ir: PalletEventMetadataIR) -> Self {
		PalletEventMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
	}
}

impl From<PalletCallMetadataIR> for PalletCallMetadata {
	fn from(ir: PalletCallMetadataIR) -> Self {
		PalletCallMetadata { ty: ir.ty, deprecation_info: ir.deprecation_info.into() }
	}
}

impl From<PalletConstantMetadataIR> for PalletConstantMetadata {
	fn from(ir: PalletConstantMetadataIR) -> Self {
		PalletConstantMetadata {
			name: ir.name,
			ty: ir.ty,
			value: ir.value,
			docs: ir.docs,
			deprecation_info: ir.deprecation_info.into(),
		}
	}
}

impl From<TransactionExtensionMetadataIR> for TransactionExtensionMetadata {
	fn from(ir: TransactionExtensionMetadataIR) -> Self {
		TransactionExtensionMetadata { identifier: ir.identifier, ty: ir.ty, implicit: ir.implicit }
	}
}

impl From<ExtrinsicMetadataIR> for ExtrinsicMetadata {
	fn from(ir: ExtrinsicMetadataIR) -> Self {
		// Assume version 0 for all extensions.
		let indexes = (0..ir.extensions.len()).map(|index| index as u32).collect();
		let transaction_extensions_by_version = [(0, indexes)].iter().cloned().collect();

		ExtrinsicMetadata {
			versions: ir.versions,
			address_ty: ir.address_ty,
			signature_ty: ir.signature_ty,
			transaction_extensions_by_version,
			transaction_extensions: ir.extensions.into_iter().map(Into::into).collect(),
		}
	}
}

impl From<OuterEnumsIR> for OuterEnums {
	fn from(ir: OuterEnumsIR) -> Self {
		OuterEnums {
			call_enum_ty: ir.call_enum_ty,
			event_enum_ty: ir.event_enum_ty,
			error_enum_ty: ir.error_enum_ty,
		}
	}
}

impl From<DeprecationStatusIR> for DeprecationStatus {
	fn from(ir: DeprecationStatusIR) -> Self {
		match ir {
			DeprecationStatusIR::NotDeprecated => DeprecationStatus::NotDeprecated,
			DeprecationStatusIR::DeprecatedWithoutNote => DeprecationStatus::DeprecatedWithoutNote,
			DeprecationStatusIR::Deprecated { since, note } =>
				DeprecationStatus::Deprecated { since, note },
		}
	}
}

impl From<DeprecationInfoIR> for DeprecationInfo {
	fn from(ir: DeprecationInfoIR) -> Self {
		match ir {
			DeprecationInfoIR::NotDeprecated => DeprecationInfo::NotDeprecated,
			DeprecationInfoIR::ItemDeprecated(status) =>
				DeprecationInfo::ItemDeprecated(status.into()),
			DeprecationInfoIR::VariantsDeprecated(btree) => DeprecationInfo::VariantsDeprecated(
				btree.into_iter().map(|(key, value)| (key.0, value.into())).collect(),
			),
		}
	}
}