github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/pkg/stafidecoder/event.go (about)

     1  package stafi_decoder
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/itering/scale.go/utiles"
     7  )
     8  
     9  type EventsDecoder struct {
    10  	Vec
    11  	Metadata *MetadataStruct
    12  }
    13  
    14  func (e *EventsDecoder) Init(data ScaleBytes, option *ScaleDecoderOption) {
    15  	if option.Metadata == nil {
    16  		panic("ExtrinsicDecoder option metadata required")
    17  	}
    18  	e.TypeString = "Vec<EventRecord>"
    19  	e.Metadata = option.Metadata
    20  	e.Vec.Init(data, option)
    21  }
    22  
    23  type EventParam struct {
    24  	Type  string      `json:"type"`
    25  	Value interface{} `json:"value"`
    26  }
    27  
    28  func (e *EventsDecoder) Process() {
    29  	elementCount := e.ProcessAndUpdateData("Compact<u32>").(int)
    30  
    31  	er := EventRecord{Metadata: e.Metadata}
    32  	er.Data = e.Data
    33  	er.Spec = e.Spec
    34  	var result []interface{}
    35  	for i := 0; i < elementCount; i++ {
    36  		element := er.Process()
    37  		element["event_idx"] = i
    38  		result = append(result, element)
    39  	}
    40  	e.Value = result
    41  }
    42  
    43  type EventRecord struct {
    44  	ScaleDecoder
    45  	Metadata     *MetadataStruct
    46  	Phase        int             `json:"phase"`
    47  	ExtrinsicIdx int             `json:"extrinsic_idx"`
    48  	Type         string          `json:"type"`
    49  	Params       []EventParam    `json:"params"`
    50  	Event        MetadataEvents  `json:"event"`
    51  	EventModule  MetadataModules `json:"event_module"`
    52  	Topic        []string        `json:"topic"`
    53  }
    54  
    55  func (e *EventRecord) Process() map[string]interface{} {
    56  	e.Params = []EventParam{}
    57  	e.Topic = []string{}
    58  
    59  	e.Phase = e.GetNextU8()
    60  
    61  	if e.Phase == 0 {
    62  		e.ExtrinsicIdx = int(e.ProcessAndUpdateData("U32").(uint32))
    63  	}
    64  	e.Type = utiles.BytesToHex(e.NextBytes(2))
    65  
    66  	call, ok := e.Metadata.EventIndex[e.Type]
    67  	if !ok {
    68  		panic(fmt.Sprintf("Not find Extrinsic Lookup %s, please check metadata info", e.Type))
    69  	}
    70  
    71  	e.Event = call.Call
    72  	e.EventModule = call.Module
    73  
    74  	for _, argType := range e.Event.Args {
    75  		e.Params = append(e.Params, EventParam{Type: argType, Value: e.ProcessAndUpdateData(argType)})
    76  	}
    77  
    78  	if e.Metadata.MetadataVersion >= 5 {
    79  		if topic := e.ProcessAndUpdateData("Vec<Hash>"); topic != nil {
    80  			topicValue := topic.([]interface{})
    81  			for _, v := range topicValue {
    82  				e.Topic = append(e.Topic, v.(string))
    83  			}
    84  		}
    85  	}
    86  
    87  	return map[string]interface{}{
    88  		"phase":         e.Phase,
    89  		"extrinsic_idx": e.ExtrinsicIdx,
    90  		"type":          e.Type,
    91  		"module_id":     e.EventModule.Name,
    92  		"event_id":      e.Event.Name,
    93  		"params":        e.Params,
    94  		"topic":         e.Topic,
    95  	}
    96  
    97  }