Fix critical balance validation and add comprehensive trading features

- Fixed CoinGecko API rate limiting with fallback SOL price (68.11)
- Corrected internal API calls to use proper Docker container ports
- Fixed balance validation to prevent trades exceeding wallet funds
- Blocked 0.5 SOL trades with only 0.073 SOL available (~2.24)

- Added persistent storage for positions, trades, and pending orders
- Implemented limit order system with auto-fill monitoring
- Created pending orders panel and management API
- Added trades history tracking and display panel
- Enhanced position tracking with P&L calculations
- Added wallet balance validation API endpoint

- Positions stored in data/positions.json
- Trade history stored in data/trades.json
- Pending orders with auto-fill logic
- Real-time balance validation before trades

- All trades now validate against actual wallet balance
- Insufficient balance trades are properly blocked
- Added comprehensive error handling and logging
- Fixed Docker networking for internal API calls

- SPOT and leveraged trading modes
- Limit orders with price monitoring
- Stop loss and take profit support
- DEX integration with Jupiter
- Real-time position updates and P&L tracking

 Tested and verified all balance validation works correctly
This commit is contained in:
mindesbunister
2025-07-14 17:19:58 +02:00
parent 0d7b46fdcf
commit b0b63d5db0
14 changed files with 1445 additions and 61 deletions

View File

@@ -127,11 +127,14 @@ export default function TradeExecutionPanel({ analysis, symbol = 'SOL' }) {
tradeData.perpCoin = perpCoin
}
// Determine API endpoint based on trading mode
// Determine API endpoint based on trading mode and order type
let apiEndpoint = '/api/trading/execute-dex'
if (tradingMode === 'PERP') {
apiEndpoint = '/api/trading/execute-perp'
} else if (tradePrice) {
// Limit orders go through the main trading API
apiEndpoint = '/api/trading'
}
const response = await fetch(apiEndpoint, {
@@ -145,11 +148,21 @@ export default function TradeExecutionPanel({ analysis, symbol = 'SOL' }) {
const result = await response.json()
if (result.success) {
setExecutionResult({
success: true,
trade: result.trade,
message: result.message
})
// Check if this was a limit order creation
if (result.type === 'limit_order_created') {
setExecutionResult({
success: true,
order: result.order,
type: 'limit_order',
message: result.message
})
} else {
setExecutionResult({
success: true,
trade: result.trade,
message: result.message
})
}
// Refresh balance after successful trade
await fetchBalance()
} else {
@@ -547,7 +560,9 @@ export default function TradeExecutionPanel({ analysis, symbol = 'SOL' }) {
executionResult.success ? 'bg-green-900 border border-green-600' : 'bg-red-900 border border-red-600'
}`}>
<div className={`font-bold ${executionResult.success ? 'text-green-400' : 'text-red-400'}`}>
{executionResult.success ? '✅ Trade Executed' : '❌ Trade Failed'}
{executionResult.success ? (
executionResult.type === 'limit_order' ? '📋 Limit Order Created' : '✅ Trade Executed'
) : '❌ Trade Failed'}
</div>
<div className="text-sm text-gray-300 mt-1">
{executionResult.message}
@@ -557,6 +572,15 @@ export default function TradeExecutionPanel({ analysis, symbol = 'SOL' }) {
TX ID: {executionResult.trade.txId}
</div>
)}
{executionResult.order && (
<div className="text-xs text-gray-400 mt-2">
Order ID: {executionResult.order.id}
<br />
Limit Price: ${executionResult.order.limitPrice}
<br />
Status: {executionResult.order.status}
</div>
)}
</div>
)}