15,000 USDC
View results
Submission Details
Severity: gas

Can return from the `getUsdValue()` method if amount is 0

Summary

Can return from the getUsdValue() method if amount is 0

Vulnerability Details

If the amount passed is 0, then the USD value will be 0. There is no point in calling the oracle and performing bunch of operations just to multiply the final answer with 0.

File: src/DSCEngine.sol
361: 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;
}

Link to code - https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/DSCEngine.sol#L361

Tools Used

Code Review using VSCode

Recommendations

Add amount == 0 check and return early.

Code change:

File: src/DSCEngine.sol
361: function getUsdValue(address token, uint256 amount) public view returns (uint256) {
+ if (amount == 0) {
+ return 0;
+ }
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
...
...
...
}

Support

FAQs

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