Prevent repeated TP2 cleanup loops
This commit is contained in:
@@ -46,6 +46,8 @@ export interface ClosePositionResult {
|
||||
closePrice?: number
|
||||
closedSize?: number
|
||||
realizedPnL?: number
|
||||
fullyClosed?: boolean
|
||||
remainingSize?: number
|
||||
error?: string
|
||||
}
|
||||
|
||||
@@ -602,13 +604,21 @@ export async function closePosition(
|
||||
console.log(` Closed notional: $${closedNotional.toFixed(2)}`)
|
||||
console.log(` Realized P&L: $${realizedPnL.toFixed(2)}`)
|
||||
|
||||
// If closing 100%, cancel all remaining orders for this market
|
||||
if (params.percentToClose === 100) {
|
||||
// Check remaining position size after close
|
||||
const updatedPosition = await driftService.getPosition(marketConfig.driftMarketIndex)
|
||||
const remainingSize = updatedPosition ? Math.abs(updatedPosition.size) : 0
|
||||
const fullyClosed = !updatedPosition || remainingSize === 0
|
||||
|
||||
if (fullyClosed) {
|
||||
console.log('🗑️ Position fully closed, cancelling remaining orders...')
|
||||
const cancelResult = await cancelAllOrders(params.symbol)
|
||||
if (cancelResult.success && cancelResult.cancelledCount! > 0) {
|
||||
if (cancelResult.success && (cancelResult.cancelledCount || 0) > 0) {
|
||||
console.log(`✅ Cancelled ${cancelResult.cancelledCount} orders`)
|
||||
}
|
||||
} else if (params.percentToClose === 100) {
|
||||
console.log(
|
||||
`⚠️ Requested 100% close but ${remainingSize.toFixed(4)} base remains on-chain`
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -617,6 +627,8 @@ export async function closePosition(
|
||||
closePrice: oraclePrice,
|
||||
closedSize: sizeToClose,
|
||||
realizedPnL,
|
||||
fullyClosed,
|
||||
remainingSize,
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user