# The repayment process in the NFTPositionManager can sometimes be reverted

Rounding mismatch between rayMul/rayDiv share-asset conversions makes NFTPositionManager._repay's balance check revert, blocking repayments for certain index/share/amount combos.

- Payout: Audit contest
- Severity: medium
- Program: New Scope
- Found by: Obsidian
- Disclosed: 2024-06-27
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-06-new-scope-judging/issues/488
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-new-scope%3AM-15
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:medium tech:solidity technique:source-review vuln:dos vuln:precision-loss

**Target:** NFTPositionManager._repay / Pool share-asset math (WadRayMath)
**Root cause:** The strict equality check `previousDebtBalance - currentDebtBalance != repaid.assets` fails because converting shares to assets and back via rayMul/rayDiv rounds differently, producing a 1 wei discrepancy.
**Steps:**
1. Set pool borrow index I = 2e27, user share s = 5.
2. previousDebtBalance = rayMul(5, 2e27) = 10.
3. Call repay with amount = 3 assets; removed shares = rayDiv(3, 2e27) = 2; remaining shares = 3.
4. currentDebtBalance = rayMul(3, 2e27) = 6.
5. Check: previousDebtBalance - currentDebtBalance = 4 != repaid.assets = 3, so `revert NFTErrorsLib.BalanceMisMatch()`.
**How found:** Code reading of _repay's balance assertion combined with manual arithmetic on rayMul/rayDiv rounding to construct a concrete mismatch example.
**Exploitation:** Not weaponized; griefing/DoS where a user's repay tx reverts due to rounding, potentially leaving debt accruing interest. Attacker could pick amounts that trigger mismatch.
**Insight:** When a contract asserts exact equality between a state delta and a returned amount, check whether the delta is derived through a different rounding path (share<->asset conversions) than the returned value; construct small index/share/amount combos to expose 1 wei mismatches.

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

**Repay function with balance mismatch check**

```
function _repay(AssetOperationParams memory params) internal nonReentrant {
119:  uint256 previousDebtBalance = pool.getDebt(params.asset, address(this), params.tokenId);

  DataTypes.SharesType memory repaid = pool.repay(params.asset, params.amount, params.tokenId, params.data);

121:  uint256 currentDebtBalance = pool.getDebt(params.asset, address(this), params.tokenId);
  
123:  if (previousDebtBalance - currentDebtBalance != repaid.assets) {
    revert NFTErrorsLib.BalanceMisMatch();
  }
}
```

