From 27f78748cf42f901d0552608685f051f1e58c308 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Tue, 28 Oct 2025 20:00:17 +0100 Subject: [PATCH] Fix: Initialize Drift service before cancelling orders - cancelAllOrders() now calls initializeDriftService() if service not initialized - Prevents 'Drift service not initialized' error when Position Manager tries to cancel orphaned orders - Ensures order cleanup works correctly after external position closures --- lib/drift/orders.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/drift/orders.ts b/lib/drift/orders.ts index 29ec2de..794eb12 100644 --- a/lib/drift/orders.ts +++ b/lib/drift/orders.ts @@ -4,7 +4,7 @@ * Handles opening and closing positions with market orders */ -import { getDriftService } from './client' +import { getDriftService, initializeDriftService } from './client' import { getMarketConfig } from '../../config/trading' import BN from 'bn.js' import { @@ -541,7 +541,13 @@ export async function cancelAllOrders( try { console.log(`🗑️ Cancelling all orders for ${symbol}...`) - const driftService = getDriftService() + // Ensure Drift service is initialized + let driftService = getDriftService() + if (!driftService) { + console.log('⚠️ Drift service not initialized, initializing now...') + driftService = await initializeDriftService() + } + const driftClient = driftService.getClient() const marketConfig = getMarketConfig(symbol)