# Interest rate is updated before updating the debt when repaying debt

In executeRepay, interest rates are recalculated before debt shares are reduced, so suppliers keep accruing interest on stale debt even after full repayment.

- Payout: Audit contest
- Severity: high
- Program: New Scope
- Found by: 000000
- Disclosed: 2024-06-27
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/413
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-new-scope%3AH-10
- Tags: area:web3 domain:web3 impact:funds-loss impact:integrity kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Lending pool BorrowLogic.executeRepay / ReserveLogic.updateInterestRates
**Root cause:** updateInterestRates reads cache.nextDebtShares before repayDebt updates totalSupplies.debtShares, so the new rate is computed against pre-repayment debt and treats the repayment as fresh liquidity.
**Steps:**
1. Bob supplies tokenB; Alice supplies tokenA and borrows tokenB, raising utilization and rate.
2. Alice calls `repaySimple(tokenB, type(uint256).max, 0)` to fully repay.
3. Observe `updateInterestRates` runs before `balances.repayDebt`/`cache.nextDebtShares = totalSupplies.debtShares`, so totalDebt still reflects old debt.
4. Skip 30 days, call `forceUpdateReserve(tokenB)`, and check `supplyAssets` for Bob's position — it increased despite zero borrows.
5. Bob's `withdrawSimple(tokenB, bob, type(uint256).max, 0)` reverts because accrued shares exceed pool balance when no other suppliers exist.
**How found:** Code reading of executeRepay ordering: noticed updateInterestRates is called before repayDebt mutates debtShares, and traced that updateInterestRates consumes cache.nextDebtShares as totalDebt.
**Exploitation:** Any borrower fully repaying leaves the reserve's borrow/liquidity rates stale; suppliers continue accruing interest on phantom debt, diluting or draining other suppliers' funds on withdrawal.
**Insight:** In lending pools, audit the ordering of state-mutating calls vs. rate/index recomputation in repay/withdraw/liquidate paths — any rate update that reads cached debt shares before the debt mutation will misprice utilization.

## Proof of concept (copied exactly from the original report)

**Foundry test proving interest accrues after full repay**

```
function testRepay() external {
    _mintAndApprove(alice, tokenA, 3000 ether, address(pool));
    _mintAndApprove(alice, tokenB, 1000 ether, address(pool));
    _mintAndApprove(bob, tokenB, 5000 ether, address(pool));

    vm.startPrank(bob);
    pool.supplySimple(address(tokenB), bob, 500 ether, 0);

    skip(12);
    vm.startPrank(alice);
    pool.supplySimple(address(tokenA), alice, 1000 ether, 0);

    skip(12);
    oracleA.updateRoundTimestamp();
    oracleB.updateRoundTimestamp();
    pool.borrowSimple(address(tokenB), alice, 375 ether, 0);

    skip(12);
    pool.repaySimple(address(tokenB), type(uint256).max, 0);

    vm.stopPrank();

    bytes32 bobPos = keccak256(abi.encodePacked(bob, 'index', uint256(0)));
    uint256 bobSupplyAssetsBefore = pool.supplyAssets(address(tokenB), bobPos);

    skip(24 * 30 * 60 * 60);

    pool.forceUpdateReserve(address(tokenB));

    assertGt(pool.supplyAssets(address(tokenB), bobPos), bobSupplyAssetsBefore); // Bob accrued interest even if there are no borrows anymore
```

