diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index d0319e4..a158fcb 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -141,8 +141,8 @@ export class PositionManager { this.activeTrades.set(trade.id, trade) - // Save initial state to database - await this.saveTradeState(trade) + // Note: Initial state is saved by the API endpoint that creates the trade + // We don't save here to avoid race condition (trade may not be in DB yet) console.log(`✅ Trade added. Active trades: ${this.activeTrades.size}`) @@ -274,7 +274,15 @@ export class PositionManager { // CRITICAL: First check if on-chain position still exists // (may have been closed by TP/SL orders without us knowing) try { - const driftService = await getDriftService() + const driftService = getDriftService() + + // Skip position verification if Drift service isn't initialized yet + // (happens briefly after restart while service initializes) + if (!driftService || !(driftService as any).isInitialized) { + // Service still initializing, skip this check cycle + return + } + const marketConfig = getMarketConfig(trade.symbol) const position = await driftService.getPosition(marketConfig.driftMarketIndex) @@ -350,7 +358,12 @@ export class PositionManager { } catch (error) { // If we can't check position, continue with monitoring (don't want to false-positive) - console.error(`⚠️ Could not verify on-chain position for ${trade.symbol}:`, error) + // This can happen briefly during startup while Drift service initializes + if ((error as Error).message?.includes('not initialized')) { + // Silent - expected during initialization + } else { + console.error(`⚠️ Could not verify on-chain position for ${trade.symbol}:`, error) + } } // Update trade data