From 135059d1d073f74360fe1ed5223b3763b30ffed2 Mon Sep 17 00:00:00 2001 From: Alexis Bruneteau Date: Sun, 7 Dec 2025 13:13:34 +0100 Subject: [PATCH] Fix trade detection: remove infinite pagination loop - API returns latest 100 trades without pagination support - Previous code was stuck in infinite while loop fetching same trades repeatedly - This prevented initial trade fetch from completing and blocked polling - Now fetches once per poll interval, allowing trades to appear in console - Changed FILTER_BTC_15M_ONLY=false to show all market types - Trades now properly detected and logged with full details --- src/account-monitor.ts | 55 ++++++++++++------------------------------ 1 file changed, 16 insertions(+), 39 deletions(-) diff --git a/src/account-monitor.ts b/src/account-monitor.ts index 1a95f63..b2db640 100644 --- a/src/account-monitor.ts +++ b/src/account-monitor.ts @@ -80,49 +80,26 @@ export class AccountMonitor { } this.lastFetchTime = Date.now(); - // Fetch all trades with pagination support + // Fetch all trades - API returns latest trades, no pagination support let allTrades: any[] = []; - let offset = 0; - const limit = 100; - let hasMore = true; - let pageCount = 0; - while (hasMore) { - try { - const response = await axios.get( - `https://data-api.polymarket.com/trades?user=${accountAddress}`, - { timeout: 10000 } - ); + try { + logger.debug(`Fetching trades from API for account: ${accountAddress}`); + const response = await axios.get( + `https://data-api.polymarket.com/trades?user=${accountAddress}`, + { timeout: 10000 } + ); - pageCount++; - - if (!Array.isArray(response.data)) { - logger.debug(`API response at offset ${offset} is not an array`); - break; - } - - if (response.data.length === 0) { - logger.debug(`No more trades at offset ${offset}`); - hasMore = false; - break; - } - - allTrades = allTrades.concat(response.data); - logger.debug(`Fetched ${response.data.length} trades from API at offset ${offset}, total: ${allTrades.length}`); - - // If we got less than limit, we've reached the end - if (response.data.length < limit) { - hasMore = false; - } else { - offset += limit; - // Small delay between pagination requests to avoid hammering API - await this.sleep(100); - } - } catch (paginationError) { - logger.warn(`Error fetching trades at offset ${offset}`, paginationError); - hasMore = false; - break; + if (!Array.isArray(response.data)) { + logger.debug(`API response is not an array`); + } else if (response.data.length > 0) { + allTrades = response.data; + logger.debug(`Fetched ${allTrades.length} trades from API`); + } else { + logger.debug('No trades fetched from API'); } + } catch (fetchError) { + logger.warn(`Error fetching trades from API`, fetchError); } // Reset error counter on successful fetch