github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV8.go (about) 1 // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls 2 // 3 // Copyright 2020 Stafi Protocol 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package types 18 19 import ( 20 "fmt" 21 "strings" 22 23 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/scale" 24 ) 25 26 // Modelled after packages/types/src/Metadata/v8/Metadata.ts 27 type MetadataV8 struct { 28 Modules []ModuleMetadataV8 29 } 30 31 func (m *MetadataV8) Decode(decoder scale.Decoder) error { 32 err := decoder.Decode(&m.Modules) 33 if err != nil { 34 return err 35 } 36 return nil 37 } 38 39 func (m MetadataV8) Encode(encoder scale.Encoder) error { 40 err := encoder.Encode(m.Modules) 41 if err != nil { 42 return err 43 } 44 return nil 45 } 46 47 func (m *MetadataV8) FindCallIndex(call string) (CallIndex, error) { 48 s := strings.Split(call, ".") 49 mi := uint8(0) 50 for _, mod := range m.Modules { 51 if !mod.HasCalls { 52 continue 53 } 54 if string(mod.Name) != s[0] { 55 mi++ 56 continue 57 } 58 for ci, f := range mod.Calls { 59 if string(f.Name) == s[1] { 60 return CallIndex{mi, uint8(ci)}, nil 61 } 62 } 63 return CallIndex{}, fmt.Errorf("method %v not found within module %v for call %v", s[1], mod.Name, call) 64 } 65 return CallIndex{}, fmt.Errorf("module %v not found in metadata for call %v", s[0], call) 66 } 67 68 func (m *MetadataV8) FindEventNamesForEventID(eventID EventID) (Text, Text, error) { 69 mi := uint8(0) 70 for _, mod := range m.Modules { 71 if !mod.HasEvents { 72 continue 73 } 74 if mi != eventID[0] { 75 mi++ 76 continue 77 } 78 if int(eventID[1]) >= len(mod.Events) { 79 return "", "", fmt.Errorf("event index %v for module %v out of range", eventID[1], mod.Name) 80 } 81 return mod.Name, mod.Events[eventID[1]].Name, nil 82 } 83 return "", "", fmt.Errorf("module index %v out of range", eventID[0]) 84 } 85 86 func (m *MetadataV8) 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 *MetadataV8) FindStorageEntryMetadata(module string, fn string) (StorageEntryMetadata, error) { 100 for _, mod := range m.Modules { 101 if !mod.HasStorage { 102 continue 103 } 104 if string(mod.Storage.Prefix) != module { 105 continue 106 } 107 for _, s := range mod.Storage.Items { 108 if string(s.Name) != fn { 109 continue 110 } 111 return s, nil 112 } 113 return nil, fmt.Errorf("storage %v not found within module %v", fn, module) 114 } 115 return nil, fmt.Errorf("module %v not found in metadata", module) 116 } 117 118 func (m *MetadataV8) ExistsModuleMetadata(module string) bool { 119 for _, mod := range m.Modules { 120 if string(mod.Name) == module { 121 return true 122 } 123 } 124 return false 125 } 126 127 type ModuleMetadataV8 struct { 128 Name Text 129 HasStorage bool 130 Storage StorageMetadata 131 HasCalls bool 132 Calls []FunctionMetadataV4 133 HasEvents bool 134 Events []EventMetadataV4 135 Constants []ModuleConstantMetadataV6 136 Errors []ErrorMetadataV8 137 } 138 139 func (m *ModuleMetadataV8) Decode(decoder scale.Decoder) error { 140 err := decoder.Decode(&m.Name) 141 if err != nil { 142 return err 143 } 144 145 err = decoder.Decode(&m.HasStorage) 146 if err != nil { 147 return err 148 } 149 150 if m.HasStorage { 151 err = decoder.Decode(&m.Storage) 152 if err != nil { 153 return err 154 } 155 } 156 157 err = decoder.Decode(&m.HasCalls) 158 if err != nil { 159 return err 160 } 161 162 if m.HasCalls { 163 err = decoder.Decode(&m.Calls) 164 if err != nil { 165 return err 166 } 167 } 168 169 err = decoder.Decode(&m.HasEvents) 170 if err != nil { 171 return err 172 } 173 174 if m.HasEvents { 175 err = decoder.Decode(&m.Events) 176 if err != nil { 177 return err 178 } 179 } 180 181 err = decoder.Decode(&m.Constants) 182 if err != nil { 183 return err 184 } 185 186 return decoder.Decode(&m.Errors) 187 } 188 189 func (m ModuleMetadataV8) 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 return encoder.Encode(m.Errors) 237 } 238 239 type ErrorMetadataV8 struct { 240 Name Text 241 Documentation []Text 242 }