#!/usr/bin/env node /** * Emergency script to restore on-chain exit orders * Use when orders vanish but Position Manager is still tracking */ import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() async function restoreOrders() { console.log('šŸ” Checking for open trades without orders...') const openTrades = await prisma.trade.findMany({ where: { exitReason: null }, }) if (openTrades.length === 0) { console.log('āœ… No open trades found') return } for (const trade of openTrades) { console.log(`\nšŸ“Š Trade: ${trade.symbol} ${trade.direction} @ $${trade.entryPrice}`) console.log(` TP1: $${trade.takeProfit1Price}`) console.log(` TP2: $${trade.takeProfit2Price}`) console.log(` SL: $${trade.stopLossPrice}`) console.log(` Size: $${trade.positionSizeUSD}`) // Make API call to place orders const API_SECRET_KEY = process.env.API_SECRET_KEY if (!API_SECRET_KEY) { console.error('āŒ API_SECRET_KEY not set in environment') process.exit(1) } const response = await fetch('http://localhost:3001/api/trading/place-exit-orders', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${API_SECRET_KEY}` }, body: JSON.stringify({ symbol: trade.symbol, direction: trade.direction, entryPrice: trade.entryPrice, tp1Price: trade.takeProfit1Price, tp2Price: trade.takeProfit2Price, slPrice: trade.stopLossPrice, positionSizeUSD: trade.positionSizeUSD, tp1SizePercent: 75, tp2SizePercent: 0, // TP2-as-runner }) }) const result = await response.json() if (result.success) { console.log('āœ… Orders placed successfully!') console.log(` Signatures: ${result.signatures?.slice(0, 2).join(', ')}...`) } else { console.error('āŒ Failed to place orders:', result.error) } } } restoreOrders() .then(() => { console.log('\nāœ… Done') process.exit(0) }) .catch(error => { console.error('āŒ Error:', error) process.exit(1) })