github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/v12.go (about)

     1  package stafi_decoder
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/huandu/xstrings"
     7  	"github.com/itering/scale.go/utiles"
     8  )
     9  
    10  type MetadataV12Decoder struct {
    11  	ScaleDecoder
    12  }
    13  
    14  type ExtrinsicMetadata struct {
    15  	SignedExtensions []string `json:"signedExtensions"`
    16  }
    17  
    18  func (m *MetadataV12Decoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
    19  	m.ScaleDecoder.Init(data, option)
    20  }
    21  
    22  func (m *MetadataV12Decoder) Process() {
    23  	// var callModuleIndex, eventModuleIndex int
    24  
    25  	result := MetadataStruct{
    26  		Metadata: MetadataTag{
    27  			Modules: nil,
    28  		},
    29  	}
    30  	MetadataV11ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV12Module>").([]interface{})
    31  	bm, _ := json.Marshal(MetadataV11ModuleCall)
    32  
    33  	var modulesType []MetadataModules
    34  	_ = json.Unmarshal(bm, &modulesType)
    35  	result.CallIndex = make(map[string]CallIndex)
    36  	result.EventIndex = make(map[string]EventIndex)
    37  	for k, module := range modulesType {
    38  		if module.Calls != nil {
    39  			for callIndex, call := range module.Calls {
    40  				modulesType[k].Calls[callIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(module.Index), 2, "0") + xstrings.RightJustify(utiles.IntToHex(callIndex), 2, "0")
    41  				result.CallIndex[modulesType[k].Calls[callIndex].Lookup] = CallIndex{Module: module, Call: call}
    42  			}
    43  		}
    44  		if module.Events != nil {
    45  			for eventIndex, event := range module.Events {
    46  				modulesType[k].Events[eventIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(module.Index), 2, "0") + xstrings.RightJustify(utiles.IntToHex(eventIndex), 2, "0")
    47  				result.EventIndex[modulesType[k].Events[eventIndex].Lookup] = EventIndex{Module: module, Call: event}
    48  			}
    49  		}
    50  	}
    51  
    52  	result.Metadata.Modules = modulesType
    53  	extrinsicMetadata := m.ProcessAndUpdateData("ExtrinsicMetadata").(map[string]interface{})
    54  	bm, _ = json.Marshal(extrinsicMetadata)
    55  	_ = json.Unmarshal(bm, &result.Extrinsic)
    56  
    57  	m.Value = result
    58  }
    59  
    60  type MetadataV12Module struct {
    61  	ScaleDecoder
    62  	Name       string                   `json:"name"`
    63  	Prefix     string                   `json:"prefix"`
    64  	CallIndex  string                   `json:"call_index"`
    65  	HasStorage bool                     `json:"has_storage"`
    66  	Storage    []MetadataStorage        `json:"storage"`
    67  	HasCalls   bool                     `json:"has_calls"`
    68  	Calls      []MetadataModuleCall     `json:"calls"`
    69  	HasEvents  bool                     `json:"has_events"`
    70  	Events     []MetadataEvents         `json:"events"`
    71  	Constants  []map[string]interface{} `json:"constants"`
    72  	Errors     []MetadataModuleError    `json:"errors"`
    73  	Index      int                      `json:"index"`
    74  }
    75  
    76  func (m *MetadataV12Module) GetIdentifier() string {
    77  	return m.Name
    78  }
    79  
    80  func (m *MetadataV12Module) Process() {
    81  	cm := MetadataV12Module{}
    82  	cm.Name = m.ProcessAndUpdateData("String").(string)
    83  
    84  	// storage
    85  	cm.HasStorage = m.ProcessAndUpdateData("bool").(bool)
    86  	if cm.HasStorage {
    87  		storageValue := m.ProcessAndUpdateData("MetadataV7ModuleStorage").(MetadataV7ModuleStorage)
    88  		cm.Storage = storageValue.Items
    89  		cm.Prefix = storageValue.Prefix
    90  	}
    91  
    92  	// call
    93  	cm.HasCalls = m.ProcessAndUpdateData("bool").(bool)
    94  	if cm.HasCalls {
    95  		callValue := m.ProcessAndUpdateData("Vec<MetadataModuleCall>").([]interface{})
    96  		var calls []MetadataModuleCall
    97  		for _, v := range callValue {
    98  			calls = append(calls, v.(MetadataModuleCall))
    99  		}
   100  		cm.Calls = calls
   101  	}
   102  
   103  	// event
   104  	cm.HasEvents = m.ProcessAndUpdateData("bool").(bool)
   105  	if cm.HasEvents {
   106  		eventValue := m.ProcessAndUpdateData("Vec<MetadataModuleEvent>").([]interface{})
   107  		var events []MetadataEvents
   108  		for _, v := range eventValue {
   109  			events = append(events, v.(MetadataEvents))
   110  		}
   111  		cm.Events = events
   112  	}
   113  
   114  	// constant
   115  	constantValue := m.ProcessAndUpdateData("Vec<MetadataV7ModuleConstants>").([]interface{})
   116  	var constants []map[string]interface{}
   117  	for _, v := range constantValue {
   118  		constants = append(constants, v.(map[string]interface{}))
   119  	}
   120  	cm.Constants = constants
   121  
   122  	errorValue := m.ProcessAndUpdateData("Vec<MetadataModuleError>").([]interface{})
   123  	var errors []MetadataModuleError
   124  	for _, v := range errorValue {
   125  		errors = append(errors, v.(MetadataModuleError))
   126  	}
   127  	cm.Errors = errors
   128  	cm.Index = m.ProcessAndUpdateData("U8").(int)
   129  	m.Value = cm
   130  }