Borrower's debt is double counted after they borrow more reserve assets.
When user borrows from LendingPool
, DebtToken
tokens are minted to the user.
If it's the first time user borrows, the minted DebtToken
amount is calculated based on amount
vaule user specified.
Because DebtToken
overrides ERC20's update()
, under the hood, the actual stored value in _balances[user]
is amount / usageIndex
.
When the user borrows more reserve assets and more DebtTokens
tokens will be minted. Protocol calls DebtToken
's balanceOf()
to retrieve user's current balance, and calculates balanceIncrease
based on user balance. The newly minted token amount is amount + balanceIncrease
.
It's import to note that DebtToken
also overrides ERC20's balanceOf()
, and this function essentially returns the user's accrued debt so far.
Assuming user borrowed 100
reserve assets when usageIndex
was 1
, to repay their debt when usageIndex
is 1.1
, they need to repay 100 / 1 * 1.1
, i.e. 110
reserve assets.
The problem is that, when user borrows more reserve assets, balanceIncrease
is minted to the user to reflect the accrued interest, but the user's existing tokens are not burned accordingly.
Suppose the newly borrowed amount is a
and balanceIncrease
is b
, then _balance[user]
is (100 / 1) + (a + b) / 1.1
, if user repays in the same block, because they do not have to pay the interest for the newly borrowed amount, they are expected to repay 110 + a
, however, the actually repaid amount is ((100 / 1) + (a + b) / 1.1)) * 1.1
, i.e. 110 + a + b
.
Therefore, the debt is double accounted.
User has to pay more funds to repay their debt.
Change LendingPool.sol#L358 as below to fix the issue that user cannot fully repay:
Change DebtToken.sol#L155 as below to fix the issue that balanceIncrease
is not correctly calculated:
Run forge test --mt testAudit_DebtIsDoubleCounted
:
Manual Review
If balanceIncrease
is non-zero, burn some DebtToken
tokens based on the current usageIndex
.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.