Truncation decreases accuracy in health factors' computation.
Although truncation, in this case, still makes the code's logic work as expected, improving accuracy is vital for enabling clients to make more informed decisions.
In the DSCEngine.sol
smart contract, specifically in the _calculateHealthFactor()
method, there is a loss of precision arising from the division operation with totalDscMinted
as denominator.
Let's evaluate two hypothetical yet realistic users:
User 1 đ§âđĻą
Deposit: $343
of collateral in exchange for $100
worth of stablecoin value.
Variables:
collateralValueInUsd
= 343.
totalDscMinted
= 100
Real health factor = (343 * 0.5)/100 = 1.715
User 2 đ
Deposit: $201
of collateral in exchange for $100
worth of stablecoin value.
Variables:
collateralValueInUsd
= 201.
totalDscMinted
= 100
Real health factor = (201 * 0.5)/100 = 1.005
Yet the health factors are different, the value returned by _calculateHealthFactor()
is 1 for both users. This discrepancy arises because, while the code's division values are adjusted for decimals, the numerator isn't multiplied by a significant factor to treat the result as a real number instead of an integer.
đ Notice âšī¸: Considering the existence of a public function for computing health factors in the contract, it's reasonable to assume the protocol isn't expecting clients to perform off-chain health factor computations.
This imprecision, while not causing logical errors in the protocol, can introduce confusion for users leading to misconceptions about other user positions.
Manual audit
Slither
To avoid truncation and get accurate health factor calculations:
Introduce a new constant named HEALTH_FACTOR_PRECISION
. It's value should be 10^(desired number of decimals)
.
Adjust the _calculateHealthFactor()
accordingly.
Add a getter function so clients can consult the new constant.
đ§ Note â ī¸: This code has not been tested, it's meant to serve as a reference.
Update the contract's logic that deals with health factors' values to account for this new constant.
đ§ Note â ī¸: It's crucial for users to be aware of the number of decimals in a health factor. Examples on how to consult certain information should be added in the final docs of the contract.
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.