github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV9.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/v9/Metadata.ts
    27  type MetadataV9 struct {
    28  	Modules []ModuleMetadataV8
    29  }
    30  
    31  func (m *MetadataV9) 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 MetadataV9) 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 *MetadataV9) 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 *MetadataV9) 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 *MetadataV9) 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 *MetadataV9) 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 *MetadataV9) 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  }