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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// 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

use crate::construct_runtime::{parse::PalletPath, Pallet};
use proc_macro2::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::Ident;

pub fn expand_runtime_metadata(
	runtime: &Ident,
	pallet_declarations: &[Pallet],
	scrate: &TokenStream,
	extrinsic: &TokenStream,
	system_path: &PalletPath,
) -> TokenStream {
	let pallets = pallet_declarations
		.iter()
		.filter_map(|pallet_declaration| {
			pallet_declaration.find_part("Pallet").map(|_| {
				let filtered_names: Vec<_> = pallet_declaration
					.pallet_parts()
					.iter()
					.filter(|part| part.name() != "Pallet")
					.map(|part| part.name())
					.collect();
				(pallet_declaration, filtered_names)
			})
		})
		.map(|(decl, filtered_names)| {
			let name = &decl.name;
			let index = &decl.index;
			let storage = expand_pallet_metadata_storage(&filtered_names, runtime, decl);
			let calls = expand_pallet_metadata_calls(&filtered_names, runtime, decl);
			let event = expand_pallet_metadata_events(&filtered_names, runtime, decl);
			let constants = expand_pallet_metadata_constants(runtime, decl);
			let errors = expand_pallet_metadata_errors(runtime, decl);
			let docs = expand_pallet_metadata_docs(runtime, decl);
			let attr = decl.cfg_pattern.iter().fold(TokenStream::new(), |acc, pattern| {
				let attr = TokenStream::from_str(&format!("#[cfg({})]", pattern.original()))
					.expect("was successfully parsed before; qed");
				quote! {
					#acc
					#attr
				}
			});
			let deprecation_info = expand_pallet_metadata_deprecation(runtime, decl);
			quote! {
				#attr
				#scrate::__private::metadata_ir::PalletMetadataIR {
					name: stringify!(#name),
					index: #index,
					storage: #storage,
					calls: #calls,
					event: #event,
					constants: #constants,
					error: #errors,
					docs: #docs,
					deprecation_info: #deprecation_info,
				}
			}
		})
		.collect::<Vec<_>>();

	quote! {
		impl #runtime {
			fn metadata_ir() -> #scrate::__private::metadata_ir::MetadataIR {
				// Each runtime must expose the `runtime_metadata()` to fetch the runtime API metadata.
				// The function is implemented by calling `impl_runtime_apis!`.
				//
				// However, the `construct_runtime!` may be called without calling `impl_runtime_apis!`.
				// Rely on the `Deref` trait to differentiate between a runtime that implements
				// APIs (by macro impl_runtime_apis!) and a runtime that is simply created (by macro construct_runtime!).
				//
				// Both `InternalConstructRuntime` and `InternalImplRuntimeApis` expose a `runtime_metadata()` function.
				// `InternalConstructRuntime` is implemented by the `construct_runtime!` for Runtime references (`& Runtime`),
				// while `InternalImplRuntimeApis` is implemented by the `impl_runtime_apis!` for Runtime (`Runtime`).
				//
				// Therefore, the `Deref` trait will resolve the `runtime_metadata` from `impl_runtime_apis!`
				// when both macros are called; and will resolve an empty `runtime_metadata` when only the `construct_runtime!`
				// is called.
				//
				// `Deref` needs a reference for resolving the function call.
				let rt = #runtime;

				let ty = #scrate::__private::scale_info::meta_type::<#extrinsic>();
				let address_ty = #scrate::__private::scale_info::meta_type::<
						<<#extrinsic as #scrate::sp_runtime::traits::Extrinsic>::SignaturePayload as #scrate::sp_runtime::traits::SignaturePayload>::SignatureAddress
					>();
				let call_ty = #scrate::__private::scale_info::meta_type::<
					<#extrinsic as #scrate::sp_runtime::traits::Extrinsic>::Call
					>();
				let signature_ty = #scrate::__private::scale_info::meta_type::<
						<<#extrinsic as #scrate::sp_runtime::traits::Extrinsic>::SignaturePayload as #scrate::sp_runtime::traits::SignaturePayload>::Signature
					>();
				let extra_ty = #scrate::__private::scale_info::meta_type::<
						<<#extrinsic as #scrate::sp_runtime::traits::Extrinsic>::SignaturePayload as #scrate::sp_runtime::traits::SignaturePayload>::SignatureExtra
					>();

				#scrate::__private::metadata_ir::MetadataIR {
					pallets: #scrate::__private::vec![ #(#pallets),* ],
					extrinsic: #scrate::__private::metadata_ir::ExtrinsicMetadataIR {
						ty,
						version: <#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata>::VERSION,
						address_ty,
						call_ty,
						signature_ty,
						extra_ty,
						signed_extensions: <
								<
									#extrinsic as #scrate::sp_runtime::traits::ExtrinsicMetadata
								>::SignedExtensions as #scrate::sp_runtime::traits::SignedExtension
							>::metadata()
								.into_iter()
								.map(|meta| #scrate::__private::metadata_ir::SignedExtensionMetadataIR {
									identifier: meta.identifier,
									ty: meta.ty,
									additional_signed: meta.additional_signed,
								})
								.collect(),
					},
					ty: #scrate::__private::scale_info::meta_type::<#runtime>(),
					apis: (&rt).runtime_metadata(),
					outer_enums: #scrate::__private::metadata_ir::OuterEnumsIR {
						call_enum_ty: #scrate::__private::scale_info::meta_type::<
								<#runtime as #system_path::Config>::RuntimeCall
							>(),
						event_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeEvent>(),
						error_enum_ty: #scrate::__private::scale_info::meta_type::<RuntimeError>(),
					}
				}
			}

			pub fn metadata() -> #scrate::__private::metadata::RuntimeMetadataPrefixed {
				// Note: this always returns the V14 version. The runtime API function
				// must be deprecated.
				#scrate::__private::metadata_ir::into_v14(#runtime::metadata_ir())
			}

			pub fn metadata_at_version(version: u32) -> Option<#scrate::__private::OpaqueMetadata> {
				#scrate::__private::metadata_ir::into_version(#runtime::metadata_ir(), version).map(|prefixed| {
					#scrate::__private::OpaqueMetadata::new(prefixed.into())
				})
			}

			pub fn metadata_versions() -> #scrate::__private::Vec<u32> {
				#scrate::__private::metadata_ir::supported_versions()
			}
		}
	}
}

fn expand_pallet_metadata_storage(
	filtered_names: &[&'static str],
	runtime: &Ident,
	decl: &Pallet,
) -> TokenStream {
	if filtered_names.contains(&"Storage") {
		let instance = decl.instance.as_ref().into_iter();
		let path = &decl.path;

		quote! {
			Some(#path::Pallet::<#runtime #(, #path::#instance)*>::storage_metadata())
		}
	} else {
		quote!(None)
	}
}

fn expand_pallet_metadata_calls(
	filtered_names: &[&'static str],
	runtime: &Ident,
	decl: &Pallet,
) -> TokenStream {
	if filtered_names.contains(&"Call") {
		let instance = decl.instance.as_ref().into_iter();
		let path = &decl.path;

		quote! {
			Some(#path::Pallet::<#runtime #(, #path::#instance)*>::call_functions())
		}
	} else {
		quote!(None)
	}
}

fn expand_pallet_metadata_events(
	filtered_names: &[&'static str],
	runtime: &Ident,
	decl: &Pallet,
) -> TokenStream {
	if filtered_names.contains(&"Event") {
		let path = &decl.path;
		let part_is_generic = !decl
			.find_part("Event")
			.expect("Event part exists; qed")
			.generics
			.params
			.is_empty();
		let pallet_event = match (decl.instance.as_ref(), part_is_generic) {
			(Some(inst), true) => quote!(#path::Event::<#runtime, #path::#inst>),
			(Some(inst), false) => quote!(#path::Event::<#path::#inst>),
			(None, true) => quote!(#path::Event::<#runtime>),
			(None, false) => quote!(#path::Event),
		};

		quote! {
			Some(
				#pallet_event::event_metadata::<#pallet_event>()
			)
		}
	} else {
		quote!(None)
	}
}

fn expand_pallet_metadata_deprecation(runtime: &Ident, decl: &Pallet) -> TokenStream {
	let path = &decl.path;
	let instance = decl.instance.as_ref().into_iter();

	quote! { #path::Pallet::<#runtime #(, #path::#instance)*>::deprecation_info() }
}

fn expand_pallet_metadata_constants(runtime: &Ident, decl: &Pallet) -> TokenStream {
	let path = &decl.path;
	let instance = decl.instance.as_ref().into_iter();

	quote! {
		#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_constants_metadata()
	}
}

fn expand_pallet_metadata_errors(runtime: &Ident, decl: &Pallet) -> TokenStream {
	let path = &decl.path;
	let instance = decl.instance.as_ref().into_iter();

	quote! {
		#path::Pallet::<#runtime #(, #path::#instance)*>::error_metadata()
	}
}

fn expand_pallet_metadata_docs(runtime: &Ident, decl: &Pallet) -> TokenStream {
	let path = &decl.path;
	let instance = decl.instance.as_ref().into_iter();

	quote! {
		#path::Pallet::<#runtime #(, #path::#instance)*>::pallet_documentation_metadata()
	}
}