github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/v6.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 MetadataV6Decoder struct {
    11  	ScaleDecoder
    12  }
    13  
    14  func (m *MetadataV6Decoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
    15  	m.ScaleDecoder.Init(data, option)
    16  }
    17  
    18  func (m *MetadataV6Decoder) Process() {
    19  	result := MetadataStruct{
    20  		Metadata: MetadataTag{
    21  			Modules: nil,
    22  		},
    23  	}
    24  	result.CallIndex = make(map[string]CallIndex)
    25  	result.EventIndex = make(map[string]EventIndex)
    26  	metadataV6ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV6Module>").([]interface{})
    27  
    28  	callModuleIndex := 0
    29  	eventModuleIndex := 0
    30  	bm, _ := json.Marshal(metadataV6ModuleCall)
    31  	var modulesType []MetadataModules
    32  	_ = json.Unmarshal(bm, &modulesType)
    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{
    38  					Module: module,
    39  					Call:   call,
    40  				}
    41  			}
    42  			callModuleIndex++
    43  		}
    44  		if module.Events != nil {
    45  			for eventIndex, event := range module.Events {
    46  				modulesType[k].Events[eventIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(eventModuleIndex), 2, "0") + xstrings.RightJustify(utiles.IntToHex(eventIndex), 2, "0")
    47  				result.EventIndex[modulesType[k].Events[eventIndex].Lookup] = EventIndex{
    48  					Module: module,
    49  					Call:   event,
    50  				}
    51  			}
    52  			eventModuleIndex++
    53  		}
    54  	}
    55  
    56  	result.Metadata.Modules = modulesType
    57  	m.Value = result
    58  }
    59  
    60  type MetadataV6Module 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  }
    73  
    74  func (m *MetadataV6Module) GetIdentifier() string {
    75  	return m.Name
    76  }
    77  
    78  func (m *MetadataV6Module) Process() {
    79  	cm := MetadataV6Module{}
    80  	cm.Name = m.ProcessAndUpdateData("String").(string)
    81  	cm.Prefix = m.ProcessAndUpdateData("String").(string)
    82  	cm.HasStorage = m.ProcessAndUpdateData("bool").(bool)
    83  	if cm.HasStorage {
    84  		storageValue := m.ProcessAndUpdateData("Vec<MetadataV6ModuleStorage>").([]interface{})
    85  		var storage []MetadataStorage
    86  		for _, v := range storageValue {
    87  			storage = append(storage, v.(MetadataStorage))
    88  		}
    89  		cm.Storage = storage
    90  	}
    91  
    92  	cm.HasCalls = m.ProcessAndUpdateData("bool").(bool)
    93  	if cm.HasCalls {
    94  		callValue := m.ProcessAndUpdateData("Vec<MetadataModuleCall>").([]interface{})
    95  		var calls []MetadataModuleCall
    96  		for _, v := range callValue {
    97  			calls = append(calls, v.(MetadataModuleCall))
    98  		}
    99  		cm.Calls = calls
   100  	}
   101  	cm.HasEvents = m.ProcessAndUpdateData("bool").(bool)
   102  	if cm.HasEvents {
   103  		eventValue := m.ProcessAndUpdateData("Vec<MetadataModuleEvent>").([]interface{})
   104  		var events []MetadataEvents
   105  		for _, v := range eventValue {
   106  			events = append(events, v.(MetadataEvents))
   107  		}
   108  		cm.Events = events
   109  	}
   110  	constantValue := m.ProcessAndUpdateData("Vec<MetadataV6ModuleConstants>").([]interface{})
   111  	var constants []map[string]interface{}
   112  	for _, v := range constantValue {
   113  		constants = append(constants, v.(map[string]interface{}))
   114  	}
   115  	cm.Constants = constants
   116  	m.Value = cm
   117  }
   118  
   119  type MetadataV6ModuleConstants struct {
   120  	ScaleDecoder
   121  	Name           string   `json:"name"`
   122  	Type           string   `json:"type"`
   123  	ConstantsValue string   `json:"constants_value"`
   124  	Docs           []string `json:"docs"`
   125  }
   126  
   127  func (m *MetadataV6ModuleConstants) Process() {
   128  	name := m.ProcessAndUpdateData("String").(string)
   129  	cType := ConvertType(m.ProcessAndUpdateData("String").(string))
   130  	ConstantsValue := m.ProcessAndUpdateData("HexBytes").(string)
   131  	var docsArr []string
   132  	docs := m.ProcessAndUpdateData("Vec<String>").([]interface{})
   133  	for _, v := range docs {
   134  		docsArr = append(docsArr, v.(string))
   135  	}
   136  	r := map[string]interface{}{
   137  		"name":            name,
   138  		"type":            cType,
   139  		"constants_value": ConstantsValue,
   140  		"docs":            docsArr,
   141  	}
   142  	m.Value = r
   143  }
   144  
   145  type MetadataV6ModuleStorage struct {
   146  	ScaleDecoder
   147  	Name     string                 `json:"name"`
   148  	Modifier string                 `json:"modifier"`
   149  	Type     map[string]interface{} `json:"type"`
   150  	Fallback string                 `json:"fallback"`
   151  	Docs     []string               `json:"docs"`
   152  	Hasher   string                 `json:"hasher"`
   153  }
   154  
   155  func (m *MetadataV6ModuleStorage) Init(data ScaleBytes, option *ScaleDecoderOption) {
   156  	m.ScaleDecoder.Init(data, option)
   157  }
   158  
   159  func (m *MetadataV6ModuleStorage) Process() {
   160  	cm := MetadataStorage{}
   161  	cm.Name = m.ProcessAndUpdateData("String").(string)
   162  	cm.Modifier = m.ProcessAndUpdateData("StorageModify").(string)
   163  	storageFunctionType := m.ProcessAndUpdateData("StorageFunctionType").(string)
   164  	if storageFunctionType == "MapType" {
   165  		cm.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
   166  		cm.Type = StorageType{
   167  			Origin: "MapType",
   168  			MapType: &MapType{
   169  				Hasher:   cm.Hasher,
   170  				Key:      ConvertType(m.ProcessAndUpdateData("String").(string)),
   171  				Value:    ConvertType(m.ProcessAndUpdateData("String").(string)),
   172  				IsLinked: m.ProcessAndUpdateData("bool").(bool),
   173  			},
   174  		}
   175  	} else if storageFunctionType == "DoubleMapType" {
   176  		cm.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
   177  		key1 := ConvertType(m.ProcessAndUpdateData("String").(string))
   178  		key2 := ConvertType(m.ProcessAndUpdateData("String").(string))
   179  		value := ConvertType(m.ProcessAndUpdateData("String").(string))
   180  		key2Hasher := m.ProcessAndUpdateData("StorageHasher").(string)
   181  		cm.Type = StorageType{
   182  			Origin: "DoubleMapType",
   183  			DoubleMapType: &MapType{
   184  				Hasher:     cm.Hasher,
   185  				Key:        key1,
   186  				Key2:       key2,
   187  				Value:      value,
   188  				Key2Hasher: key2Hasher,
   189  			},
   190  		}
   191  	} else if storageFunctionType == "PlainType" {
   192  		plainType := ConvertType(m.ProcessAndUpdateData("String").(string))
   193  		cm.Type = StorageType{Origin: "PlainType", PlainType: &plainType}
   194  	}
   195  	cm.Fallback = m.ProcessAndUpdateData("HexBytes").(string)
   196  	docs := m.ProcessAndUpdateData("Vec<String>").([]interface{})
   197  	for _, v := range docs {
   198  		cm.Docs = append(m.Docs, v.(string))
   199  	}
   200  	m.Value = cm
   201  }