wip: Emergency order restoration endpoint (has singleton issues)
- Created /api/trading/place-exit-orders endpoint - Created restore-orders.mjs script - Issue: Next.js creates separate Drift instances per route - Workaround: Use /api/trading/cancel-orders to remove orphaned orders Current situation: - 32 orphaned orders existed and were cancelled - Position Manager should auto-place new orders - Manual order placement endpoint needs refactoring
This commit is contained in:
75
scripts/restore-orders.mjs
Normal file
75
scripts/restore-orders.mjs
Normal file
@@ -0,0 +1,75 @@
|
||||
#!/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)
|
||||
})
|
||||
Reference in New Issue
Block a user