"use client" import React, { useState, useEffect, ChangeEvent } from 'react' interface TradeModalProps { isOpen: boolean onClose: () => void tradeData: { entry: string tp: string tp2?: string sl: string symbol?: string timeframe?: string risk?: string reward?: string action?: 'BUY' | 'SELL' } | null onExecute: (data: any) => void } interface FormData { entry: string tp1: string tp2: string sl: string positionValue: string leverage: number tradingCoin: string tp1Percentage: number tp2Percentage: number } const supportedCoins = [ { symbol: 'SOL', name: 'Solana', icon: '◎', price: 159.5 }, { symbol: 'USDC', name: 'USD Coin', icon: '$', price: 1.0 } ] export default function TradeModal({ isOpen, onClose, tradeData, onExecute }: TradeModalProps) { console.log('🚀 TradeModal loaded with enhanced features - Version 2.0') const [loading, setLoading] = useState(false) const [walletBalance, setWalletBalance] = useState(null) const [formData, setFormData] = useState({ entry: tradeData?.entry || '', tp1: tradeData?.tp || '', tp2: '', sl: tradeData?.sl || '', positionValue: '64', // USD amount for position size leverage: 3, tradingCoin: 'SOL', tp1Percentage: 50, // % of position to close at TP1 tp2Percentage: 50 // % of position to close at TP2 }) // Fetch wallet balance when modal opens useEffect(() => { if (isOpen) { fetchWalletBalance() } }, [isOpen]) // Update form data when tradeData changes useEffect(() => { if (tradeData) { console.log('🔄 TradeModal updating form with new tradeData:', tradeData) setFormData(prev => ({ ...prev, entry: tradeData.entry || '', tp1: tradeData.tp || '', tp2: tradeData.tp2 || '', sl: tradeData.sl || '', tradingCoin: tradeData.symbol ? tradeData.symbol.replace('USD', '') : 'SOL' })) } }, [tradeData]) const fetchWalletBalance = async () => { try { const response = await fetch('/api/wallet/balance') const data = await response.json() setWalletBalance(data) } catch (error) { console.error('Failed to fetch wallet balance:', error) setWalletBalance({ solBalance: 2.5, usdcBalance: 150.0 }) // Fallback } } // Helper function to safely format numbers const formatNumber = (value: number, decimals: number = 2): string => { return isNaN(value) ? '0.00' : value.toFixed(decimals) } // Calculate various metrics const currentCoin = supportedCoins.find(coin => coin.symbol === formData.tradingCoin) const coinPrice = currentCoin?.price || 159.5 // Default to SOL price // Position calculations const positionValueUSD = parseFloat(formData.positionValue) || 0 const positionSizeSOL = positionValueUSD / coinPrice // Calculate SOL amount from USD const leverageNum = formData.leverage || 1 const entryPrice = parseFloat(formData.entry) || 0 const tp1Price = parseFloat(formData.tp1) || 0 const tp2Price = parseFloat(formData.tp2) || 0 const slPrice = parseFloat(formData.sl) || 0 // Profit calculations const leveragedValue = positionValueUSD * leverageNum // TP1 Profit calculation (percentage-based) const profitTP1 = (entryPrice > 0 && tp1Price > 0 && leveragedValue > 0) ? ((tp1Price - entryPrice) / entryPrice) * leveragedValue * (formData.tp1Percentage / 100) : 0 // TP2 Profit calculation (percentage-based) const profitTP2 = (entryPrice > 0 && tp2Price > 0 && leveragedValue > 0) ? ((tp2Price - entryPrice) / entryPrice) * leveragedValue * (formData.tp2Percentage / 100) : 0 // Stop Loss calculation const lossAtSL = (entryPrice > 0 && slPrice > 0 && leveragedValue > 0) ? ((slPrice - entryPrice) / entryPrice) * leveragedValue : 0 const totalPotentialProfit = profitTP1 + profitTP2 if (!isOpen) return null const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) try { console.log('🎯 Executing trade with data:', formData) const tradingData = { ...formData, positionSizeSOL: formatNumber(positionSizeSOL, 4), leveragedValue: formatNumber(leveragedValue, 2), profitTP1: formatNumber(profitTP1, 2), profitTP2: formatNumber(profitTP2, 2), totalProfit: formatNumber(totalPotentialProfit, 2), lossAtSL: formatNumber(lossAtSL, 2) } await onExecute(tradingData) onClose() } catch (error) { console.error('Trade execution failed:', error) } finally { setLoading(false) } } const setPositionPercentage = (percentage: number) => { if (walletBalance && walletBalance.solBalance) { const availableBalance = walletBalance.solBalance * coinPrice // Convert SOL to USD const newPosition = (availableBalance * percentage / 100).toFixed(0) setFormData(prev => ({ ...prev, positionValue: newPosition })) } } return (
{/* Header */}

Execute Trade

Configure your position details

{/* Coin Selection */}
{supportedCoins.map((coin) => ( ))}
{/* Position Size */}
{walletBalance && ( Available: ${formatNumber(walletBalance.solBalance * coinPrice, 2)} )}
setFormData(prev => ({ ...prev, positionValue: e.target.value }))} className="w-full px-4 py-3 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:border-blue-500 transition-colors" placeholder="Enter USD amount" /> {/* Quick percentage buttons */}
{[25, 50, 75, 100].map((percent) => ( ))}
{/* Position info */}
Position Size: {formatNumber(positionSizeSOL, 4)} {formData.tradingCoin}
USD Value: ${formatNumber(positionValueUSD, 2)}
{/* Leverage */}
{formData.leverage}x
setFormData(prev => ({ ...prev, leverage: parseInt(e.target.value) }))} className="w-full h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer range-slider" />
1x 5x 10x
Leveraged Value: ${formatNumber(leveragedValue, 2)}
{/* Price Inputs Grid */}
setFormData(prev => ({ ...prev, entry: e.target.value }))} className="w-full px-3 py-2 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:border-blue-500" placeholder="0.00" />
setFormData(prev => ({ ...prev, sl: e.target.value }))} className="w-full px-3 py-2 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:border-red-500" placeholder="0.00" />
{/* Take Profit 1 */}

Take Profit 1

+${formatNumber(profitTP1, 2)}
setFormData(prev => ({ ...prev, tp1: e.target.value }))} className="w-full px-3 py-2 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:border-green-500" placeholder="TP1 Price" />
Allocation: setFormData(prev => ({ ...prev, tp1Percentage: parseInt(e.target.value) }))} className="flex-1 h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer" /> {formData.tp1Percentage}%
{/* Take Profit 2 */}

Take Profit 2

+${formatNumber(profitTP2, 2)}
setFormData(prev => ({ ...prev, tp2: e.target.value }))} className="w-full px-3 py-2 bg-gray-800 border border-gray-600 rounded-lg text-white placeholder-gray-400 focus:outline-none focus:border-green-500" placeholder="TP2 Price" />
Allocation: setFormData(prev => ({ ...prev, tp2Percentage: parseInt(e.target.value) }))} className="flex-1 h-2 bg-gray-700 rounded-lg appearance-none cursor-pointer" /> {formData.tp2Percentage}%
{/* Profit Summary */}

Profit Summary

TP1 Profit: +${formatNumber(profitTP1, 2)}
TP2 Profit: +${formatNumber(profitTP2, 2)}
Total Profit: +${formatNumber(totalPotentialProfit, 2)}
Max Loss: ${formatNumber(Math.abs(lossAtSL), 2)}
{/* Execute Button */}
) }