Files
trading_bot_v3/app/api/drift/realtime-monitoring/route.ts
mindesbunister 19d4020622 feat: Implement real-time monitoring for Drift trading
 Features Added:
- Real-time event subscription using Drift SDK EventSubscriber
- Periodic fallback monitoring for position changes
- Interactive UI controls for starting/stopping monitoring
- Comprehensive data source status tracking
- Multi-source trade aggregation and deduplication

🔧 Backend Implementation:
- EventSubscriber integration with OrderActionRecord events
- Fallback to periodic monitoring (30s intervals) if events fail
- Real-time trade cache management (last 100 trades)
- Enhanced data availability status with monitoring state
- Improved trade history from 5+ different API sources

🎨 Frontend Enhancements:
- Live monitoring toggle button (🔴 Start Live / 🟢 Live)
- Real-time status panel showing active monitoring state
- Trade counter and last activity timestamps
- Clear cache functionality for real-time trades
- Enhanced status modal with monitoring details

🔗 API Endpoints:
- POST /api/drift/realtime-monitoring - Control monitoring
- GET /api/drift/realtime-monitoring - Check status
- GET /api/drift/data-status - Enhanced with monitoring state

🐳 Docker Integration:
- Updated container configuration for persistent monitoring
- Environment variable support for real-time features
- Database persistence for captured trades

💾 Database & Storage:
- Automatic storage of real-time detected trades
- Deduplication logic to prevent synthetic/duplicate trades
- Persistent cache across container restarts

🚀 Usage:
- Click 'Start Live' button in Trading History panel
- Monitor will attempt EventSubscriber, fallback to periodic checks
- All future trades automatically captured and stored
- Status panel shows monitoring state and trade statistics

This implements comprehensive real-time trading monitoring for Drift Protocol with robust fallback mechanisms and professional UI integration.
2025-07-13 13:29:10 +02:00

91 lines
2.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { driftTradingService } from '../../../../lib/drift-trading'
export async function POST(request: NextRequest) {
try {
const { action } = await request.json()
if (action === 'start') {
console.log('🚀 Starting real-time monitoring...')
const result = await driftTradingService.startRealtimeMonitoring()
if (result.success) {
return NextResponse.json({
success: true,
message: 'Real-time monitoring started successfully',
status: driftTradingService.getRealtimeMonitoringStatus()
})
} else {
return NextResponse.json({
success: false,
error: result.error,
message: 'Failed to start real-time monitoring'
}, { status: 500 })
}
} else if (action === 'stop') {
console.log('🛑 Stopping real-time monitoring...')
await driftTradingService.stopRealtimeMonitoring()
return NextResponse.json({
success: true,
message: 'Real-time monitoring stopped',
status: driftTradingService.getRealtimeMonitoringStatus()
})
} else if (action === 'status') {
const status = driftTradingService.getRealtimeMonitoringStatus()
return NextResponse.json({
success: true,
status,
message: status.isActive ? 'Real-time monitoring is active' : 'Real-time monitoring is not active'
})
} else if (action === 'clear') {
driftTradingService.clearRealtimeTradesCache()
return NextResponse.json({
success: true,
message: 'Real-time trades cache cleared',
status: driftTradingService.getRealtimeMonitoringStatus()
})
} else {
return NextResponse.json({
success: false,
error: 'Invalid action. Use: start, stop, status, or clear'
}, { status: 400 })
}
} catch (error: any) {
console.error('❌ Error in realtime monitoring endpoint:', error)
return NextResponse.json({
success: false,
error: error.message,
message: 'Internal server error'
}, { status: 500 })
}
}
export async function GET(request: NextRequest) {
try {
const status = driftTradingService.getRealtimeMonitoringStatus()
return NextResponse.json({
success: true,
status,
message: status.isActive ? 'Real-time monitoring is active' : 'Real-time monitoring is not active'
})
} catch (error: any) {
console.error('❌ Error getting monitoring status:', error)
return NextResponse.json({
success: false,
error: error.message
}, { status: 500 })
}
}