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

ingest: Move SAC event parsing to its own package, add more events. #4802

Merged
merged 12 commits into from
Mar 9, 2023
152 changes: 0 additions & 152 deletions ingest/event_test.go

This file was deleted.

54 changes: 54 additions & 0 deletions support/contractevents/burn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package contractevents

import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)

var ErrNotBurnEvent = errors.New("event is not a valid 'burn' event")

type BurnEvent struct {
sacEvent

From string
Amount xdr.Int128Parts
}

// parseBurnEvent tries to parse the given topics and value as a SAC "burn"
// event.
//
// Internally, it assumes that the `topics` array has already validated both the
// function name AND the asset <--> contract ID relationship. It will return a
// best-effort parsing even in error cases.
func (event *BurnEvent) parse(topics xdr.ScVec, value xdr.ScVal) error {
//
// The burn event format is:
//
// "burn" Symbol
// <from> Address
// <asset> Bytes
//
// <amount> i128
//
// Reference: https://github.com/stellar/rs-soroban-env/blob/main/soroban-env-host/src/native_contract/token/event.rs#L102-L109
//
if len(topics) != 3 {
return ErrNotBurnEvent
}

rawFrom := topics[1]
from := parseAddress(&rawFrom)
if from == nil {
return ErrNotBurnEvent
}

event.From = MustScAddressToString(from)

amount := parseAmount(&value)
if amount == nil {
return ErrNotBurnEvent
}

event.Amount = *amount
return nil
}
41 changes: 41 additions & 0 deletions support/contractevents/clawback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package contractevents

import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)

var ErrNotClawbackEvent = errors.New("event is not a valid 'clawback' event")

type ClawbackEvent struct {
sacEvent

Admin string
From string
Amount xdr.Int128Parts
}

// parseClawbackEvent tries to parse the given topics and value as a SAC
// "clawback" event.
//
// Internally, it assumes that the `topics` array has already validated both the
// function name AND the asset <--> contract ID relationship. It will return a
// best-effort parsing even in error cases.
func (event *ClawbackEvent) parse(topics xdr.ScVec, value xdr.ScVal) error {
//
// The clawback event format is:
//
// "clawback" Symbol
// <admin> Address
// <from> Address
// <asset> Bytes
//
// <amount> i128
//
var err error
event.Admin, event.From, event.Amount, err = parseBalanceChangeEvent(topics, value)
if err != nil {
return ErrNotClawbackEvent
}
return nil
}
Loading