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
use crate::{ExtrinsicInfo, ModuleMetadataExt, Result}; #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct MetadataV13 { pub modules: Vec<ModuleMetadata>, pub extrinsics: ExtrinsicMetadata, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct ModuleMetadata { pub name: String, pub storage: Option<StorageMetadata>, pub calls: Option<Vec<FunctionMetadata>>, pub events: Option<Vec<EventMetadata>>, pub constants: Vec<ModuleConstantMetadata>, pub errors: Vec<ErrorMetadata>, pub index: u8, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct StorageMetadata { pub prefix: String, pub entries: Vec<StorageEntryMetadata>, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct StorageEntryMetadata { pub name: String, pub modifier: StorageEntryModifier, pub ty: StorageEntryType, pub default: Vec<u8>, pub documentation: Vec<String>, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub enum StorageEntryModifier { Optional, Default, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub enum StorageEntryType { Plain(String), Map { hasher: StorageHasher, key: String, value: String, unused: bool, }, DoubleMap { hasher: StorageHasher, key1: String, key2: String, value: String, key2_hasher: StorageHasher, }, NMap { keys: String, hashers: Vec<StorageHasher>, value: String, }, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub enum StorageHasher { Blake2_128, Blake2_256, Blake2_128Concat, Twox128, Twox256, Twox64Concat, Identity, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct FunctionMetadata { pub name: String, pub arguments: Vec<FunctionArgumentMetadata>, pub documentation: Vec<String>, } impl FunctionMetadata { pub fn to_extrinsic_info<'a>( &'a self, module_id: usize, dispatch_id: usize, module_name: &'a str, ) -> ExtrinsicInfo<'a> { ExtrinsicInfo { module_id: module_id, dispatch_id: dispatch_id, module_name: module_name, extrinsic_name: self.name.as_str(), args: self .arguments .iter() .map(|arg_meta| (arg_meta.name.as_str(), arg_meta.ty.as_str())) .collect(), documentation: self.documentation.iter().map(|s| s.as_str()).collect(), } } } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct FunctionArgumentMetadata { pub name: String, pub ty: String, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct EventMetadata { pub name: String, pub arguments: Vec<String>, pub documentation: Vec<String>, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct ModuleConstantMetadata { pub name: String, pub ty: String, pub value: Vec<u8>, pub documentation: Vec<String>, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct ErrorMetadata { pub name: String, pub documentation: Vec<String>, } #[derive(Debug, Clone, PartialEq, Encode, Decode)] pub struct ExtrinsicMetadata { pub version: u8, pub signed_extensions: Vec<String>, } impl ModuleMetadataExt for MetadataV13 { fn modules_extrinsics<'a>(&'a self) -> Vec<ExtrinsicInfo<'a>> { self.modules .iter() .enumerate() .map(|(module_id, mod_meta)| { mod_meta .calls .as_ref() .map(|funcs_meta| { funcs_meta .iter() .enumerate() .map(|(dispatch_id, func_meta)| { func_meta.to_extrinsic_info( module_id, dispatch_id, mod_meta.name.as_str(), ) }) .collect() }) .unwrap_or(vec![]) }) .flatten() .collect() } fn find_module_extrinsic<'a>( &'a self, method: &str, extrinsic: &str, ) -> Result<Option<ExtrinsicInfo<'a>>> { Ok(self .modules .iter() .enumerate() .find(|(_, mod_meta)| mod_meta.name.as_str() == method) .map(|(module_id, mod_meta)| { mod_meta.calls.as_ref().map(|funcs_meta| { funcs_meta .iter() .enumerate() .find(|(_, func_meta)| func_meta.name.as_str() == extrinsic) .map(|(dispatch_id, func_meta)| { func_meta.to_extrinsic_info( module_id, dispatch_id, mod_meta.name.as_str(), ) }) }) }) .and_then(|res| res?)) } }