- Added stop loss and take profit parameters to TradeParams interface - Implemented conditional order placement in executeTrade method - Added ZERO import and closePosition method to DriftTradingService - Enhanced trade API to handle stop loss/take profit parameters - Added position fetching and closing functionality to AdvancedTradingPanel - Added open positions display with close buttons - Implemented risk management calculations and UI - Added conditional order tracking in TradeResult interface
106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { driftTradingService } from '../../../../lib/drift-trading'
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const {
|
|
symbol,
|
|
side,
|
|
amount,
|
|
leverage,
|
|
orderType,
|
|
price,
|
|
stopLoss,
|
|
takeProfit,
|
|
stopLossType,
|
|
takeProfitType
|
|
} = await request.json()
|
|
|
|
console.log(`🎯 Trade request: ${side} ${amount} ${symbol} at ${leverage}x leverage`)
|
|
if (stopLoss) console.log(`🛑 Stop Loss: $${stopLoss} (${stopLossType})`)
|
|
if (takeProfit) console.log(`🎯 Take Profit: $${takeProfit} (${takeProfitType})`)
|
|
|
|
// Validate inputs
|
|
if (!symbol || !side || !amount || !leverage) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Missing required trade parameters' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (amount <= 0 || leverage < 1 || leverage > 20) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Invalid trade parameters' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Validate stop loss and take profit if provided
|
|
if (stopLoss && stopLoss <= 0) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Invalid stop loss price' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
if (takeProfit && takeProfit <= 0) {
|
|
return NextResponse.json(
|
|
{ success: false, error: 'Invalid take profit price' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Convert LONG/SHORT to BUY/SELL for the trading service
|
|
const tradeSide: 'BUY' | 'SELL' = side === 'LONG' ? 'BUY' : 'SELL'
|
|
|
|
// Execute trade
|
|
const tradeParams = {
|
|
symbol,
|
|
side: tradeSide,
|
|
amount, // Position size in tokens
|
|
orderType: orderType || 'MARKET',
|
|
price: orderType === 'LIMIT' ? price : undefined,
|
|
stopLoss,
|
|
takeProfit,
|
|
stopLossType,
|
|
takeProfitType
|
|
}
|
|
|
|
const result = await driftTradingService.executeTrade(tradeParams)
|
|
|
|
if (result.success) {
|
|
console.log(`✅ Trade executed successfully: ${result.txId}`)
|
|
const response: any = {
|
|
success: true,
|
|
txId: result.txId,
|
|
executedPrice: result.executedPrice,
|
|
executedAmount: result.executedAmount,
|
|
message: `${side} order for ${amount} ${symbol} executed successfully`
|
|
}
|
|
|
|
if (result.conditionalOrders && result.conditionalOrders.length > 0) {
|
|
response.conditionalOrders = result.conditionalOrders
|
|
response.message += ` with ${result.conditionalOrders.length} conditional order(s)`
|
|
}
|
|
|
|
return NextResponse.json(response)
|
|
} else {
|
|
console.error(`❌ Trade execution failed: ${result.error}`)
|
|
return NextResponse.json(
|
|
{ success: false, error: result.error },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
} catch (error: any) {
|
|
console.error('❌ Trade API error:', error)
|
|
return NextResponse.json(
|
|
{
|
|
success: false,
|
|
error: error.message || 'Trade execution failed'
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|