Skip to content

Commit

Permalink
xdrill functions for contract events
Browse files Browse the repository at this point in the history
  • Loading branch information
chowbao committed Jan 30, 2025
1 parent c8c4caf commit dd68880
Showing 1 changed file with 115 additions and 0 deletions.
115 changes: 115 additions & 0 deletions ingest/diagnostic_event/diagnostic_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package diagnosticevent

import (
"encoding/json"
"fmt"

"github.com/chowbao/go-stellar-xdr-json/xdr2json"

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 1.22)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 1.23)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.22, 16)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.23, 12)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:

Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-22.04, 1.23, 16)

no required module provides package github.com/chowbao/go-stellar-xdr-json/xdr2json; to add it:
"github.com/stellar/go/strkey"
"github.com/stellar/go/xdr"
)

func Successful(d xdr.DiagnosticEvent) bool {
return d.InSuccessfulContractCall
}

func Type(d xdr.DiagnosticEvent) int32 {
return int32(d.Event.Type)
}

func ContractID(d xdr.DiagnosticEvent) (string, bool, error) {
if d.Event.ContractId == nil {
return "", false, nil
}

var err error
var contractIdByte []byte
var contractIDString string
contractId := *d.Event.ContractId
contractIdByte, err = contractId.MarshalBinary()
if err != nil {
return "", false, nil
}
contractIDString, err = strkey.Encode(strkey.VersionByteContract, contractIdByte)
if err != nil {
return "", false, nil
}

return contractIDString, true, nil
}

func Topics(d xdr.DiagnosticEvent) ([]interface{}, error) {
topics, err := GetEventTopics(d)
if err != nil {
return []interface{}{}, err
}

return serializeScValArray(topics)
}

func Data(d xdr.DiagnosticEvent) (interface{}, error) {
data, err := GetEventData(d)
if err != nil {
return []interface{}{}, err
}

return serializeScVal(data)
}

func GetEventTopics(d xdr.DiagnosticEvent) ([]xdr.ScVal, error) {
switch d.Event.Body.V {
case 0:
contractEventV0 := d.Event.Body.MustV0()
return contractEventV0.Topics, nil
default:
return []xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V))
}
}

func GetEventData(d xdr.DiagnosticEvent) (xdr.ScVal, error) {
switch d.Event.Body.V {
case 0:
contractEventV0 := d.Event.Body.MustV0()
return contractEventV0.Data, nil
default:
return xdr.ScVal{}, fmt.Errorf("unsupported event body version: " + string(d.Event.Body.V))
}
}

func serializeScVal(scVal xdr.ScVal) (interface{}, error) {
var serializedDataDecoded interface{}
serializedDataDecoded = "n/a"

if _, ok := scVal.ArmForSwitch(int32(scVal.Type)); ok {
var err error
var raw []byte
var jsonMessage json.RawMessage
raw, err = scVal.MarshalBinary()
if err != nil {
return nil, err
}

jsonMessage, err = xdr2json.ConvertBytes(xdr.ScVal{}, raw)
if err != nil {
return nil, err
}

serializedDataDecoded = jsonMessage
}

return serializedDataDecoded, nil
}

func serializeScValArray(scVals []xdr.ScVal) ([]interface{}, error) {
dataDecoded := make([]interface{}, 0, len(scVals))

for _, scVal := range scVals {
serializedDataDecoded, err := serializeScVal(scVal)
if err != nil {
return nil, err
}
dataDecoded = append(dataDecoded, serializedDataDecoded)
}

return dataDecoded, nil
}

0 comments on commit dd68880

Please sign in to comment.