# Liquidations will revert incorrectly due to an out-of-sync leftover collateral value

Peapods liquidation check uses optimistic clean-fee leftover collateral instead of recomputed dirty-fee value, causing valid liquidations to revert and risking bad debt.

- Payout: Audit contest
- Severity: medium
- Program: Peapods Finance
- Found by: X77
- Disclosed: 2025-01-08
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2025-01-peapods-finance-judging/issues/74
- Library page: https://bugbountylibrary.com/r/sherlock%3A2025-01-peapods-finance%3AM-2
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:liquidation

**Target:** Lending/liquidation contract collateral accounting
**Root cause:** _leftoverCollateral is computed from the optimistic clean-fee collateral, but when positive the liquidator collateral is recomputed with the lower dirty fee; the leftover is never updated, so the minCollateralRequiredOnDirtyLiquidation check uses a stale, too-low value.
**Steps:**
1. Set userCollateralBalance=2.3e18, cleanFee=10000, dirtyFee=9000, liquidationAmountInCollateralUnits=2e18, minCollateralRequiredOnDirtyLiquidation=1.1e17
2. Compute _optimisticCollateralForLiquidator = 2e18*(1e5+1e4)/1e5 = 2.2e18
3. _leftoverCollateral = 2.3e18 - 2.2e18 = 1e17 (>0)
4. Recompute _collateralForLiquidator = 2e18*(1e5+9e3)/1e5 = 2.18e18
5. Check 1e17 < 1.1e17 -> revert BadDirtyLiquidation(), though true leftover is 2.3e18-2.18e18=1.2e17
**How found:** Code reading of the liquidation branch: noticed leftover collateral computed before the dirty-fee recomputation and reused in the min-collateral revert check, making the check inconsistent with actual collateral removed.
**Insight:** When a function computes a provisional value, then recomputes a dependent value under a different fee/rate branch, audit every downstream check that still consumes the provisional value; fee differentials (clean vs dirty) are a common source of stale-variable mismatches.

