Fix spot trade logic: remove incorrect positions and enhance trade history display

- Remove incorrect open positions for spot swaps (instant settlements)
- Add DELETE API route for position removal (/api/trading/positions/[positionId])
- Update existing SOL/USDC trade to clearly mark as SPOT_SWAP
- Enhance TradesHistoryPanel with visual trade type indicators:
  * SPOT_SWAP: Purple badge with  icon
  * MARKET: Blue badge with 📈 icon
  * LIMIT: Orange badge with 🎯 icon
  * STOP: Red badge with 🛑 icon
- Add trade history update functionality for modifying existing trades
- Fix container communication URLs in execute-dex route

Result: Spot trades no longer create open positions, trade history clearly shows trade types
This commit is contained in:
mindesbunister
2025-07-16 11:36:58 +02:00
parent cd1273b612
commit 77eb727f8d
4 changed files with 134 additions and 31 deletions

View File

@@ -191,8 +191,9 @@ export async function POST(request) {
message: `${side.toUpperCase()} order executed on Jupiter DEX${stopLoss || takeProfit ? ' with TP/SL monitoring' : ''}`
}
// Add trade to history
// Add trade to history with clear spot swap indication
try {
// Use localhost for internal container communication
await fetch('http://localhost:3000/api/trading/history', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -202,44 +203,26 @@ export async function POST(request) {
side: side.toUpperCase(),
amount: amount,
price: 168.1, // Get from actual execution
type: 'market',
type: 'SPOT_SWAP', // Clearly indicate this is a spot swap
status: 'executed',
txId: tradeResult.txId,
dex: 'JUPITER',
notes: closePosition ? 'Position closing trade' : null
tradingMode: 'SPOT',
notes: closePosition ? 'Position closing trade' : `Spot swap: ${amount} ${fromCoin || symbol}${toCoin || 'USDC'}`
})
})
console.log('✅ Trade added to history')
console.log('✅ Spot trade added to history')
} catch (error) {
console.error('❌ Failed to add trade to history:', error)
}
// Create position only if not closing an existing position
if (!closePosition) {
try {
const positionResponse = await fetch('http://localhost:3000/api/trading/positions', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'add',
symbol: tradingPair || `${fromCoin || symbol}/${toCoin || 'USDC'}`,
side: side.toUpperCase(),
amount: amount,
entryPrice: 168.1, // Get from actual execution
stopLoss: stopLoss,
takeProfit: takeProfit,
txId: tradeResult.txId,
leverage: 1
})
})
if (positionResponse.ok) {
console.log('✅ Position created for DEX trade')
}
} catch (error) {
console.error('❌ Failed to create position:', error)
}
}
// DON'T create positions for spot swaps - they're instant settlements
// Only leverage/perpetual trades should create positions
// For spot trades, we only need trade history entries
// Note: Position creation is intentionally removed for spot trades
// If this trade had stop loss or take profit, those would be handled as separate limit orders
console.log('✅ Spot trade completed - no position created (instant settlement)')
return NextResponse.json(tradeResponse)