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
This commit is contained in:
mindesbunister
2025-10-28 20:00:17 +01:00
parent 715fa8bd11
commit 27f78748cf

View File

@@ -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)