diff --git a/lib/drift/orders.ts b/lib/drift/orders.ts index b9efb95..ad1c016 100644 --- a/lib/drift/orders.ts +++ b/lib/drift/orders.ts @@ -273,10 +273,11 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise< const signatures: string[] = [] // Helper to compute base asset amount from USD notional and price - // CRITICAL: Use ENTRY price to calculate position size, not TP price! - // This ensures we close the correct percentage of the actual position - const usdToBase = (usd: number) => { - const base = usd / options.entryPrice // Use entry price for size calculation + // CRITICAL FIX (Dec 10, 2025): Must use SPECIFIC PRICE for each order (TP1 price, TP2 price, SL price) + // Bug discovered: Using entryPrice for all orders causes wrong token quantities = rejected orders + // Original working implementation (commit 4cc294b, Oct 26): "All 3 exit orders placed successfully" + const usdToBase = (usd: number, price: number) => { + const base = usd / price // Use the specific order price (NOT entryPrice) return Math.floor(base * 1e9) // 9 decimals expected by SDK } @@ -297,7 +298,7 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise< // Place TP1 LIMIT reduce-only if (tp1USD > 0) { - const baseAmount = usdToBase(tp1USD) + const baseAmount = usdToBase(tp1USD, options.tp1Price) // Use TP1 price if (baseAmount >= Math.floor(marketConfig.minOrderSize * 1e9)) { const orderParams: any = { orderType: OrderType.LIMIT, @@ -321,7 +322,7 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise< // Place TP2 LIMIT reduce-only if (tp2USD > 0) { - const baseAmount = usdToBase(tp2USD) + const baseAmount = usdToBase(tp2USD, options.tp2Price) // Use TP2 price if (baseAmount >= Math.floor(marketConfig.minOrderSize * 1e9)) { const orderParams: any = { orderType: OrderType.LIMIT, @@ -350,7 +351,7 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise< // 3. Single TRIGGER_MARKET (default, guaranteed execution) const slUSD = options.positionSizeUSD - const slBaseAmount = usdToBase(slUSD) + const slBaseAmount = usdToBase(slUSD, options.stopLossPrice) // Use SL price // Calculate expected number of orders for validation (Bug #76 fix) const useDualStops = options.useDualStops ?? false