Fixed Position Size Calculation: - Changed input from SOL to USD for clarity - Fixed calculation: positionSizeSOL = positionValueUSD / coinPrice - Resolved issue where entering 0.4 SOL showed incorrect 0.0025 underneath Added Real Wallet Balance Integration: - TradeModal now fetches actual wallet balance from /api/wallet/balance - Percentage buttons now calculate from real available balance (3.40) - No more impossible 1 SOL positions when only 3.40 available Enhanced Position Sizing UI: - Added slider for smooth position adjustment ( to full balance) - Percentage buttons (25%, 50%, 75%, 100%) now accurate - Real-time display shows both USD and SOL amounts - Live percentage display of balance usage Added Wallet Overview to Dashboard: - Main dashboard shows real wallet balance prominently - Trading page displays actual wallet holdings - StatusOverview component enhanced with wallet info - Accurate position sizing based on actual 3.40 balance - Intuitive slider + percentage buttons - Real-time balance updates every 30 seconds - Clear USD/SOL conversion display - No more calculation errors in trading modal
256 lines
10 KiB
JavaScript
256 lines
10 KiB
JavaScript
"use client"
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
export default function StatusOverview() {
|
|
const [status, setStatus] = useState({
|
|
driftBalance: 0,
|
|
activeTrades: 0,
|
|
dailyPnL: 0,
|
|
systemStatus: 'offline',
|
|
bitqueryStatus: 'unknown',
|
|
marketPrices: [],
|
|
walletBalance: null, // Real wallet balance
|
|
availableCoins: [] // Available coins in wallet
|
|
})
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
useEffect(() => {
|
|
async function fetchStatus() {
|
|
try {
|
|
setLoading(true)
|
|
|
|
// Get real wallet balance
|
|
let walletBalance = null
|
|
let availableCoins = []
|
|
|
|
try {
|
|
const walletRes = await fetch('/api/wallet/balance')
|
|
if (walletRes.ok) {
|
|
const walletData = await walletRes.json()
|
|
if (walletData.success) {
|
|
walletBalance = walletData.balance
|
|
availableCoins = walletData.balance.positions || []
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.warn('Could not fetch wallet balance:', e)
|
|
}
|
|
|
|
// Get market data from Bitquery
|
|
let balance = walletBalance?.totalValue || 0 // Use real wallet balance
|
|
let bitqueryStatus = 'error'
|
|
let marketPrices = []
|
|
|
|
try {
|
|
const marketRes = await fetch('/api/market')
|
|
if (marketRes.ok) {
|
|
const marketData = await marketRes.json()
|
|
if (marketData.success) {
|
|
marketPrices = marketData.data.prices || []
|
|
bitqueryStatus = marketData.data.status?.connected ? 'online' : 'error'
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.warn('Could not fetch market data:', e)
|
|
}
|
|
|
|
// Get system status
|
|
let systemStatus = 'online'
|
|
try {
|
|
const statusRes = await fetch('/api/status')
|
|
if (!statusRes.ok) {
|
|
systemStatus = 'error'
|
|
}
|
|
} catch (e) {
|
|
systemStatus = 'error'
|
|
}
|
|
|
|
setStatus({
|
|
driftBalance: balance,
|
|
activeTrades: 0, // No fake trades - will show real ones when we have them
|
|
dailyPnL: 0, // No fake P&L
|
|
systemStatus: systemStatus,
|
|
bitqueryStatus: bitqueryStatus,
|
|
marketPrices: marketPrices,
|
|
walletBalance: walletBalance,
|
|
availableCoins: availableCoins
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching status:', error)
|
|
setStatus(prev => ({ ...prev, systemStatus: 'error' }))
|
|
}
|
|
setLoading(false)
|
|
}
|
|
|
|
fetchStatus()
|
|
// Refresh every 30 seconds
|
|
const interval = setInterval(fetchStatus, 30000)
|
|
return () => clearInterval(interval)
|
|
}, [])
|
|
|
|
const statusColor = {
|
|
online: 'text-green-400',
|
|
offline: 'text-yellow-400',
|
|
error: 'text-red-400'
|
|
}
|
|
|
|
const statusIcon = {
|
|
online: '🟢',
|
|
offline: '🟡',
|
|
error: '🔴'
|
|
}
|
|
|
|
return (
|
|
<div className="card card-gradient">
|
|
<div className="flex items-center justify-between mb-6">
|
|
<h2 className="text-xl font-bold text-white">System Status</h2>
|
|
<div className="flex items-center space-x-2">
|
|
<span className="text-lg">{statusIcon[status.systemStatus]}</span>
|
|
<span className={`text-sm font-medium ${statusColor[status.systemStatus]}`}>
|
|
{status.systemStatus.toUpperCase()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{loading ? (
|
|
<div className="flex items-center justify-center py-8">
|
|
<div className="spinner"></div>
|
|
<span className="ml-2 text-gray-400">Loading status...</span>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-6">
|
|
{/* Main Status Grid */}
|
|
<div className="grid grid-cols-1 sm:grid-cols-3 gap-6">
|
|
<div className="text-center">
|
|
<div className="w-16 h-16 bg-blue-500/20 rounded-full flex items-center justify-center mx-auto mb-3">
|
|
<span className="text-blue-400 text-2xl">💎</span>
|
|
</div>
|
|
<p className="text-2xl font-bold text-blue-400">
|
|
${status.driftBalance.toFixed(2)}
|
|
</p>
|
|
<p className="text-gray-400 text-sm">Portfolio Value</p>
|
|
</div>
|
|
|
|
{/* Wallet Balance */}
|
|
{status.walletBalance && (
|
|
<div className="text-center">
|
|
<div className="w-16 h-16 bg-emerald-500/20 rounded-full flex items-center justify-center mx-auto mb-3">
|
|
<span className="text-emerald-400 text-2xl">🪙</span>
|
|
</div>
|
|
<p className="text-2xl font-bold text-emerald-400">
|
|
{status.walletBalance.positions?.[0]?.amount?.toFixed(4) || '0.0000'} SOL
|
|
</p>
|
|
<p className="text-gray-400 text-sm">Wallet Balance</p>
|
|
</div>
|
|
)}
|
|
|
|
<div className="text-center">
|
|
<div className="w-16 h-16 bg-purple-500/20 rounded-full flex items-center justify-center mx-auto mb-3">
|
|
<span className="text-purple-400 text-2xl">🔄</span>
|
|
</div>
|
|
<p className="text-2xl font-bold text-purple-400">
|
|
{status.activeTrades}
|
|
</p>
|
|
<p className="text-gray-400 text-sm">Active Trades</p>
|
|
</div>
|
|
|
|
<div className="text-center">
|
|
<div className="w-16 h-16 bg-green-500/20 rounded-full flex items-center justify-center mx-auto mb-3">
|
|
<span className="text-green-400 text-2xl">📈</span>
|
|
</div>
|
|
<p className={`text-2xl font-bold ${status.dailyPnL >= 0 ? 'text-green-400' : 'text-red-400'}`}>
|
|
{status.dailyPnL >= 0 ? '+' : ''}${status.dailyPnL.toFixed(2)}
|
|
</p>
|
|
<p className="text-gray-400 text-sm">Daily P&L</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Service Status */}
|
|
<div className="border-t border-gray-700 pt-6">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="text-lg font-semibold text-white">Service Status</h3>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="flex items-center justify-between p-3 bg-gray-800 rounded-lg">
|
|
<span className="text-gray-300">Trading Bot</span>
|
|
<div className="flex items-center space-x-2">
|
|
<span className="text-sm">{statusIcon[status.systemStatus]}</span>
|
|
<span className={`text-sm font-medium ${statusColor[status.systemStatus]}`}>
|
|
{status.systemStatus.toUpperCase()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center justify-between p-3 bg-gray-800 rounded-lg">
|
|
<span className="text-gray-300">Bitquery API</span>
|
|
<div className="flex items-center space-x-2">
|
|
<span className="text-sm">{statusIcon[status.bitqueryStatus]}</span>
|
|
<span className={`text-sm font-medium ${statusColor[status.bitqueryStatus]}`}>
|
|
{status.bitqueryStatus.toUpperCase()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Market Prices */}
|
|
{status.marketPrices.length > 0 && (
|
|
<div className="border-t border-gray-700 pt-6">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="text-lg font-semibold text-white">Live Market Prices</h3>
|
|
<span className="text-xs text-gray-400">Via Bitquery</span>
|
|
</div>
|
|
<div className="grid grid-cols-1 sm:grid-cols-3 gap-4">
|
|
{status.marketPrices.map((price, index) => (
|
|
<div key={index} className="p-3 bg-gray-800 rounded-lg">
|
|
<div className="flex items-center justify-between">
|
|
<span className="text-white font-medium">{price.symbol}</span>
|
|
<span className="text-white font-bold">${price.price?.toFixed(4)}</span>
|
|
</div>
|
|
<div className="flex items-center justify-between mt-1">
|
|
<span className="text-xs text-gray-400">24h Change</span>
|
|
<span className={`text-xs font-medium ${
|
|
price.change24h >= 0 ? 'text-green-400' : 'text-red-400'
|
|
}`}>
|
|
{price.change24h >= 0 ? '+' : ''}{price.change24h?.toFixed(2)}%
|
|
</span>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Available Coins in Wallet */}
|
|
{status.availableCoins.length > 0 && (
|
|
<div className="border-t border-gray-700 pt-6">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<h3 className="text-lg font-semibold text-white">Available Wallet Coins</h3>
|
|
<span className="text-xs text-gray-400">Ready for Trading</span>
|
|
</div>
|
|
<div className="grid grid-cols-1 gap-3">
|
|
{status.availableCoins.map((coin, index) => (
|
|
<div key={index} className="p-3 bg-gray-800 rounded-lg">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center space-x-3">
|
|
<span className="text-lg">🪙</span>
|
|
<div>
|
|
<span className="text-white font-medium">{coin.symbol}</span>
|
|
<div className="text-xs text-gray-400">${coin.price?.toFixed(2)}</div>
|
|
</div>
|
|
</div>
|
|
<div className="text-right">
|
|
<div className="text-white font-bold">{coin.amount?.toFixed(4)}</div>
|
|
<div className="text-sm text-gray-400">${coin.usdValue?.toFixed(2)}</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|