github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/v9.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 MetadataV9Decoder struct {
    11  	ScaleDecoder
    12  }
    13  
    14  func (m *MetadataV9Decoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
    15  	m.ScaleDecoder.Init(data, option)
    16  }
    17  
    18  func (m *MetadataV9Decoder) Process() {
    19  	result := MetadataStruct{
    20  		Metadata: MetadataTag{
    21  			Modules: nil,
    22  		},
    23  	}
    24  	MetadataV9ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV8Module>").([]interface{})
    25  	callModuleIndex := 0
    26  	eventModuleIndex := 0
    27  	result.CallIndex = make(map[string]CallIndex)
    28  	result.EventIndex = make(map[string]EventIndex)
    29  	bm, _ := json.Marshal(MetadataV9ModuleCall)
    30  	var modulesType []MetadataModules
    31  	_ = json.Unmarshal(bm, &modulesType)
    32  	for k, module := range modulesType {
    33  		if module.Calls != nil {
    34  			for callIndex, call := range module.Calls {
    35  				modulesType[k].Calls[callIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(callModuleIndex), 2, "0") + xstrings.RightJustify(utiles.IntToHex(callIndex), 2, "0")
    36  				result.CallIndex[modulesType[k].Calls[callIndex].Lookup] = CallIndex{Module: module, Call: call}
    37  			}
    38  			callModuleIndex++
    39  		}
    40  		if module.Events != nil {
    41  			for eventIndex, event := range module.Events {
    42  				modulesType[k].Events[eventIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(eventModuleIndex), 2, "0") + xstrings.RightJustify(utiles.IntToHex(eventIndex), 2, "0")
    43  				result.EventIndex[modulesType[k].Events[eventIndex].Lookup] = EventIndex{Module: module, Call: event}
    44  			}
    45  			eventModuleIndex++
    46  		}
    47  	}
    48  
    49  	result.Metadata.Modules = modulesType
    50  	m.Value = result
    51  }