Files
trading_bot_v4/lib
mindesbunister 4bf5761ec0 critical: Bug #91 - Use SDK closePosition() for exact BN (no Math.floor truncation)
Root Cause: Math.floor(sizeToClose * 1e9) truncated position sizes, leaving tiny fractional remnants (e.g., 0.00000008 SOL) that prevented full position closure.

Discovery: Drift UI 'Close All Positions' failed with 'not enough collateral' but clicking 'Market' order worked - because Market uses exact position size.

Solution: SDK's driftClient.closePosition() uses exact BN arithmetic internally (baseAssetAmount.abs()), avoiding any floating point truncation.

Changes:
- lib/drift/orders.ts lines 647-690
- For 100% closes: Now uses driftClient.closePosition(marketIndex)
- For partial closes: Continues using placeAndTakePerpOrder

Expected Impact: Flip operations will now fully close positions without leaving fractional remnants that cause 'position still open' failures.

Financial Impact: Prevents flip failures that caused user 000+ losses from multiple bugs in position closing logic.
2026-01-01 13:50:45 +01:00
..