import { NextRequest, NextResponse } from 'next/server' import { driftTradingService } from '../../../../lib/drift-trading' export async function POST(request: NextRequest) { try { const { symbol, side, leverage } = await request.json() console.log(`📊 Calculating trade requirements for ${symbol} ${side} with ${leverage}x leverage`) // Get current account balance const balance = await driftTradingService.getAccountBalance() // Get current market price for the symbol let marketPrice = 160 // Default SOL price try { // You could get real market price here from Drift or other price feeds if (symbol === 'SOLUSD') { marketPrice = 160 // Could be fetched from oracle } else if (symbol === 'BTCUSD') { marketPrice = 65000 } else if (symbol === 'ETHUSD') { marketPrice = 3500 } } catch (priceError) { console.log('⚠️ Could not get market price, using default') } // Calculate position limits based on available collateral const availableCollateral = balance.freeCollateral || balance.availableBalance || 0 const maxLeveragedValue = availableCollateral * (leverage || 1) // Calculate max position size in tokens const maxPositionSize = marketPrice > 0 ? maxLeveragedValue / marketPrice : 0 // Calculate margin requirement for this position size const marginRequirement = maxLeveragedValue / (leverage || 1) // Calculate estimated liquidation price (simplified) const maintenanceMarginRatio = 0.05 // 5% maintenance margin let estimatedLiquidationPrice = 0 if (side.toUpperCase() === 'LONG') { estimatedLiquidationPrice = marketPrice * (1 - (1 / leverage) + maintenanceMarginRatio) } else { estimatedLiquidationPrice = marketPrice * (1 + (1 / leverage) - maintenanceMarginRatio) } const tradingCalculations = { marketPrice, availableCollateral, maxPositionSize, maxLeveragedValue, marginRequirement, estimatedLiquidationPrice, leverage: leverage || 1, symbol, side: side.toUpperCase() } console.log(`📊 Trading calculations:`, tradingCalculations) return NextResponse.json({ success: true, calculations: tradingCalculations, balance: { totalCollateral: balance.totalCollateral, freeCollateral: balance.freeCollateral, availableBalance: balance.availableBalance, marginRequirement: balance.marginRequirement, netUsdValue: balance.netUsdValue } }) } catch (error: any) { console.error('❌ Error calculating trade requirements:', error) return NextResponse.json({ success: false, error: error.message, calculations: { marketPrice: 0, availableCollateral: 0, maxPositionSize: 0, maxLeveragedValue: 0, marginRequirement: 0, estimatedLiquidationPrice: 0, leverage: 1, symbol: 'UNKNOWN', side: 'BUY' } }, { status: 500 }) } } export async function GET(request: NextRequest) { try { // Return basic trading info without specific calculations const balance = await driftTradingService.getAccountBalance() return NextResponse.json({ success: true, balance: { totalCollateral: balance.totalCollateral, freeCollateral: balance.freeCollateral, availableBalance: balance.availableBalance, marginRequirement: balance.marginRequirement, netUsdValue: balance.netUsdValue, leverage: balance.leverage, unrealizedPnl: balance.unrealizedPnl }, message: 'Account balance retrieved successfully' }) } catch (error: any) { console.error('❌ Error getting trading info:', error) return NextResponse.json({ success: false, error: error.message }, { status: 500 }) } }