Core Contracts

Regnum Aurum Acquisition Corp
HardhatReal World AssetsNFT
77,280 USDC
View results
Submission Details
Severity: high
Invalid

Wrong Health Factor Scaling in calculateHealthFactor

Summary

The calculateHealthFactor function claims to return values in RAY (1e27) but instead operates in WAD (1e18) due to incorrect scaling. This results in health factors being 1e9 times smaller than intended,

Details

In LendingPool::calculateHealthFactor we see the dev comment says that the function should return the health factor in RAY, which is 1e27.

/**
* @notice Calculates the user's health factor
* @param userAddress The address of the user
@> * @return The health factor (in RAY)
*/
function calculateHealthFactor(
address userAddress
) public view returns (uint256) {
uint256 collateralValue = getUserCollateralValue(userAddress);
uint256 userDebt = getUserDebt(userAddress);
if (userDebt < 1) return type(uint256).max;
uint256 collateralThreshold = collateralValue.percentMul(
liquidationThreshold
);
//@audit comment says health factor is in RAY which is 1e27 but we're dealing with 1e18
return (collateralThreshold * 1e18) / userDebt;
}

But it returns the value scaled with 1e18 (WAD).

This is wrong. If we check how the user debt is calculated via getUserDebt, we see that it is also calculated with 1e27 (RAY)

* @notice Gets the user's debt including interest
* @param userAddress The address of the user
* @return The user's total debt
*/
function getUserDebt(address userAddress) public view returns (uint256) {
UserData storage user = userData[userAddress];
return user.scaledDebtBalance.rayMul(reserve.usageIndex);
}

So, the values are taken with 1e27 but scaled with 1e18, which is wrong and result in faulty calculations.

Impact

Liquidations wouldn't trigger when needed, risking protocol insolvency.

Recommendation

Change scaling factor to RAY:

return (collateralThreshold * 1e27) / userDebt;
Updates

Lead Judging Commences

inallhonesty Lead Judge 3 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
inallhonesty Lead Judge 3 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.