The function KittyVault::getTotalMeowllateralInAave
is intended to calculate and return the total amount of collateral deposited in Aave, as well as the collateral earned from interest. However, the current implementation contains an error in its calculation.
The issue here is that the multiplication and division via Math::mulDiv
are performed in the wrong order, leading to an incorrect calculation of the total collateral. This results in an incorrect final value being returned by the function. The correct order of operations should first multiply the totalCollateralBase
by the collateralToUsdPrice
(adjusted for decimals) and then divide by the PRECISION
.
As a PoC and to demonstrate the bug in the implementation of KittyVault::getTotalMeowllateralInAave
we will perform the calculation of totalCollateralBase.mulDiv(PRECISION, uint256(collateralToUsdPrice) * EXTRA_DECIMALS)
using the following values:
The result of 4e16 is incorrect for representing the total collateral in USD terms. This result demonstrates the error in logic caused by the wrong order of multiplication and division.
Now we will demonstrate the correct calculation by first multiplying the TotalCollateralBase
by the CollateralToUsdPrice
(adjusted for decimals) and then dividing by the PRECISION
:
With the given collateralToUsdPrice
of 2500e8 and totalCollateralBase
of 100e18, the correctly calculated total collateral value in USD terms, adjusted to 18 decimals precision, should be 250000e18. This reflects the accurate conversion and scaling of the collateral value to USD, indicating that the calculation is consistent with the expected results.
The impact of this calculation error is significant, as it leads to the KittyVault::getTotalMeowllateral
function returning incorrect values. This can have several downstream effects, including e.g.:
Incorrect User's Collateral Checks using KittyPool::_hasEnoughMeowllateral
Incorrect Calculations and Distribution of Shares in KittyVault::executeDepawsit
Incorrect Calculations of Withdrawal Amount in KittyVault::executeWhiskdrawal
Manual review, vscode
To address the issue, the calculation in the getTotalMeowllateralInAave
function should be corrected by adjusting the order of operations:
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.