github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV12.go (about) 1 package types 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 8 ) 9 10 // Modelled after packages/types/src/Metadata/v11/toV12.ts 11 type MetadataV12 struct { 12 Modules []ModuleMetadataV12 13 Extrinsic ExtrinsicV11 14 } 15 16 func (m *MetadataV12) Decode(decoder scale.Decoder) error { 17 err := decoder.Decode(&m.Modules) 18 if err != nil { 19 return err 20 } 21 return decoder.Decode(&m.Extrinsic) 22 } 23 24 func (m MetadataV12) Encode(encoder scale.Encoder) error { 25 err := encoder.Encode(m.Modules) 26 if err != nil { 27 return err 28 } 29 return encoder.Encode(m.Extrinsic) 30 } 31 32 func (m *MetadataV12) FindCallIndex(call string) (CallIndex, error) { 33 s := strings.Split(call, ".") 34 for _, mod := range m.Modules { 35 if !mod.HasCalls { 36 continue 37 } 38 if string(mod.Name) != s[0] { 39 continue 40 } 41 for ci, f := range mod.Calls { 42 if string(f.Name) == s[1] { 43 return CallIndex{mod.Index, uint8(ci)}, nil 44 } 45 } 46 return CallIndex{}, fmt.Errorf("method %v not found within module %v for call %v", s[1], mod.Name, call) 47 } 48 return CallIndex{}, fmt.Errorf("module %v not found in metadata for call %v", s[0], call) 49 } 50 51 func (m *MetadataV12) FindEventNamesForEventID(eventID EventID) (Text, Text, error) { 52 for _, mod := range m.Modules { 53 if !mod.HasEvents { 54 continue 55 } 56 if mod.Index != eventID[0] { 57 continue 58 } 59 if int(eventID[1]) >= len(mod.Events) { 60 return "", "", fmt.Errorf("event index %v for module %v out of range", eventID[1], mod.Name) 61 } 62 return mod.Name, mod.Events[eventID[1]].Name, nil 63 } 64 return "", "", fmt.Errorf("module index %v out of range", eventID[0]) 65 } 66 67 func (m *MetadataV12) FindStorageEntryMetadata(module string, fn string) (StorageEntryMetadata, error) { 68 for _, mod := range m.Modules { 69 if !mod.HasStorage { 70 continue 71 } 72 if string(mod.Storage.Prefix) != module { 73 continue 74 } 75 for _, s := range mod.Storage.Items { 76 if string(s.Name) != fn { 77 continue 78 } 79 return s, nil 80 } 81 return nil, fmt.Errorf("storage %v not found within module %v", fn, module) 82 } 83 return nil, fmt.Errorf("module %v not found in metadata", module) 84 } 85 86 func (m *MetadataV12) FindConstantValue(module Text, constant Text) ([]byte, error) { 87 for _, mod := range m.Modules { 88 if mod.Name == module { 89 for _, cons := range mod.Constants { 90 if cons.Name == constant { 91 return cons.Value, nil 92 } 93 } 94 } 95 } 96 return nil, fmt.Errorf("could not find constant %s.%s", module, constant) 97 } 98 99 func (m *MetadataV12) ExistsModuleMetadata(module string) bool { 100 for _, mod := range m.Modules { 101 if string(mod.Name) == module { 102 return true 103 } 104 } 105 return false 106 } 107 108 func (m MetadataV12) GetConst(prefix, name string, res interface{}) error { 109 for _, mod := range m.Modules { 110 if string(mod.Name) == prefix { 111 for _, cons := range mod.Constants { 112 if string(cons.Name) == name { 113 return DecodeFromBytes(cons.Value, res) 114 } 115 } 116 } 117 } 118 return fmt.Errorf("could not find constant %s.%s", prefix, name) 119 } 120 121 type ModuleMetadataV12 struct { 122 Name Text 123 HasStorage bool 124 Storage StorageMetadataV10 125 HasCalls bool 126 Calls []FunctionMetadataV4 127 HasEvents bool 128 Events []EventMetadataV4 129 Constants []ModuleConstantMetadataV6 130 Errors []ErrorMetadataV8 131 Index uint8 132 } 133 134 func (m *ModuleMetadataV12) Decode(decoder scale.Decoder) error { 135 err := decoder.Decode(&m.Name) 136 if err != nil { 137 return err 138 } 139 140 err = decoder.Decode(&m.HasStorage) 141 if err != nil { 142 return err 143 } 144 145 if m.HasStorage { 146 err = decoder.Decode(&m.Storage) 147 if err != nil { 148 return err 149 } 150 } 151 152 err = decoder.Decode(&m.HasCalls) 153 if err != nil { 154 return err 155 } 156 157 if m.HasCalls { 158 err = decoder.Decode(&m.Calls) 159 if err != nil { 160 return err 161 } 162 } 163 164 err = decoder.Decode(&m.HasEvents) 165 if err != nil { 166 return err 167 } 168 169 if m.HasEvents { 170 err = decoder.Decode(&m.Events) 171 if err != nil { 172 return err 173 } 174 } 175 176 err = decoder.Decode(&m.Constants) 177 if err != nil { 178 return err 179 } 180 181 err = decoder.Decode(&m.Errors) 182 if err != nil { 183 return err 184 } 185 186 return decoder.Decode(&m.Index) 187 } 188 189 func (m ModuleMetadataV12) Encode(encoder scale.Encoder) error { 190 err := encoder.Encode(m.Name) 191 if err != nil { 192 return err 193 } 194 195 err = encoder.Encode(m.HasStorage) 196 if err != nil { 197 return err 198 } 199 200 if m.HasStorage { 201 err = encoder.Encode(m.Storage) 202 if err != nil { 203 return err 204 } 205 } 206 207 err = encoder.Encode(m.HasCalls) 208 if err != nil { 209 return err 210 } 211 212 if m.HasCalls { 213 err = encoder.Encode(m.Calls) 214 if err != nil { 215 return err 216 } 217 } 218 219 err = encoder.Encode(m.HasEvents) 220 if err != nil { 221 return err 222 } 223 224 if m.HasEvents { 225 err = encoder.Encode(m.Events) 226 if err != nil { 227 return err 228 } 229 } 230 231 err = encoder.Encode(m.Constants) 232 if err != nil { 233 return err 234 } 235 236 err = encoder.Encode(m.Errors) 237 if err != nil { 238 return err 239 } 240 241 return encoder.Encode(m.Index) 242 }