15,000 USDC
View results
Submission Details
Severity: high
Valid

getUsdValue returns incorrect value for collateral with different decimals than 18

Summary

getUsdValue returns incorrect value for collateral with different decimals than 18.

Vulnerability Details

getUsdValue returns usd value for collateral in 18 decimals:

function getUsdValue(address token, uint256 amount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
(, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
// 1 ETH = $1000
// The returned value from CL will be 1000 * 1e8
return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
}

Firstly, this function gets collateral price from Chainlink, the price is in 8 decimals;
Then the price is multiplied by ADDITIONAL_FEED_PRECISION (1e10), to make price in 18 decimals;
Price is multiplied by amount (in wei), we then get the collateral value in (collateral decimals + 18) decimals;
Lastly, the collateral value is divided by PRECISION (1e18) to make the collateral value in 18 decimals.

This works for collateral with 18 decimals, for example, assuming weth price is 2000e8, amount is 2e18, we can get collateral value is 4000e18 (2000e8 * 1e10 * 2e18 / 1e18).

However, it does not works for collateral with different decimals, in the case of wbtc, its decimals is 8, assume wbtc price is 20000e8, amount is 2e8, we can get collateral value is 0.000004e18 (20000e8 * 1e10 * 2e8 / 1e18), and this is apparently wrong.

Impact

Incorrect collateral value can lead to incorrect health factor, then incorrect amount of DCS tokens can be minted, protocol will become insolvent.

Tools Used

Mannual Review

Recommendations

In the calculation, because the collateral value we get in step 3 is in (collateral decimals + 18) decimals, so it should be divided by collateral decimals intead of 1e18. For example, wbtc's collateral value in the example above should be calculated as: 20000e8 * 1e10 * 2e8 / 1e8 = 40000e18.

Support

FAQs

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