From 29fce0176f7da0e7d0c2596111401c5977150e3c Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Fri, 21 Nov 2025 16:44:04 +0100 Subject: [PATCH] fix: Correct order filtering to prevent false '32 orders' count Problem: Bot reported '32 open orders' when Drift UI showed 0 orders Root Cause: Filter checked orderId > 0 but didn't verify baseAssetAmount Impact: Misleading logs suggesting ghost order accumulation Fix: Enhanced filter with proper empty slot detection: - Check orderId exists and is non-zero - Check baseAssetAmount exists and is non-zero (BN comparison) - Added logging to show: 'Found X orders (checked 32 total slots)' Result: Bot now correctly reports 0 orders when none exist Verification: Container restart shows no false positives Files: lib/drift/orders.ts (cancelAllOrders function) --- lib/drift/orders.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/drift/orders.ts b/lib/drift/orders.ts index f3a9397..6e5ecbb 100644 --- a/lib/drift/orders.ts +++ b/lib/drift/orders.ts @@ -807,12 +807,23 @@ export async function cancelAllOrders( throw new Error('User account not found') } - // Filter orders for this market (check for active orders, not just status) - // Note: Trigger orders may have different status values, so we check for non-zero orderId + // Filter orders for this market (check for TRULY active orders) + // CRITICAL: Empty slots have orderId=0 OR baseAssetAmount=0 + // Only count orders that actually exist and are open const ordersToCancel = userAccount.orders.filter( - (order: any) => - order.marketIndex === marketConfig.driftMarketIndex && - order.orderId > 0 // Active orders have orderId > 0 + (order: any) => { + // Skip if not our market + if (order.marketIndex !== marketConfig.driftMarketIndex) return false + + // Skip if orderId is 0 (empty slot) + if (!order.orderId || order.orderId === 0) return false + + // Skip if baseAssetAmount is 0 (no actual order size) + if (!order.baseAssetAmount || order.baseAssetAmount.eq(new BN(0))) return false + + // This is a real active order + return true + } ) if (ordersToCancel.length === 0) { @@ -821,6 +832,7 @@ export async function cancelAllOrders( } console.log(`📋 Found ${ordersToCancel.length} open orders to cancel (including trigger orders)`) + console.log(` (checked ${userAccount.orders.length} total order slots)`) // Cancel all orders with retry logic for rate limits const txSig = await retryWithBackoff(async () => {