@@ -20,12 +20,15 @@ use alloc::{
20
20
use codec:: { Decode , Encode } ;
21
21
use frame_metadata:: {
22
22
v15:: {
23
- ExtrinsicMetadata , PalletConstantMetadata , RuntimeApiMethodMetadata ,
24
- RuntimeMetadataLastVersion , StorageEntryMetadata ,
23
+ CustomMetadata , ExtrinsicMetadata , OuterEnums , PalletConstantMetadata ,
24
+ RuntimeApiMethodMetadata , RuntimeMetadataLastVersion , StorageEntryMetadata ,
25
25
} ,
26
26
RuntimeMetadata , RuntimeMetadataPrefixed , META_RESERVED ,
27
27
} ;
28
- use scale_info:: { form:: PortableForm , PortableRegistry , Type , Variant } ;
28
+ use scale_info:: {
29
+ form:: { Form , PortableForm } ,
30
+ PortableRegistry , Type , Variant ,
31
+ } ;
29
32
use sp_storage:: StorageKey ;
30
33
31
34
#[ cfg( feature = "std" ) ]
@@ -39,8 +42,6 @@ pub struct Metadata {
39
42
pallets : BTreeMap < String , PalletMetadataInner > ,
40
43
/// Find the location in the pallet Vec by pallet index.
41
44
pallets_by_index : BTreeMap < u8 , String > ,
42
- /// Metadata of the extrinsic.
43
- extrinsic : ExtrinsicMetadata < PortableForm > ,
44
45
// Type of the DispatchError type, which is what comes back if
45
46
// an extrinsic fails.
46
47
dispatch_error_ty : Option < u32 > ,
@@ -67,6 +68,11 @@ impl Metadata {
67
68
Some ( PalletMetadata { inner, types : self . types ( ) } )
68
69
}
69
70
71
+ /// Return the type of the `Runtime`.
72
+ pub fn ty ( & self ) -> & <PortableForm as Form >:: Type {
73
+ & self . runtime_metadata . ty
74
+ }
75
+
70
76
/// Return the DispatchError type ID if it exists.
71
77
pub fn dispatch_error_ty ( & self ) -> Option < u32 > {
72
78
self . dispatch_error_ty
@@ -89,7 +95,7 @@ impl Metadata {
89
95
90
96
/// Return details about the extrinsic format.
91
97
pub fn extrinsic ( & self ) -> & ExtrinsicMetadata < PortableForm > {
92
- & self . extrinsic
98
+ & self . runtime_metadata . extrinsic
93
99
}
94
100
95
101
/// An iterator over all of the runtime APIs.
@@ -105,6 +111,16 @@ impl Metadata {
105
111
Some ( RuntimeApiMetadata { inner, types : self . types ( ) } )
106
112
}
107
113
114
+ /// Return the outer enums types as found in the runtime.
115
+ pub fn outer_enums ( & self ) -> & OuterEnums < PortableForm > {
116
+ & self . runtime_metadata . outer_enums
117
+ }
118
+
119
+ /// Returns the custom types of the metadata.
120
+ pub fn custom ( & self ) -> & CustomMetadata < PortableForm > {
121
+ & self . runtime_metadata . custom
122
+ }
123
+
108
124
#[ cfg( feature = "std" ) ]
109
125
pub fn pretty_format ( & self ) -> Result < String , std:: string:: FromUtf8Error > {
110
126
let buf = Vec :: new ( ) ;
@@ -249,13 +265,11 @@ impl<'a> PalletMetadata<'a> {
249
265
}
250
266
}
251
267
252
- // Based on https://github.com/paritytech/subxt /blob/8413c4d2dd625335b9200dc2289670accdf3391a/ metadata/src/lib .rs#L274-L298
268
+ // Based on https://github.com/paritytech/frame-metadata /blob/94e7743fa454963609763cf9cccbb7f85bc96d2f/frame- metadata/src/v15 .rs#L249-L276
253
269
#[ derive( Debug , Clone ) ]
254
270
struct PalletMetadataInner {
255
271
/// Pallet name.
256
272
name : String ,
257
- /// Pallet index.
258
- index : u8 ,
259
273
/// Pallet storage metadata.
260
274
storage : BTreeMap < String , StorageEntryMetadata < PortableForm > > ,
261
275
/// Type ID for the pallet Call enum.
@@ -266,12 +280,15 @@ struct PalletMetadataInner {
266
280
event_ty : Option < u32 > ,
267
281
/// Event variants by name/u8.
268
282
event_variant_index : VariantIndex ,
283
+ /// Map from constant name to constant details.
284
+ constants : BTreeMap < String , PalletConstantMetadata < PortableForm > > ,
269
285
/// Type ID for the pallet Error enum.
270
286
error_ty : Option < u32 > ,
271
287
/// Error variants by name/u8.
272
288
error_variant_index : VariantIndex ,
273
- /// Map from constant name to constant details.
274
- constants : BTreeMap < String , PalletConstantMetadata < PortableForm > > ,
289
+ /// Define the index of the pallet, this index will be used for the encoding of pallet event,
290
+ /// call and origin variants.
291
+ index : u8 ,
275
292
/// Pallet documentation.
276
293
docs : Vec < String > ,
277
294
}
@@ -408,7 +425,6 @@ impl TryFrom<RuntimeMetadataPrefixed> for Metadata {
408
425
runtime_metadata : m. clone ( ) ,
409
426
pallets,
410
427
pallets_by_index,
411
- extrinsic : m. extrinsic ,
412
428
dispatch_error_ty,
413
429
apis,
414
430
} )
@@ -499,3 +515,39 @@ impl Metadata {
499
515
. key ( first_double_map_key, second_double_map_key) )
500
516
}
501
517
}
518
+
519
+ #[ cfg( test) ]
520
+ mod tests {
521
+ use super :: * ;
522
+ use codec:: Decode ;
523
+ use scale_info:: TypeDef ;
524
+ use sp_core:: Bytes ;
525
+ use std:: fs;
526
+
527
+ fn metadata ( ) -> Metadata {
528
+ let encoded_metadata: Bytes = fs:: read ( "./../ksm_metadata_v14.bin" ) . unwrap ( ) . into ( ) ;
529
+ Decode :: decode ( & mut encoded_metadata. 0 . as_slice ( ) ) . unwrap ( )
530
+ }
531
+
532
+ #[ test]
533
+ fn outer_enum_access ( ) {
534
+ let metadata = metadata ( ) ;
535
+
536
+ let call_enum_ty = metadata. outer_enums ( ) . call_enum_ty ;
537
+ let ty = metadata. types ( ) . types . get ( call_enum_ty. id as usize ) . unwrap ( ) ;
538
+ if let TypeDef :: Variant ( variant) = & ty. ty . type_def {
539
+ // The first pallet call is from System pallet.
540
+ assert_eq ! ( variant. variants[ 0 ] . name, "System" ) ;
541
+ } else {
542
+ panic ! ( "Expected Variant outer enum call type." ) ;
543
+ }
544
+ }
545
+
546
+ #[ test]
547
+ fn custom_ksm_metadata_v14_is_empty ( ) {
548
+ let metadata = metadata ( ) ;
549
+ let custom_metadata = metadata. custom ( ) ;
550
+
551
+ assert ! ( custom_metadata. map. is_empty( ) ) ;
552
+ }
553
+ }
0 commit comments