fix: Implement critical risk management fixes for bugs #76, #77, #78, #80

Co-authored-by: mindesbunister <32161838+mindesbunister@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-09 22:23:43 +00:00
parent 2b0673636f
commit 63b94016fe
4 changed files with 326 additions and 131 deletions

View File

@@ -352,66 +352,83 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise<
const slUSD = options.positionSizeUSD
const slBaseAmount = usdToBase(slUSD)
// Calculate expected number of orders for validation (Bug #76 fix)
const useDualStops = options.useDualStops ?? false
const expectedOrderCount = 2 + (useDualStops ? 2 : 1) // TP1 + TP2 + (soft+hard SL OR single SL)
logger.log(`📊 Expected ${expectedOrderCount} exit orders total (TP1 + TP2 + ${useDualStops ? 'dual stops' : 'single stop'})`)
if (slBaseAmount >= Math.floor(marketConfig.minOrderSize * 1e9)) {
const useDualStops = options.useDualStops ?? false
if (useDualStops && options.softStopPrice && options.hardStopPrice) {
// ============== DUAL STOP SYSTEM ==============
logger.log('🛡️🛡️ Placing DUAL STOP SYSTEM...')
// 1. Soft Stop (TRIGGER_LIMIT) - Avoids wicks
const softStopBuffer = options.softStopBuffer ?? 0.4
const softStopMultiplier = options.direction === 'long'
? (1 - softStopBuffer / 100)
: (1 + softStopBuffer / 100)
const softStopParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.softStopPrice * 1e6)),
price: new BN(Math.floor(options.softStopPrice * softStopMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
try {
// 1. Soft Stop (TRIGGER_LIMIT) - Avoids wicks
const softStopBuffer = options.softStopBuffer ?? 0.4
const softStopMultiplier = options.direction === 'long'
? (1 - softStopBuffer / 100)
: (1 + softStopBuffer / 100)
const softStopParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.softStopPrice * 1e6)),
price: new BN(Math.floor(options.softStopPrice * softStopMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
logger.log(` 1⃣ Soft Stop (TRIGGER_LIMIT):`)
logger.log(` Trigger: $${options.softStopPrice.toFixed(4)}`)
logger.log(` Limit: $${(options.softStopPrice * softStopMultiplier).toFixed(4)}`)
logger.log(` Purpose: Avoid false breakouts/wicks`)
logger.log(` 🔄 Executing soft stop placement...`)
const softStopSig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(softStopParams)
)
logger.log(` ✅ Soft stop placed: ${softStopSig}`)
signatures.push(softStopSig)
} catch (softStopError) {
console.error(`❌ CRITICAL: Failed to place soft stop:`, softStopError)
throw new Error(`Soft stop placement failed: ${softStopError instanceof Error ? softStopError.message : 'Unknown error'}`)
}
logger.log(` 1⃣ Soft Stop (TRIGGER_LIMIT):`)
logger.log(` Trigger: $${options.softStopPrice.toFixed(4)}`)
logger.log(` Limit: $${(options.softStopPrice * softStopMultiplier).toFixed(4)}`)
logger.log(` Purpose: Avoid false breakouts/wicks`)
const softStopSig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(softStopParams)
)
logger.log(` ✅ Soft stop placed: ${softStopSig}`)
signatures.push(softStopSig)
// 2. Hard Stop (TRIGGER_MARKET) - Guarantees exit
const hardStopParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.hardStopPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
try {
// 2. Hard Stop (TRIGGER_MARKET) - Guarantees exit
const hardStopParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.hardStopPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
logger.log(` 2⃣ Hard Stop (TRIGGER_MARKET):`)
logger.log(` Trigger: $${options.hardStopPrice.toFixed(4)}`)
logger.log(` Purpose: Guaranteed exit if soft stop doesn't fill`)
logger.log(` 🔄 Executing hard stop placement...`)
const hardStopSig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(hardStopParams)
)
logger.log(` ✅ Hard stop placed: ${hardStopSig}`)
signatures.push(hardStopSig)
} catch (hardStopError) {
console.error(`❌ CRITICAL: Failed to place hard stop:`, hardStopError)
throw new Error(`Hard stop placement failed: ${hardStopError instanceof Error ? hardStopError.message : 'Unknown error'}`)
}
logger.log(` 2⃣ Hard Stop (TRIGGER_MARKET):`)
logger.log(` Trigger: $${options.hardStopPrice.toFixed(4)}`)
logger.log(` Purpose: Guaranteed exit if soft stop doesn't fill`)
const hardStopSig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(hardStopParams)
)
logger.log(` ✅ Hard stop placed: ${hardStopSig}`)
signatures.push(hardStopSig)
logger.log(`🎯 Dual stop system active: Soft @ $${options.softStopPrice.toFixed(2)} | Hard @ $${options.hardStopPrice.toFixed(2)}`)
} else {
@@ -419,64 +436,86 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise<
const useStopLimit = options.useStopLimit ?? false
const stopLimitBuffer = options.stopLimitBuffer ?? 0.5
if (useStopLimit) {
// TRIGGER_LIMIT: For liquid markets
const limitPriceMultiplier = options.direction === 'long'
? (1 - stopLimitBuffer / 100)
: (1 + stopLimitBuffer / 100)
const orderParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
price: new BN(Math.floor(options.stopLossPrice * limitPriceMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
try {
if (useStopLimit) {
// TRIGGER_LIMIT: For liquid markets
const limitPriceMultiplier = options.direction === 'long'
? (1 - stopLimitBuffer / 100)
: (1 + stopLimitBuffer / 100)
const orderParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
price: new BN(Math.floor(options.stopLossPrice * limitPriceMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
logger.log(`🛡️ Placing SL as TRIGGER_LIMIT (${stopLimitBuffer}% buffer)...`)
logger.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
logger.log(` Limit: $${(options.stopLossPrice * limitPriceMultiplier).toFixed(4)}`)
logger.log(` ⚠️ May not fill during fast moves - use for liquid markets only!`)
logger.log(`🔄 Executing SL trigger-limit placement...`)
const sig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(orderParams)
)
logger.log('✅ SL trigger-limit order placed:', sig)
signatures.push(sig)
} else {
// TRIGGER_MARKET: Default, guaranteed execution
const orderParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
logger.log(`🛡️ Placing SL as TRIGGER_MARKET (guaranteed execution - RECOMMENDED)...`)
logger.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
logger.log(` ✅ Will execute at market price when triggered (may slip but WILL fill)`)
logger.log(`🔄 Executing SL trigger-market placement...`)
const sig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(orderParams)
)
logger.log('✅ SL trigger-market order placed:', sig)
signatures.push(sig)
}
logger.log(`🛡️ Placing SL as TRIGGER_LIMIT (${stopLimitBuffer}% buffer)...`)
logger.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
logger.log(` Limit: $${(options.stopLossPrice * limitPriceMultiplier).toFixed(4)}`)
logger.log(` ⚠️ May not fill during fast moves - use for liquid markets only!`)
const sig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(orderParams)
)
logger.log('✅ SL trigger-limit order placed:', sig)
signatures.push(sig)
} else {
// TRIGGER_MARKET: Default, guaranteed execution
const orderParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
logger.log(`🛡️ Placing SL as TRIGGER_MARKET (guaranteed execution - RECOMMENDED)...`)
logger.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
logger.log(` ✅ Will execute at market price when triggered (may slip but WILL fill)`)
const sig = await retryWithBackoff(async () =>
await (driftClient as any).placePerpOrder(orderParams)
)
logger.log('✅ SL trigger-market order placed:', sig)
signatures.push(sig)
} catch (slError) {
console.error(`❌ CRITICAL: Failed to place stop loss:`, slError)
throw new Error(`Stop loss placement failed: ${slError instanceof Error ? slError.message : 'Unknown error'}`)
}
}
} else {
logger.log('⚠️ SL size below market min, skipping on-chain SL')
}
// CRITICAL VALIDATION (Bug #76 fix): Verify all expected orders were placed
if (signatures.length < expectedOrderCount) {
const errorMsg = `MISSING EXIT ORDERS: Expected ${expectedOrderCount}, got ${signatures.length}. Position is UNPROTECTED!`
console.error(`${errorMsg}`)
console.error(` Expected: TP1 + TP2 + ${useDualStops ? 'Soft SL + Hard SL' : 'SL'}`)
console.error(` Got ${signatures.length} signatures:`, signatures)
return {
success: false,
error: errorMsg,
signatures // Return partial signatures for debugging
}
}
logger.log(`✅ All ${expectedOrderCount} exit orders placed successfully`)
return { success: true, signatures }
} catch (error) {
console.error('❌ Failed to place exit orders:', error)