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

@@ -0,0 +1,72 @@
import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
// Persistent storage for positions using JSON file
const POSITIONS_FILE = path.join(process.cwd(), 'data', 'positions.json')
// Helper functions for persistent storage
function loadPositions() {
try {
if (fs.existsSync(POSITIONS_FILE)) {
const data = fs.readFileSync(POSITIONS_FILE, 'utf8')
return JSON.parse(data)
}
} catch (error) {
console.error('Error loading positions:', error)
}
return []
}
function savePositions(positions) {
try {
fs.writeFileSync(POSITIONS_FILE, JSON.stringify(positions, null, 2))
} catch (error) {
console.error('Error saving positions:', error)
}
}
export async function DELETE(request, { params }) {
try {
const { positionId } = params
if (!positionId) {
return NextResponse.json({
success: false,
error: 'Position ID is required'
}, { status: 400 })
}
// Load existing positions
const activePositions = loadPositions()
// Find and remove the position
const positionIndex = activePositions.findIndex(pos => pos.id === positionId)
if (positionIndex === -1) {
return NextResponse.json({
success: false,
error: 'Position not found'
}, { status: 404 })
}
const removedPosition = activePositions[positionIndex]
activePositions.splice(positionIndex, 1)
savePositions(activePositions)
console.log(`🗑️ Removed incorrect position: ${removedPosition.symbol} (${positionId})`)
return NextResponse.json({
success: true,
message: `Position ${positionId} removed successfully`,
removedPosition
})
} catch (error) {
console.error('Error deleting position:', error)
return NextResponse.json({
success: false,
error: 'Failed to delete position'
}, { status: 500 })
}
}