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 () => {