-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: 5552/xdrill-changes
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 GitHub Actions / build (ubuntu-22.04, 1.22)
Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go GitHub Actions / test (ubuntu-22.04, 1.22, 12)
Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go GitHub Actions / build (ubuntu-22.04, 1.23)
Check failure on line 7 in ingest/diagnostic_event/diagnostic_event.go GitHub Actions / test (ubuntu-22.04, 1.23, 12)
|
||
"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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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