#!/usr/bin/env node // CRITICAL BUG INVESTIGATION - Paper Trading → Real Trading console.log('šŸ” CRITICAL BUG INVESTIGATION'); console.log('='.repeat(60)); console.log('Issue: Paper trading executed REAL trade'); console.log('Impact: SHORT SOL-PERP 0.03 @ $164.781 with NO STOP LOSS'); console.log(''); const fs = require('fs'); const path = require('path'); async function investigateBug() { console.log('šŸ“‹ INVESTIGATION CHECKLIST:'); console.log('='.repeat(40)); // 1. Check paper trading page for any real API calls console.log('1ļøāƒ£ Examining Paper Trading Page...'); try { const paperTradingPath = './app/paper-trading/page.js'; const paperContent = fs.readFileSync(paperTradingPath, 'utf8'); // Look for suspicious API calls const suspiciousPatterns = [ '/api/trading', '/api/drift/place-order', '/api/automation/start', 'executeTrade', 'realTrade', 'liveTrade' ]; let foundSuspiciousAPIs = []; suspiciousPatterns.forEach(pattern => { if (paperContent.includes(pattern)) { foundSuspiciousAPIs.push(pattern); } }); if (foundSuspiciousAPIs.length > 0) { console.log(' 🚨 FOUND SUSPICIOUS APIs IN PAPER TRADING:'); foundSuspiciousAPIs.forEach(api => { console.log(` - ${api}`); }); } else { console.log(' āœ… No direct live trading APIs found in paper trading page'); } // Check for analysis that might trigger automation if (paperContent.includes('runEnhancedAnalysis')) { console.log(' āš ļø Found runEnhancedAnalysis - checking what it calls...'); } } catch (error) { console.log(' āŒ Failed to read paper trading page:', error.message); } // 2. Check what runEnhancedAnalysis actually calls console.log('\n2ļøāƒ£ Examining Enhanced Analysis Workflow...'); try { const paperContent = fs.readFileSync('./app/paper-trading/page.js', 'utf8'); // Extract runEnhancedAnalysis function const analysisMatch = paperContent.match(/const runEnhancedAnalysis[\s\S]*?(?=\n const)/); if (analysisMatch) { const analysisFunction = analysisMatch[0]; // Check what API it calls if (analysisFunction.includes('/api/enhanced-screenshot')) { console.log(' šŸ“” Calls: /api/enhanced-screenshot'); if (analysisFunction.includes('analyze: true')) { console.log(' āš ļø Analysis enabled - this triggers AI analysis'); console.log(' šŸ” Need to check if enhanced-screenshot triggers automation'); } } if (analysisFunction.includes('/api/enhanced-ai-analysis')) { console.log(' šŸ“” Calls: /api/enhanced-ai-analysis'); } // Check for any trade execution if (analysisFunction.includes('executeTrade') || analysisFunction.includes('trading')) { console.log(' 🚨 FOUND TRADE EXECUTION IN ANALYSIS!'); } else { console.log(' āœ… No direct trade execution in analysis function'); } } } catch (error) { console.log(' āŒ Failed to analyze enhanced analysis:', error.message); } // 3. Check enhanced-screenshot API for automation triggers console.log('\n3ļøāƒ£ Checking Enhanced Screenshot API...'); try { const screenshotAPIPath = './app/api/enhanced-screenshot/route.js'; if (fs.existsSync(screenshotAPIPath)) { const screenshotContent = fs.readFileSync(screenshotAPIPath, 'utf8'); const automationTriggers = [ 'automation', 'executeTrade', 'trading', 'drift', 'position' ]; let foundTriggers = []; automationTriggers.forEach(trigger => { if (screenshotContent.toLowerCase().includes(trigger)) { foundTriggers.push(trigger); } }); if (foundTriggers.length > 0) { console.log(' 🚨 FOUND AUTOMATION TRIGGERS IN SCREENSHOT API:'); foundTriggers.forEach(trigger => { console.log(` - ${trigger}`); }); } else { console.log(' āœ… No automation triggers in screenshot API'); } } } catch (error) { console.log(' āŒ Failed to check screenshot API:', error.message); } // 4. Check for any background automation services console.log('\n4ļøāƒ£ Checking for Background Automation...'); const automationFiles = [ './lib/simple-automation.js', './lib/auto-trading-service.ts', './lib/enhanced-autonomous-risk-manager.js' ]; automationFiles.forEach(file => { if (fs.existsSync(file)) { console.log(` šŸ“„ Found: ${file}`); try { const content = fs.readFileSync(file, 'utf8'); if (content.includes('isRunning = true') || content.includes('this.isRunning = true')) { console.log(` āš ļø ${file} might be running automation`); } } catch (e) { console.log(` āŒ Failed to read ${file}`); } } }); // 5. Check analysis APIs that might trigger automation console.log('\n5ļøāƒ£ Checking Analysis APIs...'); const analysisAPIs = [ './app/api/ai-analysis/latest/route.js', './app/api/enhanced-ai-analysis/route.js' ]; analysisAPIs.forEach(apiFile => { if (fs.existsSync(apiFile)) { console.log(` šŸ“„ Found: ${apiFile}`); try { const content = fs.readFileSync(apiFile, 'utf8'); if (content.includes('executeTrade') || content.includes('automation')) { console.log(` 🚨 ${apiFile} might execute trades!`); } } catch (e) { console.log(` āŒ Failed to read ${apiFile}`); } } }); console.log('\nšŸ“‹ INVESTIGATION SUMMARY:'); console.log('='.repeat(40)); console.log('āœ… Container stopped - no more trades possible'); console.log('šŸ” Need to identify exact execution path'); console.log('šŸ› ļø Will create fixed paper trading system'); console.log('šŸ›”ļø Will add safeguards to prevent this bug'); console.log('\nšŸŽÆ NEXT STEPS:'); console.log('1. Create truly isolated paper trading system'); console.log('2. Add explicit paper mode flags'); console.log('3. Block all real trading APIs in paper mode'); console.log('4. Add safety checks and confirmations'); console.log('5. Test thoroughly before restarting'); } investigateBug().catch(console.error);