From b4cf0338d27864f8ed93211ad220027bd8ccc904 Mon Sep 17 00:00:00 2001 From: Karthik Iyer Date: Sat, 7 Dec 2024 19:59:43 -0800 Subject: [PATCH] scaffolding code for generic trade processor --- .../trades/generic_trades_offers_processor.go | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 ingest/trades/generic_trades_offers_processor.go diff --git a/ingest/trades/generic_trades_offers_processor.go b/ingest/trades/generic_trades_offers_processor.go new file mode 100644 index 0000000000..0fb513ca76 --- /dev/null +++ b/ingest/trades/generic_trades_offers_processor.go @@ -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 +}