From 5c4c9cdc99761be1b43b307ba02423ca0bfa2a51 Mon Sep 17 00:00:00 2001 From: tinkle-community Date: Tue, 6 Jan 2026 10:43:21 +0800 Subject: [PATCH] fix: handle large Binance trade IDs in Go to avoid database CAST limitations --- store/order.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/store/order.go b/store/order.go index 313f1f18..6aeb69b6 100644 --- a/store/order.go +++ b/store/order.go @@ -2,7 +2,7 @@ package store import ( "fmt" - "strings" + "strconv" "time" "gorm.io/gorm" @@ -324,28 +324,32 @@ func (s *OrderStore) GetDuplicateFillsCount() (int, error) { // GetMaxTradeIDsByExchange returns max trade ID for each symbol for a given exchange func (s *OrderStore) GetMaxTradeIDsByExchange(exchangeID string) (map[string]int64, error) { - type symbolMaxID struct { - Symbol string - MaxTradeID int64 + type symbolTradeID struct { + Symbol string + ExchangeTradeID string } - var results []symbolMaxID + var results []symbolTradeID + // Query all trade IDs grouped by symbol, find max in Go to avoid database-specific CAST issues + // (PostgreSQL INTEGER is 32-bit, can't handle Binance trade IDs > 2.1B) err := s.db.Model(&TraderFill{}). - Select("symbol, MAX(CAST(exchange_trade_id AS INTEGER)) as max_trade_id"). + Select("symbol, exchange_trade_id"). Where("exchange_id = ? AND exchange_trade_id != ''", exchangeID). - Group("symbol"). Find(&results).Error if err != nil { - // If CAST fails (non-numeric trade IDs), fallback to string comparison - if strings.Contains(err.Error(), "CAST") || strings.Contains(err.Error(), "invalid") { - return make(map[string]int64), nil - } - return nil, fmt.Errorf("failed to query max trade IDs: %w", err) + return nil, fmt.Errorf("failed to query trade IDs: %w", err) } + // Find max trade ID per symbol in Go (handles 64-bit integers properly) result := make(map[string]int64) for _, r := range results { - result[r.Symbol] = r.MaxTradeID + tradeID, err := strconv.ParseInt(r.ExchangeTradeID, 10, 64) + if err != nil { + continue // Skip non-numeric trade IDs + } + if tradeID > result[r.Symbol] { + result[r.Symbol] = tradeID + } } return result, nil