Submitted by hash, also found by Afriauditor and monrel
Borrowers will mostly end up paying more than the required amount of interest. This can also lead to lowered borrowing interest rates and final withdrawals to revert due to the inconsistency between the expected interest amount and the actually paid interest amount.
The interest associated with a debt is calculated both in the pool and the users debt position. However, the used method for calculation leads to different values between the pool and the debt position. In the pool it is simple linear interest, while for the debt position, the interest gets compounded.
In the pool the interest is calculated as borrowed * interestRate * (elapsedTime/365 days):
PoolV3.sol#L671-L673
In the CDP vault (i.e., users debt position), the interest is calculated as debt * interestIndexNow / interestIndexPast - debt:
CDPVault.sol#L478
CreditLogic.sol#L31-L38
Where the interestIndex is updated as follows:
PoolV3.sol#L676-L678
Hence, the interestIndex gets compounded with every invocation of _calcBaseInterestIndex, eventually causing a higher interest for the debt position when compared with the pool calculation. This also causes incorrectness between expectedLiquidity_ and availableLiquidity_ (i.e., expectedLiquidity_ will be less than availableLiquidity_).
The correct relation between expectedLiquidity_ and availableLiquidity_ is required for _updateBaseInterest which is used throughout the contract for purposes like withdrawals:
If the associated shares with the excess interest are withdrawn, it will cause an underflow for expectedLiquidity during final withdrawals
Apply the following diff and run forge test --mt testHash_CompoundingVsSimpleInterest. It is asserted that the calculated interest in the position is more than in the pool and that final withdrawals can revert due to this. The minRate == 0 check in GaugeV3 contract is commented out in order to keep quotaInterest to 0 so that the only interest being accrued is the base interest, which will make it easier for displaying the difference in its calculation.
Change the index updation to align with the pool calculation (i.e., interestIndex = prevInterestIndex + baseInterest * timeElapsed.
Math
0xtj24 (LoopFi) acknowledged
Koolex (judge) increased severity to High
