-
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.
scaffolding code for generic trade processor
- Loading branch information
1 parent
3b3bf8c
commit b4cf033
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
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,70 @@ | ||
package trades | ||
|
||
import ( | ||
"github.com/stellar/go/ingest" | ||
"github.com/stellar/go/support/errors" | ||
"github.com/stellar/go/xdr" | ||
"io" | ||
) | ||
|
||
func ProcessTradesFromLedger(ledger xdr.LedgerCloseMeta, networkPassPhrase string) ([]TradeEvent, error) { | ||
changeReader, err := ingest.NewLedgerChangeReaderFromLedgerCloseMeta(networkPassPhrase, ledger) | ||
if err != nil { | ||
return []TradeEvent{}, errors.Wrap(err, "Error creating ledger change reader") | ||
} | ||
defer changeReader.Close() | ||
|
||
tradeEvents := make([]TradeEvent, 0) | ||
for { | ||
change, err := changeReader.Read() | ||
if err == io.EOF { | ||
break | ||
} | ||
if err != nil { | ||
return []TradeEvent{}, errors.Wrap(err, "Error reading ledger change") | ||
} | ||
// Process trades from the change | ||
tradesFromChange, err := processTradesFromChange(change) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error processing trades from change") | ||
} | ||
|
||
// Append to the overall trade events slice | ||
tradeEvents = append(tradeEvents, tradesFromChange...) | ||
} | ||
|
||
return []TradeEvent{}, nil | ||
} | ||
|
||
func processTradesFromChange(change ingest.Change) ([]TradeEvent, error) { | ||
var tradeEvents []TradeEvent | ||
|
||
switch change.Type { | ||
case xdr.LedgerEntryTypeOffer: | ||
trades, err := processOffersFromChange(change) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error processing offers") | ||
} | ||
tradeEvents = append(tradeEvents, trades...) | ||
case xdr.LedgerEntryTypeLiquidityPool: | ||
trades, err := processLiquidityPoolEventsFromChange(change) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Error processing liquidity pool events") | ||
} | ||
tradeEvents = append(tradeEvents, trades...) | ||
|
||
default: | ||
// Nothing to do | ||
} | ||
|
||
return tradeEvents, nil | ||
} | ||
|
||
func processOffersFromChange(change ingest.Change) ([]TradeEvent, error) { | ||
|
||
return []TradeEvent{}, nil | ||
} | ||
|
||
func processLiquidityPoolEventsFromChange(change ingest.Change) ([]TradeEvent, error) { | ||
return []TradeEvent{}, nil | ||
} |