From 674743c30f9b98d7cf66ef82a77be97ba789e541 Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Tue, 16 Dec 2025 15:58:54 +0100 Subject: [PATCH] fix: Bug #88 - Use real Prisma IDs for smart entry trades Smart entry timer was creating synthetic trade IDs (trade-${Date.now()}) instead of using real database IDs from createTrade() return value. This caused SL verification to fail with Prisma error 'No record found for update' when attempting to: - Save SL recovery signatures (attemptSLPlacement) - Mark trades as emergency closed (haltTradingAndClosePosition) Fix: - Capture createTrade() return value as savedTrade - Use savedTrade.id for ActiveTrade object - Add fallback for database save failures - Log real database ID for verification Impact: SL verification can now update smart entry trades successfully. Both active recovery and emergency shutdown will work correctly. Related: Bug #87 Phase 2 (active SL recovery) --- lib/trading/smart-entry-timer.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/trading/smart-entry-timer.ts b/lib/trading/smart-entry-timer.ts index 6d7f0f2..7a7042c 100644 --- a/lib/trading/smart-entry-timer.ts +++ b/lib/trading/smart-entry-timer.ts @@ -537,8 +537,9 @@ export class SmartEntryTimer { } // Save to database + let savedTrade try { - await createTrade({ + savedTrade = await createTrade({ positionId: openResult.transactionSignature, symbol: signal.symbol, direction: signal.direction, @@ -576,7 +577,7 @@ export class SmartEntryTimer { } }) - logger.log(`💾 Smart Entry: Trade saved to database`) + logger.log(`💾 Smart Entry: Trade saved to database (ID: ${savedTrade.id})`) } catch (dbError) { console.error(`❌ Smart Entry: Failed to save trade:`, dbError) const { logCriticalError } = await import('../utils/persistent-logger') @@ -587,6 +588,8 @@ export class SmartEntryTimer { entryPrice: fillPrice, transactionSignature: openResult.transactionSignature }) + // If database save fails, generate synthetic ID as fallback + savedTrade = { id: `trade-${Date.now()}` } as any } // Add to Position Manager @@ -600,7 +603,7 @@ export class SmartEntryTimer { ) const activeTrade: import('./position-manager').ActiveTrade = { - id: `trade-${Date.now()}`, + id: savedTrade.id, // 🔧 BUG #88 FIX: Use real Prisma ID from database positionId: openResult.transactionSignature, symbol: signal.symbol, direction: signal.direction,