# Deleveraging can result in a zero borrowed amount while maintaining the leveraged position

Deleveraging caps unhold at borrowed amount, so a price drop lets a user add margin that zeroes holdAmount while keeping full leveraged position, dodging borrowing fees.

- Payout: Contest reward
- Severity: high
- Program: Elfi Protocol
- Credited: mstpr-brainbot
- Disclosed: 2024-05-09
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-05-elfi-protocol-judging/issues/114
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-05-elfi-protocol%3AH-23
- Tags: domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:solidity technique:source-review vuln:business-logic vuln:fee-bypass

**Target:** Perpetual futures position margin/deleverage logic (PositionMarginProcess)
**Root cause:** _executeAddMargin caps subHoldAmount at position.holdPoolAmount via min(), so when required margin exceeds the borrowed amount, only the borrowed amount is unheld and holdAmount drops to zero while the leveraged position quantity is unchanged.
**Steps:**
1. Open 10x isolated LONG on tokenA at $1: $100 margin, $1000 position, borrow 900 tokenA, holdAmount=900.
2. tokenA price drops to $0.40; position now needs $500 margin for 2x deleverage.
3. Call deleverage/addMargin adding $400 worth = 1000 tokenA.
4. Observe `subHoldAmount = cache.addMarginAmount.min(position.holdPoolAmount)` = min(1000,900)=900.
5. holdPoolAmount becomes 0; position retains full quantity with no borrowing fee accrual.
**How found:** Code reading of _executeAddMargin's min() cap against holdPoolAmount, combined with reasoning about price movement between leverage update requests.
**Exploitation:** User waits for adverse price move, then submits a deleverage/add-margin request sized above the outstanding borrow; the min() cap unholds only the borrowed amount, zeroing holdAmount so the position no longer pays borrowing fees while keeping the leveraged exposure.
**Escalation:** Works on cross positions with large collateral so liquidation doesn't preempt the maneuver, per researcher's rebuttal.
**Insight:** When a margin/deleverage routine caps an unhold/repay amount at the outstanding borrow, check whether the excess margin silently clears the debt-tracking field (holdAmount) without reducing position size — fee-avoidance via accounting mismatch.
