1#[cfg(feature = "decode")]
17use codec::Decode;
18#[cfg(feature = "serde_full")]
19use serde::Serialize;
20
21use super::{RuntimeMetadataPrefixed, META_RESERVED};
22use codec::{Compact, Encode};
23use scale_info::{
24 form::{Form, MetaForm, PortableForm},
25 prelude::{collections::BTreeMap, vec::Vec},
26 IntoPortable, PortableRegistry, Registry,
27};
28
29pub use super::v14::{StorageEntryModifier, StorageEntryType, StorageHasher};
31pub use super::v15::{CustomMetadata, CustomValueMetadata, OuterEnums};
32
33pub type FunctionParamMetadata<T> = super::v15::RuntimeApiMethodParamMetadata<T>;
36
37pub type RuntimeMetadataLastVersion = RuntimeMetadataV16;
39
40impl From<RuntimeMetadataLastVersion> for super::RuntimeMetadataPrefixed {
41 fn from(metadata: RuntimeMetadataLastVersion) -> RuntimeMetadataPrefixed {
42 RuntimeMetadataPrefixed(META_RESERVED, super::RuntimeMetadata::V16(metadata))
43 }
44}
45
46#[derive(Clone, PartialEq, Eq, Encode, Debug)]
48#[cfg_attr(feature = "decode", derive(Decode))]
49#[cfg_attr(feature = "serde_full", derive(Serialize))]
50pub struct RuntimeMetadataV16 {
51 pub types: PortableRegistry,
53 pub pallets: Vec<PalletMetadata<PortableForm>>,
55 pub extrinsic: ExtrinsicMetadata<PortableForm>,
57 pub apis: Vec<RuntimeApiMetadata<PortableForm>>,
59 pub outer_enums: OuterEnums<PortableForm>,
61 pub custom: CustomMetadata<PortableForm>,
63}
64
65impl RuntimeMetadataV16 {
66 pub fn new(
68 pallets: Vec<PalletMetadata>,
69 extrinsic: ExtrinsicMetadata,
70 apis: Vec<RuntimeApiMetadata>,
71 outer_enums: OuterEnums,
72 custom: CustomMetadata,
73 ) -> Self {
74 let mut registry = Registry::new();
75 let extrinsic = extrinsic.into_portable(&mut registry);
78 let pallets = registry.map_into_portable(pallets);
79 let apis = registry.map_into_portable(apis);
80 let outer_enums = outer_enums.into_portable(&mut registry);
81 let custom = custom.into_portable(&mut registry);
82
83 Self {
84 types: registry.into(),
85 pallets,
86 extrinsic,
87 apis,
88 outer_enums,
89 custom,
90 }
91 }
92}
93
94#[derive(Clone, PartialEq, Eq, Encode, Debug)]
96#[cfg_attr(feature = "decode", derive(Decode))]
97#[cfg_attr(feature = "serde_full", derive(Serialize))]
98#[cfg_attr(
99 feature = "serde_full",
100 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
101)]
102pub struct RuntimeApiMetadata<T: Form = MetaForm> {
103 pub name: T::String,
105 pub methods: Vec<RuntimeApiMethodMetadata<T>>,
107 pub docs: Vec<T::String>,
109 pub version: Compact<u32>,
111 pub deprecation_info: ItemDeprecationInfo<T>,
113}
114
115impl IntoPortable for RuntimeApiMetadata {
116 type Output = RuntimeApiMetadata<PortableForm>;
117
118 fn into_portable(self, registry: &mut Registry) -> Self::Output {
119 RuntimeApiMetadata {
120 name: self.name.into_portable(registry),
121 methods: registry.map_into_portable(self.methods),
122 docs: registry.map_into_portable(self.docs),
123 version: self.version,
124 deprecation_info: self.deprecation_info.into_portable(registry),
125 }
126 }
127}
128
129#[derive(Clone, PartialEq, Eq, Encode, Debug)]
131#[cfg_attr(feature = "decode", derive(Decode))]
132#[cfg_attr(feature = "serde_full", derive(Serialize))]
133#[cfg_attr(
134 feature = "serde_full",
135 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
136)]
137pub struct RuntimeApiMethodMetadata<T: Form = MetaForm> {
138 pub name: T::String,
140 pub inputs: Vec<FunctionParamMetadata<T>>,
142 pub output: T::Type,
144 pub docs: Vec<T::String>,
146 pub deprecation_info: ItemDeprecationInfo<T>,
148}
149
150impl IntoPortable for RuntimeApiMethodMetadata {
151 type Output = RuntimeApiMethodMetadata<PortableForm>;
152
153 fn into_portable(self, registry: &mut Registry) -> Self::Output {
154 RuntimeApiMethodMetadata {
155 name: self.name.into_portable(registry),
156 inputs: registry.map_into_portable(self.inputs),
157 output: registry.register_type(&self.output),
158 docs: registry.map_into_portable(self.docs),
159 deprecation_info: self.deprecation_info.into_portable(registry),
160 }
161 }
162}
163
164#[derive(Clone, PartialEq, Eq, Encode, Debug)]
166#[cfg_attr(feature = "decode", derive(Decode))]
167#[cfg_attr(feature = "serde_full", derive(Serialize))]
168#[cfg_attr(
169 feature = "serde_full",
170 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
171)]
172pub struct ExtrinsicMetadata<T: Form = MetaForm> {
173 pub versions: Vec<u8>,
175 pub address_ty: T::Type,
177 pub call_ty: T::Type,
182 pub signature_ty: T::Type,
184 pub transaction_extensions_by_version: BTreeMap<u8, Vec<Compact<u32>>>,
188 pub transaction_extensions: Vec<TransactionExtensionMetadata<T>>,
190}
191
192impl IntoPortable for ExtrinsicMetadata {
193 type Output = ExtrinsicMetadata<PortableForm>;
194
195 fn into_portable(self, registry: &mut Registry) -> Self::Output {
196 ExtrinsicMetadata {
199 versions: self.versions,
200 address_ty: registry.register_type(&self.address_ty),
201 call_ty: registry.register_type(&self.call_ty),
202 signature_ty: registry.register_type(&self.signature_ty),
203 transaction_extensions_by_version: self.transaction_extensions_by_version,
204 transaction_extensions: registry.map_into_portable(self.transaction_extensions),
205 }
206 }
207}
208
209#[derive(Clone, PartialEq, Eq, Encode, Debug)]
211#[cfg_attr(feature = "decode", derive(Decode))]
212#[cfg_attr(feature = "serde_full", derive(Serialize))]
213#[cfg_attr(
214 feature = "serde_full",
215 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
216)]
217pub struct TransactionExtensionMetadata<T: Form = MetaForm> {
218 pub identifier: T::String,
220 pub ty: T::Type,
222 pub implicit: T::Type,
224}
225
226impl IntoPortable for TransactionExtensionMetadata {
227 type Output = TransactionExtensionMetadata<PortableForm>;
228
229 fn into_portable(self, registry: &mut Registry) -> Self::Output {
230 TransactionExtensionMetadata {
231 identifier: self.identifier.into_portable(registry),
232 ty: registry.register_type(&self.ty),
233 implicit: registry.register_type(&self.implicit),
234 }
235 }
236}
237
238#[derive(Clone, PartialEq, Eq, Encode, Debug)]
240#[cfg_attr(feature = "decode", derive(Decode))]
241#[cfg_attr(feature = "serde_full", derive(Serialize))]
242#[cfg_attr(
243 feature = "serde_full",
244 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
245)]
246pub struct PalletMetadata<T: Form = MetaForm> {
247 pub name: T::String,
249 pub storage: Option<PalletStorageMetadata<T>>,
251 pub calls: Option<PalletCallMetadata<T>>,
253 pub event: Option<PalletEventMetadata<T>>,
255 pub constants: Vec<PalletConstantMetadata<T>>,
257 pub error: Option<PalletErrorMetadata<T>>,
259 pub associated_types: Vec<PalletAssociatedTypeMetadata<T>>,
261 pub view_functions: Vec<PalletViewFunctionMetadata<T>>,
263 pub index: u8,
266 pub docs: Vec<T::String>,
268 pub deprecation_info: ItemDeprecationInfo<T>,
270}
271
272impl IntoPortable for PalletMetadata {
273 type Output = PalletMetadata<PortableForm>;
274
275 fn into_portable(self, registry: &mut Registry) -> Self::Output {
276 PalletMetadata {
277 name: self.name.into_portable(registry),
278 storage: self.storage.map(|storage| storage.into_portable(registry)),
279 calls: self.calls.map(|calls| calls.into_portable(registry)),
280 event: self.event.map(|event| event.into_portable(registry)),
281 constants: registry.map_into_portable(self.constants),
282 error: self.error.map(|error| error.into_portable(registry)),
283 associated_types: registry.map_into_portable(self.associated_types),
284 view_functions: registry.map_into_portable(self.view_functions),
285 index: self.index,
286 docs: registry.map_into_portable(self.docs),
287 deprecation_info: self.deprecation_info.into_portable(registry),
288 }
289 }
290}
291
292#[derive(Clone, PartialEq, Eq, Encode, Debug)]
294#[cfg_attr(feature = "decode", derive(Decode))]
295#[cfg_attr(feature = "serde_full", derive(Serialize))]
296#[cfg_attr(
297 feature = "serde_full",
298 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
299)]
300pub struct PalletCallMetadata<T: Form = MetaForm> {
301 pub ty: T::Type,
303 pub deprecation_info: EnumDeprecationInfo<T>,
305}
306
307impl IntoPortable for PalletCallMetadata {
308 type Output = PalletCallMetadata<PortableForm>;
309
310 fn into_portable(self, registry: &mut Registry) -> Self::Output {
311 PalletCallMetadata {
312 ty: registry.register_type(&self.ty),
313 deprecation_info: self.deprecation_info.into_portable(registry),
314 }
315 }
316}
317
318#[derive(Clone, PartialEq, Eq, Encode, Debug)]
320#[cfg_attr(feature = "decode", derive(Decode))]
321#[cfg_attr(feature = "serde_full", derive(Serialize))]
322#[cfg_attr(
323 feature = "serde_full",
324 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
325)]
326pub struct PalletStorageMetadata<T: Form = MetaForm> {
327 pub prefix: T::String,
329 pub entries: Vec<StorageEntryMetadata<T>>,
331}
332
333impl IntoPortable for PalletStorageMetadata {
334 type Output = PalletStorageMetadata<PortableForm>;
335
336 fn into_portable(self, registry: &mut Registry) -> Self::Output {
337 PalletStorageMetadata {
338 prefix: self.prefix.into_portable(registry),
339 entries: registry.map_into_portable(self.entries),
340 }
341 }
342}
343
344#[derive(Clone, PartialEq, Eq, Encode, Debug)]
346#[cfg_attr(feature = "decode", derive(Decode))]
347#[cfg_attr(feature = "serde_full", derive(Serialize))]
348#[cfg_attr(
349 feature = "serde_full",
350 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
351)]
352pub struct StorageEntryMetadata<T: Form = MetaForm> {
353 pub name: T::String,
355 pub modifier: StorageEntryModifier,
357 pub ty: StorageEntryType<T>,
359 pub default: Vec<u8>,
361 pub docs: Vec<T::String>,
363 pub deprecation_info: ItemDeprecationInfo<T>,
365}
366
367impl IntoPortable for StorageEntryMetadata {
368 type Output = StorageEntryMetadata<PortableForm>;
369
370 fn into_portable(self, registry: &mut Registry) -> Self::Output {
371 StorageEntryMetadata {
372 name: self.name.into_portable(registry),
373 modifier: self.modifier,
374 ty: self.ty.into_portable(registry),
375 default: self.default,
376 docs: registry.map_into_portable(self.docs),
377 deprecation_info: self.deprecation_info.into_portable(registry),
378 }
379 }
380}
381
382#[derive(Clone, PartialEq, Eq, Encode, Debug)]
384#[cfg_attr(feature = "decode", derive(Decode))]
385#[cfg_attr(feature = "serde_full", derive(Serialize))]
386#[cfg_attr(
387 feature = "serde_full",
388 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
389)]
390pub struct PalletEventMetadata<T: Form = MetaForm> {
391 pub ty: T::Type,
393 pub deprecation_info: EnumDeprecationInfo<T>,
395}
396
397impl IntoPortable for PalletEventMetadata {
398 type Output = PalletEventMetadata<PortableForm>;
399
400 fn into_portable(self, registry: &mut Registry) -> Self::Output {
401 PalletEventMetadata {
402 ty: registry.register_type(&self.ty),
403 deprecation_info: self.deprecation_info.into_portable(registry),
404 }
405 }
406}
407
408#[derive(Clone, PartialEq, Eq, Encode, Debug)]
410#[cfg_attr(feature = "decode", derive(Decode))]
411#[cfg_attr(feature = "serde_full", derive(Serialize))]
412#[cfg_attr(
413 feature = "serde_full",
414 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
415)]
416pub struct PalletConstantMetadata<T: Form = MetaForm> {
417 pub name: T::String,
419 pub ty: T::Type,
421 pub value: Vec<u8>,
423 pub docs: Vec<T::String>,
425 pub deprecation_info: ItemDeprecationInfo<T>,
427}
428
429impl IntoPortable for PalletConstantMetadata {
430 type Output = PalletConstantMetadata<PortableForm>;
431
432 fn into_portable(self, registry: &mut Registry) -> Self::Output {
433 PalletConstantMetadata {
434 name: self.name.into_portable(registry),
435 ty: registry.register_type(&self.ty),
436 value: self.value,
437 docs: registry.map_into_portable(self.docs),
438 deprecation_info: self.deprecation_info.into_portable(registry),
439 }
440 }
441}
442
443#[derive(Clone, PartialEq, Eq, Encode, Debug)]
445#[cfg_attr(feature = "decode", derive(Decode))]
446#[cfg_attr(feature = "serde_full", derive(Serialize))]
447#[cfg_attr(
448 feature = "serde_full",
449 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
450)]
451pub struct PalletErrorMetadata<T: Form = MetaForm> {
452 pub ty: T::Type,
454 pub deprecation_info: EnumDeprecationInfo<T>,
456}
457
458impl IntoPortable for PalletErrorMetadata {
459 type Output = PalletErrorMetadata<PortableForm>;
460
461 fn into_portable(self, registry: &mut Registry) -> Self::Output {
462 PalletErrorMetadata {
463 ty: registry.register_type(&self.ty),
464 deprecation_info: self.deprecation_info.into_portable(registry),
465 }
466 }
467}
468
469#[derive(Clone, PartialEq, Eq, Encode, Debug)]
471#[cfg_attr(feature = "decode", derive(Decode))]
472#[cfg_attr(feature = "serde_full", derive(Serialize))]
473#[cfg_attr(
474 feature = "serde_full",
475 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
476)]
477pub struct PalletAssociatedTypeMetadata<T: Form = MetaForm> {
478 pub name: T::String,
480 pub ty: T::Type,
482 pub docs: Vec<T::String>,
484}
485
486impl IntoPortable for PalletAssociatedTypeMetadata {
487 type Output = PalletAssociatedTypeMetadata<PortableForm>;
488
489 fn into_portable(self, registry: &mut Registry) -> Self::Output {
490 PalletAssociatedTypeMetadata {
491 name: self.name.into_portable(registry),
492 ty: registry.register_type(&self.ty),
493 docs: registry.map_into_portable(self.docs),
494 }
495 }
496}
497
498#[derive(Clone, PartialEq, Eq, Encode, Debug)]
500#[cfg_attr(feature = "decode", derive(Decode))]
501#[cfg_attr(feature = "serde_full", derive(Serialize))]
502#[cfg_attr(
503 feature = "serde_full",
504 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
505)]
506pub struct PalletViewFunctionMetadata<T: Form = MetaForm> {
507 pub id: [u8; 32],
509 pub name: T::String,
511 pub inputs: Vec<FunctionParamMetadata<T>>,
513 pub output: T::Type,
515 pub docs: Vec<T::String>,
517 pub deprecation_info: ItemDeprecationInfo<T>,
519}
520
521impl IntoPortable for PalletViewFunctionMetadata {
522 type Output = PalletViewFunctionMetadata<PortableForm>;
523
524 fn into_portable(self, registry: &mut Registry) -> Self::Output {
525 PalletViewFunctionMetadata {
526 id: self.id,
527 name: self.name.into_portable(registry),
528 inputs: registry.map_into_portable(self.inputs),
529 output: registry.register_type(&self.output),
530 docs: registry.map_into_portable(self.docs),
531 deprecation_info: self.deprecation_info.into_portable(registry),
532 }
533 }
534}
535
536#[derive(Clone, PartialEq, Eq, Encode, Debug)]
538#[cfg_attr(feature = "decode", derive(Decode))]
539#[cfg_attr(feature = "serde_full", derive(Serialize))]
540#[cfg_attr(
541 feature = "serde_full",
542 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
543)]
544pub enum ItemDeprecationInfo<T: Form = MetaForm> {
545 NotDeprecated,
547 DeprecatedWithoutNote,
549 Deprecated {
551 note: T::String,
553 since: Option<T::String>,
555 },
556}
557
558impl IntoPortable for ItemDeprecationInfo {
559 type Output = ItemDeprecationInfo<PortableForm>;
560
561 fn into_portable(self, registry: &mut Registry) -> Self::Output {
562 match self {
563 Self::NotDeprecated => ItemDeprecationInfo::NotDeprecated,
564 Self::DeprecatedWithoutNote => ItemDeprecationInfo::DeprecatedWithoutNote,
565 Self::Deprecated { note, since } => {
566 let note = note.into_portable(registry);
567 let since = since.map(|x| x.into_portable(registry));
568 ItemDeprecationInfo::Deprecated { note, since }
569 }
570 }
571 }
572}
573
574#[derive(Clone, PartialEq, Eq, Encode, Debug)]
577#[cfg_attr(feature = "decode", derive(Decode))]
578#[cfg_attr(feature = "serde_full", derive(Serialize))]
579#[cfg_attr(
580 feature = "serde_full",
581 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
582)]
583pub struct EnumDeprecationInfo<T: Form = MetaForm>(pub BTreeMap<u8, VariantDeprecationInfo<T>>);
584
585impl<T: Form> EnumDeprecationInfo<T> {
586 pub fn nothing_deprecated() -> Self {
588 Self(BTreeMap::new())
589 }
590
591 pub fn has_deprecated_variants(&self) -> bool {
593 !self.0.is_empty()
594 }
595
596 pub fn is_variant_deprecated(&self, variant_index: u8) -> bool {
598 self.0.contains_key(&variant_index)
599 }
600}
601
602impl IntoPortable for EnumDeprecationInfo {
603 type Output = EnumDeprecationInfo<PortableForm>;
604
605 fn into_portable(self, registry: &mut Registry) -> Self::Output {
606 let entries = self
607 .0
608 .into_iter()
609 .map(|(k, entry)| (k, entry.into_portable(registry)));
610 EnumDeprecationInfo(entries.collect())
611 }
612}
613
614#[derive(Clone, PartialEq, Eq, Encode, Debug)]
618#[cfg_attr(feature = "decode", derive(Decode))]
619#[cfg_attr(feature = "serde_full", derive(Serialize))]
620#[cfg_attr(
621 feature = "serde_full",
622 serde(bound(serialize = "T::Type: Serialize, T::String: Serialize"))
623)]
624pub enum VariantDeprecationInfo<T: Form = MetaForm> {
625 #[codec(index = 1)]
627 DeprecatedWithoutNote,
628 #[codec(index = 2)]
630 Deprecated {
631 note: T::String,
633 since: Option<T::String>,
635 },
636}
637
638impl IntoPortable for VariantDeprecationInfo {
639 type Output = VariantDeprecationInfo<PortableForm>;
640
641 fn into_portable(self, registry: &mut Registry) -> Self::Output {
642 match self {
643 Self::Deprecated { note, since } => {
644 let note = note.into_portable(registry);
645 let since = since.map(|x| x.into_portable(registry));
646 VariantDeprecationInfo::Deprecated { note, since }
647 }
648 Self::DeprecatedWithoutNote => VariantDeprecationInfo::DeprecatedWithoutNote,
649 }
650 }
651}