Fix trading modal balance calculation and improve symbol parsing

- Fixed TradeModal to send both positionSizeSOL (amount) and amountUSD in tradingData
- Improved symbol parsing with better fallbacks and enhanced logging
- Updated validation API to use amountUSD directly instead of amount * price calculation
- Resolves issue where 10 USD position was incorrectly requiring 1665 USD balance
- Enhanced error handling and debug logging for better troubleshooting
This commit is contained in:
mindesbunister
2025-07-16 16:07:54 +02:00
parent f0845d0fd1
commit ab717ea2fb

View File

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