#!/usr/bin/env node /** * Drift Order Cleanup Management Script * Easy commands to manage the automated cleanup service */ const { driftOrderCleanupService } = require('./lib/drift-order-cleanup-service.js') const commands = { status: async () => { console.log('๐Ÿ“Š Drift Order Cleanup Status') console.log('============================') const status = driftOrderCleanupService.getStatus() console.log(`Running: ${status.isRunning ? 'โœ… YES' : 'โŒ NO'}`) if (status.lastCleanupTime > 0) { const ago = Math.floor((Date.now() - status.lastCleanupTime) / 1000) console.log(`Last cleanup: ${ago}s ago`) } else { console.log('Last cleanup: Never') } if (status.cooldownRemaining > 0) { console.log(`Cooldown: ${Math.floor(status.cooldownRemaining / 1000)}s remaining`) } // Get current positions and orders try { const [positionsRes, ordersRes] = await Promise.all([ fetch('http://localhost:9001/api/drift/positions'), fetch('http://localhost:9001/api/drift/orders') ]) if (positionsRes.ok && ordersRes.ok) { const positions = await positionsRes.json() const orders = await ordersRes.json() console.log(`\nCurrent: ${positions.positions?.length || 0} positions, ${orders.orders?.length || 0} orders`) if (positions.positions?.length > 0) { console.log('\nActive Positions:') positions.positions.forEach(pos => { console.log(` ๐Ÿ“ˆ ${pos.symbol}: ${pos.size > 0 ? 'LONG' : 'SHORT'} ${Math.abs(pos.size)} ($${pos.value?.toFixed(2) || 'N/A'})`) }) } if (orders.orders?.length > 0) { console.log('\nActive Orders:') orders.orders.forEach(order => { console.log(` ๐Ÿ“‹ ${order.symbol}: ${order.side} ${order.size} @ $${order.price} (${order.orderType})`) }) } } } catch (error) { console.log('โš ๏ธ Could not fetch current positions/orders') } }, start: () => { console.log('๐Ÿš€ Starting automated cleanup service...') driftOrderCleanupService.start() console.log('โœ… Service started! It will check for orphaned orders every 60 seconds.') }, stop: () => { console.log('๐Ÿ›‘ Stopping automated cleanup service...') driftOrderCleanupService.stop() console.log('โœ… Service stopped.') }, cleanup: async () => { console.log('๐Ÿงน Running manual cleanup...') try { const result = await driftOrderCleanupService.forceCleanup() console.log('\n๐Ÿ“Š Cleanup Results:') console.log(` Positions: ${result.summary.activePositions}`) console.log(` Orders: ${result.summary.activeOrders}`) console.log(` Orphaned: ${result.summary.orphanedOrders}`) console.log(` Conflicting: ${result.summary.conflictingOrders}`) console.log(` โœ… Canceled: ${result.summary.totalCanceled}`) console.log(` โŒ Failed: ${result.summary.totalFailed}`) if (result.canceledOrders?.length > 0) { console.log('\nCanceled Orders:') result.canceledOrders.forEach(order => { if (order.success) { console.log(` โœ… ${order.symbol} order ${order.orderId} (${order.reason})`) } else { console.log(` โŒ Order ${order.orderId}: ${order.error}`) } }) } } catch (error) { console.error('โŒ Cleanup failed:', error.message) } }, help: () => { console.log('๐Ÿงน Drift Order Cleanup Commands') console.log('===============================') console.log('') console.log('Commands:') console.log(' status - Show service status and current positions/orders') console.log(' start - Start automated cleanup monitoring') console.log(' stop - Stop automated cleanup monitoring') console.log(' cleanup - Run manual cleanup now') console.log(' help - Show this help') console.log('') console.log('Examples:') console.log(' node drift-cleanup-manager.js status') console.log(' node drift-cleanup-manager.js start') console.log(' node drift-cleanup-manager.js cleanup') console.log('') console.log('What it does:') console.log('โ€ข Detects orphaned orders (orders for markets with no position)') console.log('โ€ข Finds conflicting reduce-only orders') console.log('โ€ข Automatically cancels problematic orders') console.log('โ€ข Prevents manual order management after SL/TP hits') } } async function main() { const command = process.argv[2] || 'help' if (commands[command]) { await commands[command]() } else { console.log(`โŒ Unknown command: ${command}`) console.log('Run "node drift-cleanup-manager.js help" for available commands') } } if (require.main === module) { main().catch(console.error) } module.exports = { commands }