Skip to content

Commit

Permalink
ingest: Move SAC event parsing to its own package, add more events. (#…
Browse files Browse the repository at this point in the history
…4802)

* Move contract event parsing to its own package
* Add helper to parse ScAddresses
* Add mint test cases, contract address parsing
* Add fuzzing to parser to ensure no panics
* Add clawback event parsing, abstract to generic parser
* Add burn event support
* Add helper to parse i128 amounts
  • Loading branch information
Shaptic authored Mar 9, 2023
1 parent 0c5115c commit 8743284
Show file tree
Hide file tree
Showing 8 changed files with 596 additions and 272 deletions.
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

0 comments on commit 8743284

Please sign in to comment.