github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/v8.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 MetadataV8Decoder struct { 11 ScaleDecoder 12 } 13 14 func (m *MetadataV8Decoder) Init(data ScaleBytes, option *ScaleDecoderOption) { 15 m.ScaleDecoder.Init(data, option) 16 } 17 18 func (m *MetadataV8Decoder) Process() { 19 result := MetadataStruct{ 20 Metadata: MetadataTag{ 21 Modules: nil, 22 }, 23 } 24 metadataV8ModuleCall := 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(metadataV8ModuleCall) 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{ 37 Module: module, 38 Call: call, 39 } 40 } 41 callModuleIndex++ 42 } 43 if module.Events != nil { 44 for eventIndex, event := range module.Events { 45 modulesType[k].Events[eventIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(eventModuleIndex), 2, "0") + xstrings.RightJustify(utiles.IntToHex(eventIndex), 2, "0") 46 result.EventIndex[modulesType[k].Events[eventIndex].Lookup] = EventIndex{ 47 Module: module, 48 Call: event, 49 } 50 } 51 eventModuleIndex++ 52 } 53 } 54 55 result.Metadata.Modules = modulesType 56 m.Value = result 57 } 58 59 type MetadataV8Module struct { 60 ScaleDecoder 61 Name string `json:"name"` 62 Prefix string `json:"prefix"` 63 CallIndex string `json:"call_index"` 64 HasStorage bool `json:"has_storage"` 65 Storage []MetadataStorage `json:"storage"` 66 HasCalls bool `json:"has_calls"` 67 Calls []MetadataModuleCall `json:"calls"` 68 HasEvents bool `json:"has_events"` 69 Events []MetadataEvents `json:"events"` 70 Constants []map[string]interface{} `json:"constants"` 71 Errors []MetadataModuleError `json:"errors"` 72 } 73 74 func (m *MetadataV8Module) GetIdentifier() string { 75 return m.Name 76 } 77 78 func (m *MetadataV8Module) Process() { 79 cm := MetadataV8Module{} 80 cm.Name = m.ProcessAndUpdateData("String").(string) 81 82 // storage 83 cm.HasStorage = m.ProcessAndUpdateData("bool").(bool) 84 if cm.HasStorage { 85 storageValue := m.ProcessAndUpdateData("MetadataV7ModuleStorage").(MetadataV7ModuleStorage) 86 cm.Storage = storageValue.Items 87 cm.Prefix = storageValue.Prefix 88 } 89 90 // call 91 cm.HasCalls = m.ProcessAndUpdateData("bool").(bool) 92 if cm.HasCalls { 93 callValue := m.ProcessAndUpdateData("Vec<MetadataModuleCall>").([]interface{}) 94 calls := []MetadataModuleCall{} 95 for _, v := range callValue { 96 calls = append(calls, v.(MetadataModuleCall)) 97 } 98 cm.Calls = calls 99 } 100 101 // event 102 cm.HasEvents = m.ProcessAndUpdateData("bool").(bool) 103 if cm.HasEvents { 104 eventValue := m.ProcessAndUpdateData("Vec<MetadataModuleEvent>").([]interface{}) 105 events := []MetadataEvents{} 106 for _, v := range eventValue { 107 events = append(events, v.(MetadataEvents)) 108 } 109 cm.Events = events 110 } 111 112 // constant 113 constantValue := m.ProcessAndUpdateData("Vec<MetadataV7ModuleConstants>").([]interface{}) 114 var constants []map[string]interface{} 115 for _, v := range constantValue { 116 constants = append(constants, v.(map[string]interface{})) 117 } 118 cm.Constants = constants 119 120 errorValue := m.ProcessAndUpdateData("Vec<MetadataModuleError>").([]interface{}) 121 var errors []MetadataModuleError 122 for _, v := range errorValue { 123 errors = append(errors, v.(MetadataModuleError)) 124 } 125 cm.Errors = errors 126 m.Value = cm 127 } 128 129 type MetadataModuleError struct { 130 ScaleDecoder `json:"-"` 131 Name string `json:"name"` 132 Doc []string `json:"doc"` 133 } 134 135 func (m *MetadataModuleError) Init(data ScaleBytes, option *ScaleDecoderOption) { 136 m.Name = "" 137 m.Doc = []string{} 138 m.ScaleDecoder.Init(data, option) 139 } 140 141 func (m *MetadataModuleError) Process() { 142 cm := MetadataModuleError{} 143 cm.Name = m.ProcessAndUpdateData("String").(string) 144 var docsArr []string 145 docs := m.ProcessAndUpdateData("Vec<String>").([]interface{}) 146 for _, v := range docs { 147 docsArr = append(docsArr, v.(string)) 148 } 149 cm.Doc = docsArr 150 m.Value = cm 151 }