# User could have impossible to close position if funding fees grow too big.

Velar Artha funding-fee accounting lets a funding receiver drain quote collateral beyond backing, making other longs impossible to close via underflow.

- Payout: Audit contest
- Severity: medium
- Program: Velar Artha
- Found by: bughuntoor
- Disclosed: 2024-08-22
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-velar-artha-judging/issues/96
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-velar-artha%3AM-10
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:dos

**Target:** Vyper perpetuals positions/fees contracts (positions.vy, fees.vy)
**Root cause:** Funding fees are paid first-come-first-serve assuming the paying side closes before the receiving side; if the receiver closes first it can withdraw funding_received exceeding the payer's capped funding_paid, reducing quote_collateral below what remaining positions need.
**Steps:**
1. Open long with collateral X and short; long pays funding to short.
2. Let funding fee accrue to >= X (liquidation bot fails to liquidate).
3. Open a second long with collateral X; total quote collateral 2X.
4. Short closes, receiving funding_received ~1.2X; quote_collateral drops to ~0.8X.
5. Second long tries to close: MATH.eval computes 0.8X + 0.1X - 1X and underflows, reverting.
**How found:** Code-reading of the first-come-first-serve funding payout comment in positions.vy; noticed the assumption that the paying side always closes before the receiving side, then constructed a reversed-order scenario.
**Exploitation:** Attacker opens a max-leverage long that accrues funding beyond collateral while liquidation lags, then a counterparty short closes first to drain quote_collateral; the attacker's position becomes unclosable and keeps accruing unbacked funding fees.
**Escalation:** Unclosable position keeps accruing funding fees not backed by collateral, letting attacker double down.
**Insight:** When a contract pays out accrued fees 'first come first serve' with min(received, avail), check whether the receiving side can close before the paying side and withdraw more than the payer's capped contribution, leaving global collateral underflowed for later closers.

