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

Incorrect decimals on USD calculation

Summary

The code consider the return decimals from price feed is always 8 decimals but there is some cases where the decimals is different, like AMPL/USDC which have 18 decimals, this can lead to users mint more or can't be liquidated.

Vulnerability Details

The code consider to use any kind of token to create a stable coin, from description we hare: "The system is meant to be such that someone could fork this codebase, swap out WETH & WBTC for any basket of assets they like, and the code would work the same." In case of one collateral token be AMPL, which returns 18 decimals from price feed, the calculation for getTokenAmountFromUsd and getUsdValue would be incorrect, leading the user to mint more than expected and can't be liquidated.

POC

The getUsdValue is used to get total value in USD in collateral that the user have, and further used to calculate the account health factor. The incorrect decimals calculation would leave the user with larger health factor, check the calculation:

//price = 1e18 (AMPL/USDC)
//amount = 10e18 AMPL tokens
//ADDITIONAL_FEED_PREICION = 1e10
//PRECISION = 1e18
valueUSD = price * ADDITIONAL_FEED_PRECISION * amount / PRECISION
valueUSD = 1e18 * 1e10 * 10e18 / 1e18 = 10e28

The correct value would be 10e18 and not 10e28, the inflating value in USD leave the user with better health factor than expected.

Now, checking the getTokenAmountFromUsd function, this function is called in liquidate function to calculate the amount of collateral the liquidator will get from account.

//price = 1e18 (AMPL/USDC)
//usdAmountInWei = 1e18
//PRECISION = 1e18
//ADDITIONAL_FEED_PRECISION = 1e10
collateral = (usdAmountInWei * PRECISION) / (uint256(price) * ADDITIONAL_FEED_PRECISION);
collateral = (1e18 * 1e18) / 1e18 * 1e10 = 1e8

The expected value here is 1e18 but the returned is 1e8, this means the liquidator will get less collateral than he should get and in some cases he would get nothing, just paying the liquidator debts

Impact

In case of one of collateral list is AMPL, the contract will be exposed to be exploited by AMPL holders.

Tools Used

Manual Review

Recommendations

Consider using the price feed decimals instead of a constant ADDITIONAL_FEED_PRECISION

Support

FAQs

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