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();
return ((uint256(price) * ADDITIONAL_FEED_PRECISION) * amount) / PRECISION;
}
contract DSCEngine
function getTokenAmountFromUsd(address token, uint256 usdAmountInWei) public view returns (uint256) {
AggregatorV3Interface priceFeed = AggregatorV3Interface(s_priceFeeds[token]);
- (, int256 price,,,) = priceFeed.staleCheckLatestRoundData();
+ int256 price = priceFeed.staleCheckLatestRoundData();
return (usdAmountInWei * PRECISION) / (uint256(price) * ADDITIONAL_FEED_PRECISION);
}