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