critical: Emergency close unprotected positions when exit orders missing (Bug #76 recurring)
This commit is contained in:
@@ -956,9 +956,10 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
console.error(` Expected: ${expectedCount} signatures (TP1 + TP2 + ${config.useDualStops ? 'Soft SL + Hard SL' : 'SL'})`)
|
console.error(` Expected: ${expectedCount} signatures (TP1 + TP2 + ${config.useDualStops ? 'Soft SL + Hard SL' : 'SL'})`)
|
||||||
console.error(` Got: ${exitOrderSignatures.length} signatures`)
|
console.error(` Got: ${exitOrderSignatures.length} signatures`)
|
||||||
console.error(` Position is UNPROTECTED! Missing stop loss!`)
|
console.error(` Position is UNPROTECTED! Missing stop loss!`)
|
||||||
|
console.error(` ⚠️ CLOSING POSITION IMMEDIATELY FOR SAFETY`)
|
||||||
|
|
||||||
// Log to persistent file for post-mortem
|
// Log to persistent file for post-mortem
|
||||||
logCriticalError('MISSING_EXIT_ORDERS', {
|
await logCriticalError('MISSING_EXIT_ORDERS', {
|
||||||
symbol: driftSymbol,
|
symbol: driftSymbol,
|
||||||
direction: body.direction,
|
direction: body.direction,
|
||||||
entryPrice,
|
entryPrice,
|
||||||
@@ -970,7 +971,25 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
|||||||
useDualStops: config.useDualStops
|
useDualStops: config.useDualStops
|
||||||
})
|
})
|
||||||
|
|
||||||
// Continue with trade creation but flag as needing verification
|
// CRITICAL: Close the unprotected position immediately
|
||||||
|
try {
|
||||||
|
const driftService = getDriftService()
|
||||||
|
const closeResult = await driftService.closePosition(driftMarketIndex, 100)
|
||||||
|
console.log(`✅ Emergency closed unprotected position: ${closeResult}`)
|
||||||
|
} catch (closeError) {
|
||||||
|
console.error(`❌ Failed to emergency close unprotected position:`, closeError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return error response - DO NOT create trade in database
|
||||||
|
return NextResponse.json({
|
||||||
|
success: false,
|
||||||
|
error: `Missing exit orders: expected ${expectedCount}, got ${exitOrderSignatures.length}. Position emergency closed for safety.`,
|
||||||
|
details: {
|
||||||
|
entryTx: openResult.transactionSignature,
|
||||||
|
expectedOrders: expectedCount,
|
||||||
|
actualOrders: exitOrderSignatures.length
|
||||||
|
}
|
||||||
|
}, { status: 500 })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user