github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/v7.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 MetadataV7Decoder struct {
    11  	ScaleDecoder
    12  }
    13  
    14  func (m *MetadataV7Decoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
    15  	m.ScaleDecoder.Init(data, option)
    16  }
    17  
    18  func (m *MetadataV7Decoder) 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  	metadataV7ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV7Module>").([]interface{})
    27  	callModuleIndex := 0
    28  	eventModuleIndex := 0
    29  	bm, _ := json.Marshal(metadataV7ModuleCall)
    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 MetadataV7Module 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  }
    72  
    73  func (m *MetadataV7Module) Process() {
    74  	cm := MetadataV7Module{}
    75  	cm.Name = m.ProcessAndUpdateData("String").(string)
    76  
    77  	cm.HasStorage = m.ProcessAndUpdateData("bool").(bool)
    78  	if cm.HasStorage {
    79  		storageValue := m.ProcessAndUpdateData("MetadataV7ModuleStorage").(MetadataV7ModuleStorage)
    80  		cm.Storage = storageValue.Items
    81  		cm.Prefix = storageValue.Prefix
    82  	}
    83  	cm.HasCalls = m.ProcessAndUpdateData("bool").(bool)
    84  	if cm.HasCalls {
    85  		callValue := m.ProcessAndUpdateData("Vec<MetadataModuleCall>").([]interface{})
    86  		var calls []MetadataModuleCall
    87  		for _, v := range callValue {
    88  			calls = append(calls, v.(MetadataModuleCall))
    89  		}
    90  		cm.Calls = calls
    91  	}
    92  	cm.HasEvents = m.ProcessAndUpdateData("bool").(bool)
    93  	if cm.HasEvents {
    94  		eventValue := m.ProcessAndUpdateData("Vec<MetadataModuleEvent>").([]interface{})
    95  		var events []MetadataEvents
    96  		for _, v := range eventValue {
    97  			events = append(events, v.(MetadataEvents))
    98  		}
    99  		cm.Events = events
   100  	}
   101  	constantValue := m.ProcessAndUpdateData("Vec<MetadataV7ModuleConstants>").([]interface{})
   102  	var constants []map[string]interface{}
   103  	for _, v := range constantValue {
   104  		constants = append(constants, v.(map[string]interface{}))
   105  	}
   106  	cm.Constants = constants
   107  	m.Value = cm
   108  }
   109  
   110  type MetadataV7ModuleConstants struct {
   111  	MetadataV6ModuleConstants
   112  }
   113  
   114  type MetadataV7ModuleStorage struct {
   115  	MetadataV6ModuleStorage
   116  	Prefix string            `json:"prefix"`
   117  	Items  []MetadataStorage `json:"items"`
   118  }
   119  
   120  func (m *MetadataV7ModuleStorage) Process() {
   121  	cm := MetadataV7ModuleStorage{}
   122  	cm.Prefix = m.ProcessAndUpdateData("String").(string)
   123  	itemsValue := m.ProcessAndUpdateData("Vec<MetadataV7ModuleStorageEntry>").([]interface{})
   124  	var items []MetadataStorage
   125  	for _, v := range itemsValue {
   126  		items = append(items, v.(MetadataStorage))
   127  	}
   128  	cm.Items = items
   129  	m.Value = cm
   130  }
   131  
   132  type MetadataV7ModuleStorageEntry struct {
   133  	ScaleDecoder
   134  	Name     string      `json:"name"`
   135  	Modifier string      `json:"modifier"`
   136  	Type     StorageType `json:"type"`
   137  	Fallback string      `json:"fallback"`
   138  	Docs     []string    `json:"docs"`
   139  	Hasher   string      `json:"hasher"`
   140  }
   141  
   142  func (m *MetadataV7ModuleStorageEntry) Init(data ScaleBytes, option *ScaleDecoderOption) {
   143  	m.ScaleDecoder.Init(data, option)
   144  }
   145  
   146  func (m *MetadataV7ModuleStorageEntry) Process() {
   147  	m.Name = m.ProcessAndUpdateData("String").(string)
   148  	m.Modifier = m.ProcessAndUpdateData("StorageModify").(string)
   149  	storageFunctionType := m.ProcessAndUpdateData("StorageFunctionType").(string)
   150  	if storageFunctionType == "MapType" {
   151  		m.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
   152  		m.Type = StorageType{
   153  			Origin: "MapType",
   154  			MapType: &MapType{
   155  				Hasher:   m.Hasher,
   156  				Key:      ConvertType(m.ProcessAndUpdateData("String").(string)),
   157  				Value:    ConvertType(m.ProcessAndUpdateData("String").(string)),
   158  				IsLinked: m.ProcessAndUpdateData("bool").(bool)},
   159  		}
   160  	} else if storageFunctionType == "DoubleMapType" {
   161  		m.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
   162  		key1 := ConvertType(m.ProcessAndUpdateData("String").(string))
   163  		key2 := ConvertType(m.ProcessAndUpdateData("String").(string))
   164  		value := ConvertType(m.ProcessAndUpdateData("String").(string))
   165  		key2Hasher := m.ProcessAndUpdateData("StorageHasher").(string)
   166  		m.Type = StorageType{
   167  			Origin: "DoubleMapType",
   168  			DoubleMapType: &MapType{
   169  				Hasher:     m.Hasher,
   170  				Key:        key1,
   171  				Key2:       key2,
   172  				Value:      value,
   173  				Key2Hasher: key2Hasher,
   174  			},
   175  		}
   176  	} else if storageFunctionType == "PlainType" {
   177  		plainType := ConvertType(m.ProcessAndUpdateData("String").(string))
   178  		m.Type = StorageType{
   179  			Origin:    "PlainType",
   180  			PlainType: &plainType}
   181  	}
   182  	m.Fallback = m.ProcessAndUpdateData("HexBytes").(string)
   183  	docs := m.ProcessAndUpdateData("Vec<String>").([]interface{})
   184  	for _, v := range docs {
   185  		m.Docs = append(m.Docs, v.(string))
   186  	}
   187  	m.Value = MetadataStorage{
   188  		Name:     m.Name,
   189  		Modifier: m.Modifier,
   190  		Type:     m.Type,
   191  		Fallback: m.Fallback,
   192  		Docs:     m.Docs,
   193  	}
   194  
   195  }