Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xdrill functions for contract events #4

Open
wants to merge 1 commit into
base: 5552/xdrill-changes
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 / test (ubuntu-22.04, 1.22, 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 / 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.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:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: move xdr2json to it's own repo stellar/stellar-rpc#350

"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) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted to pass in the event like with the go/ingest/ledger xdrill LCM functions

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
}
Loading