# Funding Paid != Funding Received

Velar Artha's calc_fees zeroes funding_received for negative positions but global received sums already counted their collateral, leaving funding fees permanently stuck.

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

**Target:** Vyper perpetuals positions/fees accounting (positions.vy, fees.vy)
**Root cause:** calc_fees sets funding_received to 0 (or min with available collateral) when a position is negative, but the global received_long/short sums were already accrued using that position's collateral in fs.long/short_collateral, so the pro-rata distribution never accounts for the removed collateral.
**Steps:**
1. Open a long and short position in a BTC-STX pool via `open(...)`.
2. Advance blocks so funding accrues; a position's remaining collateral drops below funding_paid.
3. Close/liquidate the negative position: `calc_fees` returns funding_received=0 because remaining==0.
4. Observe `current_fees` computes `received_short_term = divide(paid_long_term, fs.short_collateral)` using collateral that still includes the now-removed position.
5. Other positions receive only their pro-rata share of the inflated denominator; the remainder is never claimable.
**How found:** Code review of calc_fees special cases (remaining==0 and min(fees.funding_received, avail)) against the stated invariant sum(funding_received)=sum(funding_paid) in core.vy comments; traced how current_fees uses fs.long/short_collateral at update time versus when positions are removed.
**Exploitation:** No direct attacker profit path described; the flaw causes a portion of paid funding fees to remain unclaimed in the contract and dilutes distributions to future recipients.
**Insight:** When an accounting invariant is enforced by zeroing a per-position value, check whether global accumulators (received_*_sum) were already incremented using that position's collateral — the denominator at accrual time must match the denominator at claim time.

