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

DSCEngine will not work with tokens that have no 18 decimals

Summary

DSCEngine will not work with tokens that have no 18 decimals, because of incorrect handling of decimals.

Vulnerability Details

In order to convert token amount into stablecoin amount, getUsdValue function is used. It should return stablecoin amount in e18 scaling.
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L361-L367

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;
}

This will work fine in case if collateral token has 18 decimals. But in case of other tokens, this calculation will not work.
For example 1000 usdc with price = e8

amount = e8 * e10 * 1000e6 / e18 = 1000e6

As you can see, in this case usd will be much less than it should be.

Impact

Health factor calculation will not work.

Tools Used

VsCode

Recommendations

You need to use decimals of collateral token in order to make e18 precision.

Support

FAQs

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