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

Remove unused variables in `OracleLib`

Summary

Currently OracleLib.staleCheckLatestRoundData() returns 4 variables (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) which are returned from latestRoundData()

In the current implementation, only answer variable is used. By removing all the unused properties in both library and at the places where library method is used we will save roughly 400 gas.

Instances

https://github.com/Cyfrin/2023-07-foundry-defi-stablecoin/blob/main/src/libraries/OracleLib.sol#L21-L33

Tools Used

Manual

Recommendations

Updated OracleLib

library OracleLib
function staleCheckLatestRoundData(AggregatorV3Interface priceFeed)
public
view
- returns (uint80, int256, uint256, uint256, uint80)
+ returns (int256)
{
- (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
priceFeed.latestRoundData();
+ (, int256 answer,, uint256 updatedAt,) = priceFeed.latestRoundData();
uint256 secondsSince = block.timestamp - updatedAt;
if (secondsSince > TIMEOUT) revert OracleLib__StalePrice();
- return (roundId, answer, startedAt, updatedAt, answeredInRound);
+ return (answer);
}
contract DSCEngine
function getUsdValue(address token, uint256 amount) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
- (, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
+ int256 price = priceFeed.staleCheckLatestRoundData();
// 1 ETH = $1000
// The returned value from CL will be 1000 * 1e8е
return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
}
contract DSCEngine
function getTokenAmountFromUsd(address token, uint256 usdAmountInWei) public view returns (uint256) {
// price of ETH (token)
// $/ETH ETH ??
// $2000 / ETH. $1000 = 0.5 ETH
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
- (, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
+ int256 price = priceFeed.staleCheckLatestRoundData();
// ($10e18 * 1e18) / ($2000e8 * 1e10)
return (usdAmountInWei * PRECISION) / (uint256(price) * ADDITIONAL_FEED_PRECISION);
}

Support

FAQs

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