1use codec::{Compact, Decode, Encode};
19use derive_where::derive_where;
20use scale_info::{
21 form::{Form, MetaForm, PortableForm},
22 prelude::{collections::BTreeMap, vec::Vec},
23 IntoPortable, Registry,
24};
25
26pub struct MetadataIR<T: Form = MetaForm> {
35 pub pallets: Vec<PalletMetadataIR<T>>,
37 pub extrinsic: ExtrinsicMetadataIR<T>,
39 pub ty: T::Type,
41 pub apis: Vec<RuntimeApiMetadataIR<T>>,
43 pub outer_enums: OuterEnumsIR<T>,
45}
46
47#[derive(Encode)]
49#[derive_where(Clone, PartialEq, Eq, Debug;)]
50pub struct RuntimeApiMetadataIR<T: Form = MetaForm> {
51 pub name: T::String,
53 pub methods: Vec<RuntimeApiMethodMetadataIR<T>>,
55 pub docs: Vec<T::String>,
57 pub deprecation_info: ItemDeprecationInfoIR<T>,
59 pub version: Compact<u32>,
61}
62
63impl IntoPortable for RuntimeApiMetadataIR {
64 type Output = RuntimeApiMetadataIR<PortableForm>;
65
66 fn into_portable(self, registry: &mut Registry) -> Self::Output {
67 RuntimeApiMetadataIR {
68 name: self.name.into_portable(registry),
69 methods: registry.map_into_portable(self.methods),
70 docs: registry.map_into_portable(self.docs),
71 deprecation_info: self.deprecation_info.into_portable(registry),
72 version: self.version,
73 }
74 }
75}
76
77#[derive(Encode)]
79#[derive_where(Clone, PartialEq, Eq, Debug;)]
80pub struct RuntimeApiMethodMetadataIR<T: Form = MetaForm> {
81 pub name: T::String,
83 pub inputs: Vec<RuntimeApiMethodParamMetadataIR<T>>,
85 pub output: T::Type,
87 pub docs: Vec<T::String>,
89 pub deprecation_info: ItemDeprecationInfoIR<T>,
91}
92
93impl IntoPortable for RuntimeApiMethodMetadataIR {
94 type Output = RuntimeApiMethodMetadataIR<PortableForm>;
95
96 fn into_portable(self, registry: &mut Registry) -> Self::Output {
97 RuntimeApiMethodMetadataIR {
98 name: self.name.into_portable(registry),
99 inputs: registry.map_into_portable(self.inputs),
100 output: registry.register_type(&self.output),
101 docs: registry.map_into_portable(self.docs),
102 deprecation_info: self.deprecation_info.into_portable(registry),
103 }
104 }
105}
106
107#[derive(Encode)]
109#[derive_where(Clone, PartialEq, Eq, Debug;)]
110pub struct RuntimeApiMethodParamMetadataIR<T: Form = MetaForm> {
111 pub name: T::String,
113 pub ty: T::Type,
115}
116
117impl IntoPortable for RuntimeApiMethodParamMetadataIR {
118 type Output = RuntimeApiMethodParamMetadataIR<PortableForm>;
119
120 fn into_portable(self, registry: &mut Registry) -> Self::Output {
121 RuntimeApiMethodParamMetadataIR {
122 name: self.name.into_portable(registry),
123 ty: registry.register_type(&self.ty),
124 }
125 }
126}
127
128#[derive(Encode, Decode)]
130#[derive_where(Clone, PartialEq, Eq, Debug;)]
131pub struct PalletViewFunctionMetadataIR<T: Form = MetaForm> {
132 pub name: T::String,
134 pub id: [u8; 32],
136 pub inputs: Vec<PalletViewFunctionParamMetadataIR<T>>,
138 pub output: T::Type,
140 pub docs: Vec<T::String>,
142 pub deprecation_info: ItemDeprecationInfoIR<T>,
144}
145
146impl IntoPortable for PalletViewFunctionMetadataIR {
147 type Output = PalletViewFunctionMetadataIR<PortableForm>;
148
149 fn into_portable(self, registry: &mut Registry) -> Self::Output {
150 PalletViewFunctionMetadataIR {
151 name: self.name.into_portable(registry),
152 id: self.id,
153 inputs: registry.map_into_portable(self.inputs),
154 output: registry.register_type(&self.output),
155 docs: registry.map_into_portable(self.docs),
156 deprecation_info: self.deprecation_info.into_portable(registry),
157 }
158 }
159}
160
161#[derive(Encode, Decode)]
163#[derive_where(Clone, PartialEq, Eq, Debug;)]
164pub struct PalletViewFunctionParamMetadataIR<T: Form = MetaForm> {
165 pub name: T::String,
167 pub ty: T::Type,
169}
170
171impl IntoPortable for PalletViewFunctionParamMetadataIR {
172 type Output = PalletViewFunctionParamMetadataIR<PortableForm>;
173
174 fn into_portable(self, registry: &mut Registry) -> Self::Output {
175 PalletViewFunctionParamMetadataIR {
176 name: self.name.into_portable(registry),
177 ty: registry.register_type(&self.ty),
178 }
179 }
180}
181
182#[derive(Encode)]
184#[derive_where(Clone, PartialEq, Eq, Debug;)]
185pub struct PalletMetadataIR<T: Form = MetaForm> {
186 pub name: T::String,
188 pub storage: Option<PalletStorageMetadataIR<T>>,
190 pub calls: Option<PalletCallMetadataIR<T>>,
192 pub view_functions: Vec<PalletViewFunctionMetadataIR<T>>,
194 pub event: Option<PalletEventMetadataIR<T>>,
196 pub constants: Vec<PalletConstantMetadataIR<T>>,
198 pub error: Option<PalletErrorMetadataIR<T>>,
200 pub associated_types: Vec<PalletAssociatedTypeMetadataIR<T>>,
202 pub index: u8,
205 pub docs: Vec<T::String>,
207 pub deprecation_info: ItemDeprecationInfoIR<T>,
209}
210
211impl IntoPortable for PalletMetadataIR {
212 type Output = PalletMetadataIR<PortableForm>;
213
214 fn into_portable(self, registry: &mut Registry) -> Self::Output {
215 PalletMetadataIR {
216 name: self.name.into_portable(registry),
217 storage: self.storage.map(|storage| storage.into_portable(registry)),
218 calls: self.calls.map(|calls| calls.into_portable(registry)),
219 view_functions: self
220 .view_functions
221 .into_iter()
222 .map(|view_functions| view_functions.into_portable(registry))
223 .collect(),
224 event: self.event.map(|event| event.into_portable(registry)),
225 constants: registry.map_into_portable(self.constants),
226 error: self.error.map(|error| error.into_portable(registry)),
227 associated_types: registry.map_into_portable(self.associated_types),
228 index: self.index,
229 docs: registry.map_into_portable(self.docs),
230 deprecation_info: self.deprecation_info.into_portable(registry),
231 }
232 }
233}
234
235#[derive(Encode)]
237#[derive_where(Clone, PartialEq, Eq, Debug;)]
238pub struct ExtrinsicMetadataIR<T: Form = MetaForm> {
239 pub ty: T::Type,
243 pub versions: Vec<u8>,
245 pub address_ty: T::Type,
247 pub call_ty: T::Type,
249 pub signature_ty: T::Type,
251 pub extra_ty: T::Type,
254 pub extensions_by_version: BTreeMap<u8, Vec<u32>>,
257 pub extensions_in_versions: Vec<TransactionExtensionMetadataIR<T>>,
259}
260
261impl<T: Form> ExtrinsicMetadataIR<T> {
262 pub fn extensions_v0(&self) -> Option<Vec<TransactionExtensionMetadataIR<T>>> {
265 self.extensions_by_version.get(&0).map(|indices| {
266 indices
267 .iter()
268 .map(|i| self.extensions_in_versions[*i as usize].clone())
269 .collect()
270 })
271 }
272}
273
274impl IntoPortable for ExtrinsicMetadataIR {
275 type Output = ExtrinsicMetadataIR<PortableForm>;
276
277 fn into_portable(self, registry: &mut Registry) -> Self::Output {
278 ExtrinsicMetadataIR {
279 ty: registry.register_type(&self.ty),
280 versions: self.versions,
281 address_ty: registry.register_type(&self.address_ty),
282 call_ty: registry.register_type(&self.call_ty),
283 signature_ty: registry.register_type(&self.signature_ty),
284 extra_ty: registry.register_type(&self.extra_ty),
285 extensions_by_version: self.extensions_by_version,
286 extensions_in_versions: registry.map_into_portable(self.extensions_in_versions),
287 }
288 }
289}
290
291#[derive(Encode)]
293#[derive_where(Clone, PartialEq, Eq, Debug;)]
294pub struct PalletAssociatedTypeMetadataIR<T: Form = MetaForm> {
295 pub name: T::String,
297 pub ty: T::Type,
299 pub docs: Vec<T::String>,
301}
302
303impl IntoPortable for PalletAssociatedTypeMetadataIR {
304 type Output = PalletAssociatedTypeMetadataIR<PortableForm>;
305
306 fn into_portable(self, registry: &mut Registry) -> Self::Output {
307 PalletAssociatedTypeMetadataIR {
308 name: self.name.into_portable(registry),
309 ty: registry.register_type(&self.ty),
310 docs: registry.map_into_portable(self.docs),
311 }
312 }
313}
314
315#[derive(Encode)]
317#[derive_where(Clone, PartialEq, Eq, Debug;)]
318pub struct TransactionExtensionMetadataIR<T: Form = MetaForm> {
319 pub identifier: T::String,
321 pub ty: T::Type,
323 pub implicit: T::Type,
325}
326
327impl IntoPortable for TransactionExtensionMetadataIR {
328 type Output = TransactionExtensionMetadataIR<PortableForm>;
329
330 fn into_portable(self, registry: &mut Registry) -> Self::Output {
331 TransactionExtensionMetadataIR {
332 identifier: self.identifier.into_portable(registry),
333 ty: registry.register_type(&self.ty),
334 implicit: registry.register_type(&self.implicit),
335 }
336 }
337}
338
339#[derive(Encode)]
343#[derive_where(Clone, PartialEq, Eq, Debug;)]
344pub struct PalletStorageMetadataIR<T: Form = MetaForm> {
345 pub prefix: T::String,
347 pub entries: Vec<StorageEntryMetadataIR<T>>,
349}
350
351impl IntoPortable for PalletStorageMetadataIR {
352 type Output = PalletStorageMetadataIR<PortableForm>;
353
354 fn into_portable(self, registry: &mut Registry) -> Self::Output {
355 PalletStorageMetadataIR {
356 prefix: self.prefix.into_portable(registry),
357 entries: registry.map_into_portable(self.entries),
358 }
359 }
360}
361
362#[derive(Encode)]
364#[derive_where(Clone, PartialEq, Eq, Debug;)]
365pub struct StorageEntryMetadataIR<T: Form = MetaForm> {
366 pub name: T::String,
368 pub modifier: StorageEntryModifierIR,
370 pub ty: StorageEntryTypeIR<T>,
372 pub default: Vec<u8>,
374 pub docs: Vec<T::String>,
376 pub deprecation_info: ItemDeprecationInfoIR<T>,
378}
379
380impl IntoPortable for StorageEntryMetadataIR {
381 type Output = StorageEntryMetadataIR<PortableForm>;
382
383 fn into_portable(self, registry: &mut Registry) -> Self::Output {
384 StorageEntryMetadataIR {
385 name: self.name.into_portable(registry),
386 modifier: self.modifier,
387 ty: self.ty.into_portable(registry),
388 default: self.default,
389 docs: registry.map_into_portable(self.docs),
390 deprecation_info: self.deprecation_info.into_portable(registry),
391 }
392 }
393}
394
395#[derive(Clone, PartialEq, Eq, Encode, Debug)]
403pub enum StorageEntryModifierIR {
404 Optional,
406 Default,
408}
409
410#[derive(Clone, PartialEq, Eq, Encode, Debug)]
412pub enum StorageHasherIR {
413 Blake2_128,
415 Blake2_256,
417 Blake2_128Concat,
419 Twox128,
421 Twox256,
423 Twox64Concat,
425 Identity,
427}
428
429#[derive(Encode)]
431#[derive_where(Clone, PartialEq, Eq, Debug;)]
432pub enum StorageEntryTypeIR<T: Form = MetaForm> {
433 Plain(T::Type),
435 Map {
437 hashers: Vec<StorageHasherIR>,
439 key: T::Type,
441 value: T::Type,
443 },
444}
445
446impl IntoPortable for StorageEntryTypeIR {
447 type Output = StorageEntryTypeIR<PortableForm>;
448
449 fn into_portable(self, registry: &mut Registry) -> Self::Output {
450 match self {
451 Self::Plain(plain) => StorageEntryTypeIR::Plain(registry.register_type(&plain)),
452 Self::Map { hashers, key, value } => StorageEntryTypeIR::Map {
453 hashers,
454 key: registry.register_type(&key),
455 value: registry.register_type(&value),
456 },
457 }
458 }
459}
460
461#[derive(Encode)]
463#[derive_where(Clone, PartialEq, Eq, Debug;)]
464pub struct PalletCallMetadataIR<T: Form = MetaForm> {
465 pub ty: T::Type,
467 pub deprecation_info: EnumDeprecationInfoIR<T>,
469}
470
471impl IntoPortable for PalletCallMetadataIR {
472 type Output = PalletCallMetadataIR<PortableForm>;
473
474 fn into_portable(self, registry: &mut Registry) -> Self::Output {
475 PalletCallMetadataIR {
476 ty: registry.register_type(&self.ty),
477 deprecation_info: self.deprecation_info.into_portable(registry),
478 }
479 }
480}
481
482#[derive(Encode)]
484#[derive_where(Clone, PartialEq, Eq, Debug;)]
485pub struct PalletEventMetadataIR<T: Form = MetaForm> {
486 pub ty: T::Type,
488 pub deprecation_info: EnumDeprecationInfoIR<T>,
490}
491
492impl IntoPortable for PalletEventMetadataIR {
493 type Output = PalletEventMetadataIR<PortableForm>;
494
495 fn into_portable(self, registry: &mut Registry) -> Self::Output {
496 PalletEventMetadataIR {
497 ty: registry.register_type(&self.ty),
498 deprecation_info: self.deprecation_info.into_portable(registry),
499 }
500 }
501}
502
503#[derive(Encode)]
505#[derive_where(Clone, PartialEq, Eq, Debug;)]
506pub struct PalletConstantMetadataIR<T: Form = MetaForm> {
507 pub name: T::String,
509 pub ty: T::Type,
511 pub value: Vec<u8>,
513 pub docs: Vec<T::String>,
515 pub deprecation_info: ItemDeprecationInfoIR<T>,
517}
518
519impl IntoPortable for PalletConstantMetadataIR {
520 type Output = PalletConstantMetadataIR<PortableForm>;
521
522 fn into_portable(self, registry: &mut Registry) -> Self::Output {
523 PalletConstantMetadataIR {
524 name: self.name.into_portable(registry),
525 ty: registry.register_type(&self.ty),
526 value: self.value,
527 docs: registry.map_into_portable(self.docs),
528 deprecation_info: self.deprecation_info.into_portable(registry),
529 }
530 }
531}
532
533#[derive(Encode)]
535#[derive_where(Clone, PartialEq, Eq, Debug;)]
536pub struct PalletErrorMetadataIR<T: Form = MetaForm> {
537 pub ty: T::Type,
539 pub deprecation_info: EnumDeprecationInfoIR<T>,
541}
542
543impl IntoPortable for PalletErrorMetadataIR {
544 type Output = PalletErrorMetadataIR<PortableForm>;
545
546 fn into_portable(self, registry: &mut Registry) -> Self::Output {
547 PalletErrorMetadataIR {
548 ty: registry.register_type(&self.ty),
549 deprecation_info: self.deprecation_info.into_portable(registry),
550 }
551 }
552}
553
554#[derive(Encode)]
556#[derive_where(Clone, PartialEq, Eq, Debug;)]
557pub struct OuterEnumsIR<T: Form = MetaForm> {
558 pub call_enum_ty: T::Type,
560 pub event_enum_ty: T::Type,
562 pub error_enum_ty: T::Type,
578}
579
580impl IntoPortable for OuterEnumsIR {
581 type Output = OuterEnumsIR<PortableForm>;
582
583 fn into_portable(self, registry: &mut Registry) -> Self::Output {
584 OuterEnumsIR {
585 call_enum_ty: registry.register_type(&self.call_enum_ty),
586 event_enum_ty: registry.register_type(&self.event_enum_ty),
587 error_enum_ty: registry.register_type(&self.error_enum_ty),
588 }
589 }
590}
591
592#[derive(Encode)]
594#[derive_where(Clone, PartialEq, Eq, Debug;)]
595pub enum ItemDeprecationInfoIR<T: Form = MetaForm> {
596 NotDeprecated,
598 DeprecatedWithoutNote,
600 Deprecated {
602 note: T::String,
604 since: Option<T::String>,
606 },
607}
608
609impl IntoPortable for ItemDeprecationInfoIR {
610 type Output = ItemDeprecationInfoIR<PortableForm>;
611
612 fn into_portable(self, registry: &mut Registry) -> Self::Output {
613 match self {
614 Self::NotDeprecated => ItemDeprecationInfoIR::NotDeprecated,
615 Self::DeprecatedWithoutNote => ItemDeprecationInfoIR::DeprecatedWithoutNote,
616 Self::Deprecated { note, since } => {
617 let note = note.into_portable(registry);
618 let since = since.map(|x| x.into_portable(registry));
619 ItemDeprecationInfoIR::Deprecated { note, since }
620 },
621 }
622 }
623}
624
625#[derive(Encode)]
628#[derive_where(Clone, PartialEq, Eq, Debug;)]
629pub struct EnumDeprecationInfoIR<T: Form = MetaForm>(pub BTreeMap<u8, VariantDeprecationInfoIR<T>>);
630
631impl<T: Form> EnumDeprecationInfoIR<T> {
632 pub fn nothing_deprecated() -> Self {
634 Self(BTreeMap::new())
635 }
636
637 pub fn has_deprecated_variants(&self) -> bool {
639 !self.0.is_empty()
640 }
641
642 pub fn is_variant_deprecated(&self, variant_index: u8) -> bool {
644 self.0.contains_key(&variant_index)
645 }
646}
647
648impl IntoPortable for EnumDeprecationInfoIR {
649 type Output = EnumDeprecationInfoIR<PortableForm>;
650
651 fn into_portable(self, registry: &mut Registry) -> Self::Output {
652 let entries = self.0.into_iter().map(|(k, entry)| (k, entry.into_portable(registry)));
653 EnumDeprecationInfoIR(entries.collect())
654 }
655}
656
657#[derive(Encode)]
659#[derive_where(Clone, PartialEq, Eq, Debug;)]
660pub enum VariantDeprecationInfoIR<T: Form = MetaForm> {
661 DeprecatedWithoutNote,
663 Deprecated {
665 note: T::String,
667 since: Option<T::String>,
669 },
670}
671
672impl<T: Form> Into<ItemDeprecationInfoIR<T>> for VariantDeprecationInfoIR<T> {
673 fn into(self) -> ItemDeprecationInfoIR<T> {
674 match self {
675 Self::Deprecated { note, since } => ItemDeprecationInfoIR::Deprecated { note, since },
676 Self::DeprecatedWithoutNote => ItemDeprecationInfoIR::DeprecatedWithoutNote,
677 }
678 }
679}
680
681impl IntoPortable for VariantDeprecationInfoIR {
682 type Output = VariantDeprecationInfoIR<PortableForm>;
683
684 fn into_portable(self, registry: &mut Registry) -> Self::Output {
685 match self {
686 Self::Deprecated { note, since } => {
687 let note = note.into_portable(registry);
688 let since = since.map(|x| x.into_portable(registry));
689 VariantDeprecationInfoIR::Deprecated { note, since }
690 },
691 Self::DeprecatedWithoutNote => VariantDeprecationInfoIR::DeprecatedWithoutNote,
692 }
693 }
694}