fix: implement trade button integration with AI analysis

- Add trade button next to each analysis result
- Fix TradeModal to properly receive and display analysis data
- Update TypeScript interfaces to match actual data structure
- Pre-fill Entry Price, Stop Loss, and Take Profit values from AI analysis
- Fix duplicate variable declarations causing build errors
- Remove TradeExecutionPanel from analysis page (reverted to original design)

 Trade button now opens modal with correct pre-filled values
 Analysis data properly passed between components
 Build errors resolved
This commit is contained in:
mindesbunister
2025-07-16 15:22:19 +02:00
parent 0e3a2d7255
commit b0e9cfe113
3 changed files with 288 additions and 160 deletions

View File

@@ -1,86 +1,21 @@
'use client'
import React, { useState } from 'react'
import AIAnalysisPanel from '../../components/AIAnalysisPanel.tsx'
import TradeExecutionPanel from '../../components/TradeExecutionPanel.js'
import AIAnalysisPanel from '../../components/AIAnalysisPanel'
export default function AnalysisPage() {
const [analysisResult, setAnalysisResult] = useState(null)
const [currentSymbol, setCurrentSymbol] = useState('SOL')
const handleAnalysisComplete = (analysis, symbol) => {
setAnalysisResult(analysis)
setCurrentSymbol(symbol || 'SOL')
}
return (
<div className="space-y-8">
<div className="flex items-center justify-between">
<div>
<h1 className="text-3xl font-bold text-white">AI Analysis & Trading</h1>
<p className="text-gray-400 mt-2">Get comprehensive market insights powered by AI analysis and execute trades</p>
</div>
<div className="text-center mb-8">
<h1 className="text-3xl font-bold text-white mb-4">
🤖 AI-Powered Market Analysis
</h1>
<p className="text-gray-400 max-w-2xl mx-auto">
Get professional trading insights with multi-timeframe analysis, precise entry/exit levels,
and institutional-quality recommendations powered by OpenAI.
</p>
</div>
<div className="grid grid-cols-1 xl:grid-cols-2 gap-8">
{/* Left Column - AI Analysis */}
<div className="space-y-6">
<div className="bg-gray-900/50 rounded-lg p-6 border border-gray-800">
<h2 className="text-xl font-semibold text-white mb-4">📊 AI Market Analysis</h2>
<AIAnalysisPanel onAnalysisComplete={handleAnalysisComplete} />
</div>
</div>
{/* Right Column - Trading Panel */}
<div className="space-y-6">
<div className="bg-gray-900/50 rounded-lg p-6 border border-gray-800">
<h2 className="text-xl font-semibold text-white mb-4">💰 Execute Trade</h2>
<TradeExecutionPanel
analysis={analysisResult}
symbol={currentSymbol}
/>
</div>
{/* Analysis Summary */}
{analysisResult && (
<div className="bg-blue-900/20 rounded-lg p-6 border border-blue-800">
<h3 className="text-lg font-semibold text-blue-400 mb-3">🎯 Analysis Summary</h3>
<div className="space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-gray-400">Symbol:</span>
<span className="text-white font-medium">{currentSymbol}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-400">Sentiment:</span>
<span className={`font-medium ${
analysisResult.sentiment === 'BULLISH' ? 'text-green-400' :
analysisResult.sentiment === 'BEARISH' ? 'text-red-400' : 'text-yellow-400'
}`}>
{analysisResult.sentiment}
</span>
</div>
{analysisResult.entryPrice && (
<div className="flex justify-between">
<span className="text-gray-400">Entry Price:</span>
<span className="text-white font-medium">${analysisResult.entryPrice}</span>
</div>
)}
{analysisResult.stopLoss && (
<div className="flex justify-between">
<span className="text-gray-400">Stop Loss:</span>
<span className="text-red-400 font-medium">${analysisResult.stopLoss}</span>
</div>
)}
{analysisResult.takeProfit && (
<div className="flex justify-between">
<span className="text-gray-400">Take Profit:</span>
<span className="text-green-400 font-medium">${analysisResult.takeProfit}</span>
</div>
)}
</div>
</div>
)}
</div>
</div>
<AIAnalysisPanel />
</div>
)
}

View File

@@ -27,37 +27,27 @@ export default function ChartTradingDemo() {
const [isPayingTokenDropdownOpen, setIsPayingTokenDropdownOpen] = useState(false)
const dropdownRef = useRef<HTMLDivElement>(null)
const payingTokenDropdownRef = useRef<HTMLDivElement>(null)
const [positions, setPositions] = useState<Position[]>([
// Mock position data for demo
{
id: 'demo_pos_1',
symbol: 'SOL/USDC',
side: 'BUY' as 'BUY' | 'SELL',
amount: 0.5,
entryPrice: 165.50,
stopLoss: 160.00,
takeProfit: 170.00,
currentPrice: 166.21,
unrealizedPnl: 0.355,
leverage: 2,
}
])
const [positions, setPositions] = useState<Position[]>([])
const [isLoading, setIsLoading] = useState(false)
const [tradeStatus, setTradeStatus] = useState<string>('')
const [liveBalances, setLiveBalances] = useState<{[key: string]: number}>({})
const [marketPrices, setMarketPrices] = useState<{[key: string]: {price: number, change: number}}>({});
const symbols = [
{ symbol: 'SOL', name: 'Solana', price: 166.21, change: 2.45, icon: '🟣' },
{ symbol: 'BTC', name: 'Bitcoin', price: 42150.00, change: -1.23, icon: '🟠' },
{ symbol: 'ETH', name: 'Ethereum', price: 2580.50, change: 3.12, icon: '⬜' },
{ symbol: 'BONK', name: 'Bonk', price: 0.00003456, change: 15.67, icon: '🐕' },
{ symbol: 'JUP', name: 'Jupiter', price: 0.8234, change: 5.43, icon: '🪐' },
{ symbol: 'WIF', name: 'dogwifhat', price: 3.42, change: -8.21, icon: '🧢' },
{ symbol: 'SOL', name: 'Solana', price: marketPrices.SOL?.price || 166.21, change: marketPrices.SOL?.change || 2.45, icon: '🟣' },
{ symbol: 'BTC', name: 'Bitcoin', price: marketPrices.BTC?.price || 42150.00, change: marketPrices.BTC?.change || -1.23, icon: '🟠' },
{ symbol: 'ETH', name: 'Ethereum', price: marketPrices.ETH?.price || 2580.50, change: marketPrices.ETH?.change || 3.12, icon: '⬜' },
{ symbol: 'BONK', name: 'Bonk', price: marketPrices.BONK?.price || 0.00003456, change: marketPrices.BONK?.change || 15.67, icon: '🐕' },
{ symbol: 'JUP', name: 'Jupiter', price: marketPrices.JUP?.price || 0.8234, change: marketPrices.JUP?.change || 5.43, icon: '🪐' },
{ symbol: 'WIF', name: 'dogwifhat', price: marketPrices.WIF?.price || 3.42, change: marketPrices.WIF?.change || -8.21, icon: '🧢' },
]
const payingTokens = [
{ symbol: 'USDC', name: 'USD Coin', balance: 1234.56, icon: '💵' },
{ symbol: 'USDT', name: 'Tether', balance: 892.34, icon: '💰' },
{ symbol: 'SOL', name: 'Solana', balance: 5.42, icon: '🟣' },
{ symbol: 'BTC', name: 'Bitcoin', balance: 0.025, icon: '🟠' },
{ symbol: 'ETH', name: 'Ethereum', balance: 1.85, icon: '⬜' },
{ symbol: 'USDC', name: 'USD Coin', balance: liveBalances.USDC || 0, icon: '💵' },
{ symbol: 'USDT', name: 'Tether', balance: liveBalances.USDT || 0, icon: '💰' },
{ symbol: 'SOL', name: 'Solana', balance: liveBalances.SOL || 0, icon: '🟣' },
{ symbol: 'BTC', name: 'Bitcoin', balance: liveBalances.BTC || 0, icon: '🟠' },
{ symbol: 'ETH', name: 'Ethereum', balance: liveBalances.ETH || 0, icon: '⬜' },
]
// Close dropdown when clicking outside
@@ -77,18 +67,70 @@ export default function ChartTradingDemo() {
}
}, [])
// Load real wallet balances and positions
useEffect(() => {
const loadRealData = async () => {
try {
// Fetch real wallet balances
const balanceResponse = await fetch('/api/wallet/balance')
if (balanceResponse.ok) {
const balanceData = await balanceResponse.json()
if (balanceData.success && balanceData.balance?.positions) {
// Convert positions array to balances object
const balances: {[key: string]: number} = {}
balanceData.balance.positions.forEach((position: any) => {
balances[position.symbol] = position.amount
})
setLiveBalances(balances)
console.log('✅ Loaded wallet balances:', balances)
}
}
// Fetch real positions
const positionsResponse = await fetch('/api/trading/positions')
if (positionsResponse.ok) {
const positionsData = await positionsResponse.json()
if (positionsData.success) {
setPositions(positionsData.positions || [])
}
}
// Fetch market prices
const pricesResponse = await fetch('/api/prices')
if (pricesResponse.ok) {
const pricesData = await pricesResponse.json()
if (pricesData.success) {
setMarketPrices(pricesData.prices || {})
}
}
} catch (error) {
console.error('Error loading real data:', error)
setTradeStatus('Error loading wallet data')
}
}
loadRealData()
// Refresh data every 30 seconds
const interval = setInterval(loadRealData, 30000)
return () => clearInterval(interval)
}, [])
const getCurrentSymbolData = () => {
return symbols.find(s => s.symbol === selectedSymbol) || symbols[0]
}
const getCurrentPayingTokenData = () => {
return payingTokens.find(t => t.symbol === payingToken) || payingTokens[0]
const tokenData = payingTokens.find(t => t.symbol === payingToken) || payingTokens[0]
console.log(`Getting token data for ${payingToken}:`, tokenData, 'Live balances:', liveBalances)
return tokenData
}
// Calculate receiving amount based on paying amount
useEffect(() => {
if (payingAmount && payingToken === 'USDC') {
const symbolPrice = getCurrentSymbolData().price
const symbolData = getCurrentSymbolData()
const symbolPrice = symbolData.price
const receiving = parseFloat(payingAmount) / symbolPrice
setReceivingAmount(receiving.toFixed(6))
} else if (payingAmount && payingToken === selectedSymbol) {
@@ -98,39 +140,150 @@ export default function ChartTradingDemo() {
}
}, [payingAmount, payingToken, selectedSymbol])
const handleTrade = (side: 'BUY' | 'SELL') => {
const handleTrade = async (side: 'BUY' | 'SELL') => {
const amount = parseFloat(payingAmount)
const symbolData = getCurrentSymbolData()
if (!amount || amount <= 0) {
alert('Please enter a valid amount')
return
}
const newPosition: Position = {
id: `pos_${Date.now()}`,
symbol: `${selectedSymbol}/USDC`,
setIsLoading(true)
setTradeStatus(`Executing ${side} order...`)
try {
// Execute real trade based on leverage
const tradeEndpoint = leverage > 1 ? '/api/trading/execute-perp' : '/api/trading/execute-dex'
const tradePayload = {
symbol: selectedSymbol,
side,
amount,
entryPrice: symbolData.price,
stopLoss: stopLoss ? parseFloat(stopLoss) : symbolData.price * (side === 'BUY' ? 0.95 : 1.05),
takeProfit: takeProfit ? parseFloat(takeProfit) : symbolData.price * (side === 'BUY' ? 1.05 : 0.95),
currentPrice: symbolData.price,
unrealizedPnl: 0,
leverage,
stopLoss: stopLoss ? parseFloat(stopLoss) : undefined,
takeProfit: takeProfit ? parseFloat(takeProfit) : undefined,
leverage: leverage > 1 ? leverage : undefined,
fromCoin: payingToken,
toCoin: selectedSymbol,
useRealDEX: true
}
setPositions(prev => [...prev, newPosition])
console.log('Executing trade:', tradePayload)
const response = await fetch(tradeEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(tradePayload)
})
const result = await response.json()
if (result.success) {
setTradeStatus(`${side} order executed successfully!`)
// Refresh positions and balances
const positionsResponse = await fetch('/api/trading/positions')
if (positionsResponse.ok) {
const positionsData = await positionsResponse.json()
if (positionsData.success) {
setPositions(positionsData.positions || [])
}
}
const balanceResponse = await fetch('/api/wallet/balance')
if (balanceResponse.ok) {
const balanceData = await balanceResponse.json()
if (balanceData.success && balanceData.balance?.positions) {
// Convert positions array to balances object
const balances: {[key: string]: number} = {}
balanceData.balance.positions.forEach((position: any) => {
balances[position.symbol] = position.amount
})
setLiveBalances(balances)
}
}
// Clear form
setPayingAmount('')
setReceivingAmount('')
setStopLoss('')
setTakeProfit('')
alert(`${side} order placed (demo)`)
alert(`${side} order executed successfully!\nTransaction: ${result.signature || 'Completed'}`)
} else {
setTradeStatus(`Trade failed: ${result.error || 'Unknown error'}`)
alert(`❌ Trade failed: ${result.message || result.error || 'Unknown error'}`)
}
} catch (error) {
console.error('Trade execution error:', error)
setTradeStatus('Trade execution failed')
alert('❌ Trade execution failed. Please try again.')
} finally {
setIsLoading(false)
setTimeout(() => setTradeStatus(''), 5000)
}
}
const handleClosePosition = (positionId: string) => {
setPositions(prev => prev.filter(pos => pos.id !== positionId))
alert('Position closed (demo)')
const handleClosePosition = async (positionId: string) => {
const position = positions.find(pos => pos.id === positionId)
if (!position) return
if (!confirm(`Are you sure you want to close ${position.symbol} ${position.side} position?`)) {
return
}
setIsLoading(true)
setTradeStatus('Closing position...')
try {
const response = await fetch(`/api/trading/positions/${positionId}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
const result = await response.json()
if (result.success) {
setTradeStatus('Position closed successfully!')
// Refresh positions and balances
const positionsResponse = await fetch('/api/trading/positions')
if (positionsResponse.ok) {
const positionsData = await positionsResponse.json()
if (positionsData.success) {
setPositions(positionsData.positions || [])
}
}
const balanceResponse = await fetch('/api/wallet/balance')
if (balanceResponse.ok) {
const balanceData = await balanceResponse.json()
if (balanceData.success && balanceData.balance?.positions) {
// Convert positions array to balances object
const balances: {[key: string]: number} = {}
balanceData.balance.positions.forEach((position: any) => {
balances[position.symbol] = position.amount
})
setLiveBalances(balances)
}
}
alert(`✅ Position closed successfully!\nP&L: ${result.realizedPnl ? (result.realizedPnl >= 0 ? '+' : '') + '$' + result.realizedPnl.toFixed(2) : 'Calculated at settlement'}`)
} else {
setTradeStatus(`Failed to close position: ${result.error}`)
alert(`❌ Failed to close position: ${result.message || result.error}`)
}
} catch (error) {
console.error('Position close error:', error)
setTradeStatus('Failed to close position')
alert('❌ Failed to close position. Please try again.')
} finally {
setIsLoading(false)
setTimeout(() => setTradeStatus(''), 5000)
}
}
return (
@@ -172,7 +325,7 @@ export default function ChartTradingDemo() {
<div className="bg-gray-800 border-b border-gray-700 p-4">
<div className="flex items-center justify-between">
<div className="flex items-center space-x-6">
<h1 className="text-xl font-bold text-white">Chart Trading Terminal</h1>
<h1 className="text-xl font-bold text-white">Live Trading Terminal</h1>
{/* Symbol Selector Dropdown */}
<div className="relative" ref={dropdownRef}>
@@ -247,9 +400,14 @@ export default function ChartTradingDemo() {
{/* Market Status */}
<div className="flex items-center space-x-4 text-sm">
<div className="flex items-center space-x-2">
<div className="w-2 h-2 bg-green-400 rounded-full animate-pulse"></div>
<span className="text-gray-300">Demo Mode</span>
<div className={`w-2 h-2 rounded-full animate-pulse ${isLoading ? 'bg-yellow-400' : 'bg-green-400'}`}></div>
<span className="text-gray-300">{isLoading ? 'Processing...' : 'Live Trading'}</span>
</div>
{tradeStatus && (
<div className="text-blue-400 text-xs">
{tradeStatus}
</div>
)}
<div className="text-gray-400">
{new Date().toLocaleTimeString()}
</div>
@@ -331,17 +489,17 @@ export default function ChartTradingDemo() {
<div className="mt-6 space-y-3">
<button
onClick={() => handleTrade('BUY')}
disabled={!payingAmount}
disabled={!payingAmount || isLoading}
className="w-full bg-green-600 hover:bg-green-700 disabled:bg-gray-600 disabled:cursor-not-allowed text-white py-3 px-4 rounded-lg font-medium transition-all"
>
Long/Buy {leverage > 1 && `(${leverage}x)`}
{isLoading ? 'Processing...' : `Long/Buy ${leverage > 1 ? `(${leverage}x)` : ''}`}
</button>
<button
onClick={() => handleTrade('SELL')}
disabled={!payingAmount}
disabled={!payingAmount || isLoading}
className="w-full bg-red-600 hover:bg-red-700 disabled:bg-gray-600 disabled:cursor-not-allowed text-white py-3 px-4 rounded-lg font-medium transition-all"
>
Short/Sell {leverage > 1 && `(${leverage}x)`}
{isLoading ? 'Processing...' : `Short/Sell ${leverage > 1 ? `(${leverage}x)` : ''}`}
</button>
</div>
@@ -385,8 +543,7 @@ export default function ChartTradingDemo() {
onClick={() => {
setPayingToken(symbol)
setIsPayingTokenDropdownOpen(false)
}}
className={`w-full flex items-center justify-between px-3 py-2 text-left rounded-lg transition-all ${
}} className={`w-full flex items-center justify-between px-3 py-2 text-left rounded-lg transition-all ${
payingToken === symbol
? 'bg-blue-600/20 border border-blue-500/30'
: 'hover:bg-gray-700'
@@ -400,7 +557,7 @@ export default function ChartTradingDemo() {
</div>
</div>
<div className="text-xs text-gray-400">
{balance.toLocaleString()}
{balance.toFixed(balance < 1 ? 6 : 2)}
</div>
</button>
))}
@@ -412,14 +569,25 @@ export default function ChartTradingDemo() {
{/* Balance and MAX button */}
<div className="flex items-center justify-between mt-2 text-xs text-gray-400">
<span>Balance: {getCurrentPayingTokenData().balance.toLocaleString()} {payingToken}</span>
<span>Balance: {getCurrentPayingTokenData().balance.toFixed(getCurrentPayingTokenData().balance < 1 ? 6 : 2)} {payingToken}</span>
<button
onClick={() => setPayingAmount(getCurrentPayingTokenData().balance.toString())}
className="text-blue-400 hover:text-blue-300"
disabled={getCurrentPayingTokenData().balance === 0}
className="text-blue-400 hover:text-blue-300 disabled:text-gray-500 disabled:cursor-not-allowed"
>
MAX
</button>
</div>
{getCurrentPayingTokenData().balance === 0 && (
<div className="text-xs text-red-400 mt-1">
⚠️ No {payingToken} balance available
</div>
)}
{payingAmount && parseFloat(payingAmount) > getCurrentPayingTokenData().balance && (
<div className="text-xs text-red-400 mt-1">
⚠️ Insufficient balance ({getCurrentPayingTokenData().balance.toFixed(getCurrentPayingTokenData().balance < 1 ? 6 : 2)} {payingToken} available)
</div>
)}
</div>
{leverage > 1 && payingAmount && (
<div className="text-xs text-yellow-400 mt-1">
@@ -522,7 +690,13 @@ export default function ChartTradingDemo() {
{/* Positions Table */}
<div className="max-h-48 overflow-y-auto">
{positions.length === 0 ? (
{isLoading && positions.length === 0 && (
<div className="text-center py-8 text-gray-400">
<div className="inline-block animate-spin rounded-full h-6 w-6 border-b-2 border-blue-400 mr-2"></div>
Loading positions...
</div>
)}
{!isLoading && positions.length === 0 ? (
<div className="text-center py-8 text-gray-400">
No open positions
</div>
@@ -577,9 +751,10 @@ export default function ChartTradingDemo() {
<div className="flex space-x-2">
<button
onClick={() => handleClosePosition(position.id)}
className="px-3 py-1 bg-red-600 text-white rounded text-sm hover:bg-red-700 transition-all"
disabled={isLoading}
className="px-3 py-1 bg-red-600 text-white rounded text-sm hover:bg-red-700 disabled:bg-gray-600 disabled:cursor-not-allowed transition-all"
>
Close
{isLoading ? 'Processing...' : 'Close'}
</button>
</div>
</div>

View File

@@ -7,10 +7,13 @@ interface TradeModalProps {
tradeData: {
entry: string
tp: string
tp2?: string
sl: string
risk: string
reward: string
action: 'BUY' | 'SELL'
symbol?: string
timeframe?: string
risk?: string
reward?: string
action?: 'BUY' | 'SELL'
} | null
onExecute: (data: any) => void
}
@@ -55,6 +58,21 @@ export default function TradeModal({ isOpen, onClose, tradeData, onExecute }: Tr
}
}, [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')