-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ingest: Move SAC event parsing to its own package, add more events. (#…
…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
Showing
8 changed files
with
596 additions
and
272 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.