#!/usr/bin/env node // CRITICAL BUG INVESTIGATION & EMERGENCY PROTECTION console.log('🚨 CRITICAL BUG INVESTIGATION'); console.log('='.repeat(50)); console.log('Problem: Paper trading triggered REAL trade'); console.log('Status: SHORT SOL-PERP 0.03 @ $164.781 with NO STOP LOSS'); console.log('Risk: UNLIMITED LOSS if price goes up'); console.log(''); async function emergencyInvestigation() { const baseUrl = 'http://localhost:9001'; try { // 1. Immediate position status console.log('šŸ“Š CURRENT POSITION STATUS:'); const posResponse = await fetch(`${baseUrl}/api/drift/positions`); if (posResponse.ok) { const posData = await posResponse.json(); if (posData.positions?.length > 0) { const pos = posData.positions[0]; console.log(` Symbol: ${pos.symbol}`); console.log(` Side: ${pos.side.toUpperCase()}`); console.log(` Size: ${pos.size}`); console.log(` Entry: $${pos.entryPrice}`); console.log(` Current: $${pos.markPrice}`); console.log(` PnL: $${pos.unrealizedPnl.toFixed(4)}`); // Calculate risk exposure const riskExposure = pos.notionalValue; console.log(` RISK EXPOSURE: $${riskExposure.toFixed(2)}`); if (pos.side === 'short' && pos.markPrice > pos.entryPrice) { console.log(' 🚨 LOSING MONEY: Price moved against short position'); } } } // 2. Check for any protection orders console.log('\nšŸ›”ļø PROTECTION ORDERS:'); const ordersResponse = await fetch(`${baseUrl}/api/drift/orders`); if (ordersResponse.ok) { const ordersData = await ordersResponse.json(); const openOrders = ordersData.orders?.filter(o => o.status === 'OPEN') || []; if (openOrders.length === 0) { console.log(' āŒ NO STOP LOSS OR TAKE PROFIT ORDERS!'); console.log(' 🚨 POSITION IS COMPLETELY UNPROTECTED!'); } else { openOrders.forEach(order => { console.log(` Order: ${order.orderType} ${order.side} @ $${order.triggerPrice || order.price}`); }); } } // 3. Account balance check console.log('\nšŸ’° ACCOUNT STATUS:'); try { const balanceResponse = await fetch(`${baseUrl}/api/drift/account`); if (balanceResponse.ok) { const balanceData = await balanceResponse.json(); console.log(` Available: $${balanceData.availableBalance || 'Unknown'}`); console.log(` Total Collateral: $${balanceData.totalCollateral || 'Unknown'}`); } else { console.log(' āš ļø Cannot fetch account balance'); } } catch (e) { console.log(' āš ļø Account balance check failed'); } } catch (error) { console.log('āŒ Investigation failed:', error.message); } console.log('\nšŸ” POSSIBLE CAUSES OF THE BUG:'); console.log('1. Paper trading page accidentally called live trading API'); console.log('2. Background automation system still running despite "disable"'); console.log('3. Position monitor triggered unexpected trade execution'); console.log('4. API routing bug - paper calls went to live endpoint'); console.log('5. Some other automation script running independently'); console.log('\nšŸ†˜ IMMEDIATE ACTIONS NEEDED:'); console.log('1. šŸ›”ļø PLACE STOP LOSS IMMEDIATELY (manually in Drift app)'); console.log(' - Recommended: $168.08 (2% above entry $164.781)'); console.log(' - This limits loss to ~$0.10 instead of unlimited'); console.log(''); console.log('2. šŸ” INVESTIGATE THE BUG SOURCE'); console.log(' - Check what API was actually called'); console.log(' - Verify no background automation running'); console.log(' - Find the exact execution path'); console.log(''); console.log('3. 🚫 DISABLE ALL TRADING COMPLETELY'); console.log(' - Stop container if needed'); console.log(' - Verify no automation can trigger'); console.log(' - Fix the bug before any more trading'); console.log(''); console.log('4. šŸ“Š MONITOR POSITION CLOSELY'); console.log(' - Watch SOL price movement'); console.log(' - Be ready to close manually if needed'); console.log(' - Don\'t let losses compound'); console.log('\nāš ļø CRITICAL: This is a SEVERE bug that could cause major losses!'); console.log('Paper trading should NEVER execute real trades!'); console.log('System must be fixed immediately before any further use!'); } emergencyInvestigation().catch(console.error);