github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV13.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "hash" 6 "strings" 7 8 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 9 "github.com/stafiprotocol/go-substrate-rpc-client/xxhash" 10 ) 11 12 type MetadataV13 struct { 13 Modules []ModuleMetadataV13 14 Extrinsic ExtrinsicV11 15 } 16 17 func (m *MetadataV13) Decode(decoder scale.Decoder) error { 18 err := decoder.Decode(&m.Modules) 19 if err != nil { 20 return err 21 } 22 return decoder.Decode(&m.Extrinsic) 23 } 24 25 func (m MetadataV13) Encode(encoder scale.Encoder) error { 26 err := encoder.Encode(m.Modules) 27 if err != nil { 28 return err 29 } 30 return encoder.Encode(m.Extrinsic) 31 } 32 33 func (m *MetadataV13) FindCallIndex(call string) (CallIndex, error) { 34 s := strings.Split(call, ".") 35 for _, mod := range m.Modules { 36 if !mod.HasCalls { 37 continue 38 } 39 if string(mod.Name) != s[0] { 40 continue 41 } 42 for ci, f := range mod.Calls { 43 if string(f.Name) == s[1] { 44 return CallIndex{mod.Index, uint8(ci)}, nil 45 } 46 } 47 return CallIndex{}, fmt.Errorf("method %v not found within module %v for call %v", s[1], mod.Name, call) 48 } 49 return CallIndex{}, fmt.Errorf("module %v not found in metadata for call %v", s[0], call) 50 } 51 52 func (m *MetadataV13) FindEventNamesForEventID(eventID EventID) (Text, Text, error) { 53 for _, mod := range m.Modules { 54 if !mod.HasEvents { 55 continue 56 } 57 if mod.Index != eventID[0] { 58 continue 59 } 60 if int(eventID[1]) >= len(mod.Events) { 61 return "", "", fmt.Errorf("event index %v for module %v out of range", eventID[1], mod.Name) 62 } 63 return mod.Name, mod.Events[eventID[1]].Name, nil 64 } 65 return "", "", fmt.Errorf("module index %v out of range", eventID[0]) 66 } 67 68 func (m *MetadataV13) FindStorageEntryMetadata(module string, fn string) (StorageEntryMetadata, error) { 69 for _, mod := range m.Modules { 70 if !mod.HasStorage { 71 continue 72 } 73 if string(mod.Storage.Prefix) != module { 74 continue 75 } 76 for _, s := range mod.Storage.Items { 77 if string(s.Name) != fn { 78 continue 79 } 80 return s, nil 81 } 82 return nil, fmt.Errorf("storage %v not found within module %v", fn, module) 83 } 84 return nil, fmt.Errorf("module %v not found in metadata", module) 85 } 86 87 func (m MetadataV13) GetConst(prefix, name string, res interface{}) error { 88 for _, mod := range m.Modules { 89 if string(mod.Name) == prefix { 90 for _, cons := range mod.Constants { 91 if string(cons.Name) == name { 92 return DecodeFromBytes(cons.Value, res) 93 } 94 } 95 } 96 } 97 return fmt.Errorf("could not find constant %s.%s", prefix, name) 98 } 99 100 func (m *MetadataV13) FindConstantValue(module Text, constant Text) ([]byte, error) { 101 for _, mod := range m.Modules { 102 if mod.Name == module { 103 for _, cons := range mod.Constants { 104 if cons.Name == constant { 105 return cons.Value, nil 106 } 107 } 108 } 109 } 110 return nil, fmt.Errorf("could not find constant %s.%s", module, constant) 111 } 112 113 func (m *MetadataV13) ExistsModuleMetadata(module string) bool { 114 for _, mod := range m.Modules { 115 if string(mod.Name) == module { 116 return true 117 } 118 } 119 return false 120 } 121 122 type ModuleMetadataV13 struct { 123 Name Text 124 HasStorage bool 125 Storage StorageMetadataV13 126 HasCalls bool 127 Calls []FunctionMetadataV4 128 HasEvents bool 129 Events []EventMetadataV4 130 Constants []ModuleConstantMetadataV6 131 Errors []ErrorMetadataV8 132 Index uint8 133 } 134 135 func (m *ModuleMetadataV13) Decode(decoder scale.Decoder) error { 136 err := decoder.Decode(&m.Name) 137 if err != nil { 138 return err 139 } 140 141 err = decoder.Decode(&m.HasStorage) 142 if err != nil { 143 return err 144 } 145 146 if m.HasStorage { 147 err = decoder.Decode(&m.Storage) 148 if err != nil { 149 return err 150 } 151 } 152 153 err = decoder.Decode(&m.HasCalls) 154 if err != nil { 155 return err 156 } 157 158 if m.HasCalls { 159 err = decoder.Decode(&m.Calls) 160 if err != nil { 161 return err 162 } 163 } 164 165 err = decoder.Decode(&m.HasEvents) 166 if err != nil { 167 return err 168 } 169 170 if m.HasEvents { 171 err = decoder.Decode(&m.Events) 172 if err != nil { 173 return err 174 } 175 } 176 177 err = decoder.Decode(&m.Constants) 178 if err != nil { 179 return err 180 } 181 182 err = decoder.Decode(&m.Errors) 183 if err != nil { 184 return err 185 } 186 187 return decoder.Decode(&m.Index) 188 } 189 190 func (m ModuleMetadataV13) Encode(encoder scale.Encoder) error { 191 err := encoder.Encode(m.Name) 192 if err != nil { 193 return err 194 } 195 196 err = encoder.Encode(m.HasStorage) 197 if err != nil { 198 return err 199 } 200 201 if m.HasStorage { 202 err = encoder.Encode(m.Storage) 203 if err != nil { 204 return err 205 } 206 } 207 208 err = encoder.Encode(m.HasCalls) 209 if err != nil { 210 return err 211 } 212 213 if m.HasCalls { 214 err = encoder.Encode(m.Calls) 215 if err != nil { 216 return err 217 } 218 } 219 220 err = encoder.Encode(m.HasEvents) 221 if err != nil { 222 return err 223 } 224 225 if m.HasEvents { 226 err = encoder.Encode(m.Events) 227 if err != nil { 228 return err 229 } 230 } 231 232 err = encoder.Encode(m.Constants) 233 if err != nil { 234 return err 235 } 236 237 err = encoder.Encode(m.Errors) 238 if err != nil { 239 return err 240 } 241 242 return encoder.Encode(m.Index) 243 } 244 245 type StorageMetadataV13 struct { 246 Prefix Text 247 Items []StorageFunctionMetadataV13 248 } 249 250 type StorageFunctionMetadataV13 struct { 251 Name Text 252 Modifier StorageFunctionModifierV0 253 Type StorageFunctionTypeV13 254 Fallback Bytes 255 Documentation []Text 256 } 257 258 func (s StorageFunctionMetadataV13) IsPlain() bool { 259 return s.Type.IsType 260 } 261 262 func (s StorageFunctionMetadataV13) IsMap() bool { 263 return s.Type.IsMap 264 } 265 266 func (s StorageFunctionMetadataV13) IsDoubleMap() bool { 267 return s.Type.IsDoubleMap 268 } 269 270 func (s StorageFunctionMetadataV13) IsNMap() bool { 271 return s.Type.IsNMap 272 } 273 274 func (s StorageFunctionMetadataV13) Hasher() (hash.Hash, error) { 275 if s.Type.IsMap { 276 return s.Type.AsMap.Hasher.HashFunc() 277 } 278 if s.Type.IsDoubleMap { 279 return s.Type.AsDoubleMap.Hasher.HashFunc() 280 } 281 if s.Type.IsNMap { 282 return nil, fmt.Errorf("only Map and DoubleMap have a Hasher") 283 } 284 return xxhash.New128(nil), nil 285 } 286 287 func (s StorageFunctionMetadataV13) Hasher2() (hash.Hash, error) { 288 if !s.Type.IsDoubleMap { 289 return nil, fmt.Errorf("only DoubleMaps have a Hasher2") 290 } 291 return s.Type.AsDoubleMap.Key2Hasher.HashFunc() 292 } 293 294 func (s StorageFunctionMetadataV13) Hashers() ([]hash.Hash, error) { 295 if !s.Type.IsNMap { 296 return nil, fmt.Errorf("only NMaps have Hashers") 297 } 298 299 var hashers []hash.Hash 300 if s.Type.IsMap { 301 hashers = make([]hash.Hash, 1) 302 mapHasher, err := s.Type.AsMap.Hasher.HashFunc() 303 if err != nil { 304 return nil, err 305 } 306 hashers[0] = mapHasher 307 return hashers, nil 308 } 309 if s.Type.IsDoubleMap { 310 hashers = make([]hash.Hash, 2) 311 if firstDoubleMapHasher, err := s.Type.AsDoubleMap.Hasher.HashFunc(); err != nil { 312 return nil, err 313 } else { 314 hashers[0] = firstDoubleMapHasher 315 } 316 secondDoubleMapHasher, err := s.Type.AsDoubleMap.Key2Hasher.HashFunc() 317 if err != nil { 318 return nil, err 319 } 320 hashers[1] = secondDoubleMapHasher 321 return hashers, nil 322 } 323 if s.Type.IsNMap { 324 hashers = make([]hash.Hash, len(s.Type.AsNMap.Hashers)) 325 for i, hasher := range s.Type.AsNMap.Hashers { 326 hasherFn, err := hasher.HashFunc() 327 if err != nil { 328 return nil, err 329 } 330 hashers[i] = hasherFn 331 } 332 return hashers, nil 333 } 334 335 hashers = make([]hash.Hash, 1) 336 hashers[0] = xxhash.New128(nil) 337 338 return hashers, nil 339 } 340 341 type StorageFunctionTypeV13 struct { 342 IsType bool 343 AsType Type // 0 344 IsMap bool 345 AsMap MapTypeV10 // 1 346 IsDoubleMap bool 347 AsDoubleMap DoubleMapTypeV10 // 2 348 IsNMap bool 349 AsNMap NMapTypeV13 // 3 350 } 351 352 func (s *StorageFunctionTypeV13) Decode(decoder scale.Decoder) error { 353 var t uint8 354 err := decoder.Decode(&t) 355 if err != nil { 356 return err 357 } 358 359 switch t { 360 case 0: 361 s.IsType = true 362 err = decoder.Decode(&s.AsType) 363 if err != nil { 364 return err 365 } 366 case 1: 367 s.IsMap = true 368 err = decoder.Decode(&s.AsMap) 369 if err != nil { 370 return err 371 } 372 case 2: 373 s.IsDoubleMap = true 374 err = decoder.Decode(&s.AsDoubleMap) 375 if err != nil { 376 return err 377 } 378 case 3: 379 s.IsNMap = true 380 err = decoder.Decode(&s.AsNMap) 381 if err != nil { 382 return err 383 } 384 default: 385 return fmt.Errorf("received unexpected type %v", t) 386 } 387 return nil 388 } 389 390 func (s StorageFunctionTypeV13) Encode(encoder scale.Encoder) error { 391 switch { 392 case s.IsType: 393 err := encoder.PushByte(0) 394 if err != nil { 395 return err 396 } 397 err = encoder.Encode(s.AsType) 398 if err != nil { 399 return err 400 } 401 case s.IsMap: 402 err := encoder.PushByte(1) 403 if err != nil { 404 return err 405 } 406 err = encoder.Encode(s.AsMap) 407 if err != nil { 408 return err 409 } 410 case s.IsDoubleMap: 411 err := encoder.PushByte(2) 412 if err != nil { 413 return err 414 } 415 err = encoder.Encode(s.AsDoubleMap) 416 if err != nil { 417 return err 418 } 419 case s.IsNMap: 420 err := encoder.PushByte(3) 421 if err != nil { 422 return err 423 } 424 err = encoder.Encode(s.AsNMap) 425 if err != nil { 426 return err 427 } 428 default: 429 return fmt.Errorf("expected to be either type, map, double map or nmap but none was set: %v", s) 430 } 431 return nil 432 } 433 434 type NMapTypeV13 struct { 435 Keys []Type 436 Hashers []StorageHasherV10 437 Value Type 438 }