Files
trading_bot_v3/app/api/automation/status/route.js
mindesbunister fb194f1b12 Implement working Drift leverage trading
Key Features:
-  Drift SDK v2.126.0-beta.14 integration with Helius RPC
-  User account initialization and balance reading
-  Leverage trading API with real trades executed
-  Support for SOL, BTC, ETH, APT, AVAX, BNB, MATIC, ARB, DOGE, OP
-  Transaction confirmed: gNmaWVqcE4qNK31ksoUsK6pcHqdDTaUtJXY52ZoXRF

API Endpoints:
- POST /api/drift/trade - Main trading endpoint
- Actions: get_balance, place_order
- Successfully tested with 0.01 SOL buy order at 2x leverage

Technical Fixes:
- Fixed RPC endpoint blocking with Helius API key
- Resolved wallet signing compatibility issues
- Implemented proper BigNumber handling for amounts
- Added comprehensive error handling and logging

Trading Bot Status: 🚀 FULLY OPERATIONAL with leverage trading!
2025-07-22 12:23:51 +02:00

84 lines
2.3 KiB
JavaScript

import { NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export async function GET() {
try {
// Get the latest automation session with correct data
const session = await prisma.automationSession.findFirst({
where: {
userId: 'default-user',
symbol: 'SOLUSD',
timeframe: '1h'
},
orderBy: { createdAt: 'desc' }
})
if (!session) {
return NextResponse.json({
success: true,
status: {
isActive: false,
mode: 'SIMULATION',
symbol: 'SOLUSD',
timeframe: '1h',
totalTrades: 0,
successfulTrades: 0,
winRate: 0,
totalPnL: 0,
errorCount: 0
}
})
}
// Get actual trade data to calculate real statistics
const trades = await prisma.trade.findMany({
where: {
userId: session.userId,
symbol: session.symbol
},
orderBy: { createdAt: 'desc' }
})
const completedTrades = trades.filter(t => t.status === 'COMPLETED')
const successfulTrades = completedTrades.filter(t => {
const profit = t.profit || 0
return profit > 0
})
const totalPnL = completedTrades.reduce((sum, trade) => {
const profit = trade.profit || 0
return sum + profit
}, 0)
const winRate = completedTrades.length > 0 ?
(successfulTrades.length / completedTrades.length * 100) : 0
return NextResponse.json({
success: true,
status: {
isActive: session.status === 'ACTIVE',
mode: session.mode,
symbol: session.symbol,
timeframe: session.timeframe,
totalTrades: completedTrades.length,
successfulTrades: successfulTrades.length,
winRate: Math.round(winRate * 10) / 10, // Round to 1 decimal
totalPnL: Math.round(totalPnL * 100) / 100, // Round to 2 decimals
lastAnalysis: session.lastAnalysis,
lastTrade: session.lastTrade,
nextScheduled: session.nextScheduled,
errorCount: session.errorCount,
lastError: session.lastError
}
})
} catch (error) {
console.error('Automation status error:', error)
return NextResponse.json({
success: false,
error: 'Failed to get automation status'
}, { status: 500 })
}
}