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:
@@ -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}`)
|
||||
|
||||
Reference in New Issue
Block a user