# There may be excess funds in the PnL pool or bad debt due to the funding fee.

Funding fee accounting uses openNotional sums that diverge between longs and shorts after position reversals, leaving excess funds or bad debt in the PnL pool.

- Payout: Contest reward
- Severity: medium
- Program: Perpetual
- Credited: ether_sky
- Disclosed: 2024-02-19
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-02-perpetual-judging/issues/102
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-02-perpetual%3AM-3
- Tags: domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:precision-loss

**Target:** FundingFee.sol / LibPosition.sol in perp-contract-v3
**Root cause:** Funding fees are computed from openNotional, but long and short openNotional sums are not guaranteed equal after position reversals (openNotional resets to current price), so payers and receivers totals diverge.
**Steps:**
1. Two longs with openNotional S and S/2; matching shorts -S and -S/2.
2. First long cancels against the -S/2 short; that short reverses to a long whose openNotional is repriced at current price, not S/2.
3. Sum of long openNotionals no longer equals sum of short openNotionals.
4. Funding fee per second = fundingRate * sum(openNotional) of each side; unequal sums mean fees paid != fees distributed.
5. Excess accumulates in PnL pool when payers > receivers; shortfall/bad debt when receivers > payers, with no other funding source to cover it.
**How found:** Code reading of getCurrentFundingRate/getPendingFee and LibPosition.add; noticed openNotional is reset to current price on reversal, so long/short openNotional sums can diverge despite equal position sizes.
**Insight:** When auditing funding/borrowing fee accounting, check whether the quantity used to compute fees (openNotional) is conserved across position reversals; position size is conserved but notional is repriced, so fee conservation can break.
