15,000 USDC
View results
Submission Details
Severity: gas

Use named returns in `DSCEngine.getUsdValue` function

Summary

Use named returns in DSCEngine.getUsdValue function to save some gas.

Vulnerability Details

Instead of using a return statement, add a named return to the getUsdValue function and assign the result to this named return.

Impact

Save gas for the user every time getUsdValue function is being used.

Tools Used

VSCode

Recommendations

Change the following function:
https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/d1c5501aa79320ca0aeaa73f47f0dbc88c7b77e2/src/DSCEngine.sol#L361-L367

To:

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

Support

FAQs

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