# debts calculation is not accurate

MochiVault.borrow increments global `debts` excluding fee while repay/liquidate decrement including fee, skewing utilization ratio and all debt/fee calculations.

- Payout: Audit contest
- Severity: high
- Program: Mochi
- Found by: gpersoon
- Disclosed: 2021-11-23
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2021-10-mochi-findings/issues/25
- Library page: https://bugbountylibrary.com/r/c4%3A2021-10-mochi%3AH-05
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:accounting-error

**Target:** MochiVault.sol / MochiProfileV0.sol debt accounting
**Root cause:** borrow() adds `_amount` (excl. fee) to `debts` while details[_id].debt is set to totalDebt (incl. fee); repay()/liquidate() subtract the fee-inclusive amount from `debts`, so the global counter drifts from actual debt.
**Steps:**
1. Read `borrow()`: `details[_id].debt = totalDebt; debts += _amount;`
2. Read `repay()`/`liquidate()`: `debts -= _amount; details[_id].debt -= _amount;`
3. Observe `debts` under-counts by accumulated fees, drifting negative over time
4. Trace consumers: utilizationRatio(), stabilityFee(), calculateFeeIndex(), liveDebtIndex(), accrueDebt(), currentDebt()
**How found:** Code review comparing how `debts` is mutated across borrow/repay/liquidate; noticed borrow uses fee-exclusive amount while repay/liquidate use fee-inclusive amount.
**Insight:** When a global aggregate counter and per-position debt are updated in different functions, diff the exact expression each uses (fee-inclusive vs exclusive) — drift in the aggregate silently corrupts every ratio/fee derived from it.
