diff --git a/app/api/trading/validate/route.js b/app/api/trading/validate/route.js index 1fb7ae8..58aa42d 100644 --- a/app/api/trading/validate/route.js +++ b/app/api/trading/validate/route.js @@ -3,9 +3,9 @@ import { NextResponse } from 'next/server' export async function POST(request) { try { const body = await request.json() - const { symbol, side, amount, price, tradingMode = 'SPOT', fromCoin, toCoin } = body + const { symbol, side, amount, amountUSD, price, tradingMode = 'SPOT', fromCoin, toCoin } = body - console.log(`🔍 Validating trade: ${side} ${amount} ${symbol}`) + console.log(`🔍 Validating trade: ${side} ${amount} ${symbol} (USD: ${amountUSD})`) // Fetch real wallet balance from the wallet API let walletBalance @@ -42,15 +42,16 @@ export async function POST(request) { if (tradingMode === 'SPOT') { if (side.toUpperCase() === 'BUY') { - // For BUY orders, need USDC or USD equivalent - const tradePrice = price || 166.5 // Use provided price or current SOL price - requiredBalance = amount * tradePrice + // For BUY orders, use the USD amount directly (not amount * price) + requiredBalance = amountUSD || (amount * (price || 166.5)) requiredCurrency = 'USD' availableBalance = walletBalance.usdValue + + console.log(`💰 BUY validation: Need $${requiredBalance} USD, Have $${availableBalance}`) } else { - // For SELL orders, need the actual token + // For SELL orders, need the actual token amount requiredBalance = amount - requiredCurrency = fromCoin || symbol + requiredCurrency = fromCoin || symbol.replace('USD', '') // Find the token balance const tokenPosition = walletBalance.positions.find(pos => @@ -59,14 +60,16 @@ export async function POST(request) { ) availableBalance = tokenPosition ? tokenPosition.amount : walletBalance.solBalance + console.log(`💰 SELL validation: Need ${requiredBalance} ${requiredCurrency}, Have ${availableBalance}`) } } else if (tradingMode === 'PERP') { // For perpetuals, only need margin const leverage = 10 // Default leverage - const tradePrice = price || 166.5 - requiredBalance = (amount * tradePrice) / leverage + requiredBalance = (amountUSD || (amount * (price || 166.5))) / leverage requiredCurrency = 'USD' availableBalance = walletBalance.usdValue + + console.log(`💰 PERP validation: Need $${requiredBalance} USD margin, Have $${availableBalance}`) } console.log(`💰 Balance check: Need ${requiredBalance} ${requiredCurrency}, Have ${availableBalance}`)