Omniscia Steadefi Audit

ChainLinkOracle Code Style Findings

ChainLinkOracle Code Style Findings

CLO-01C: Inefficient mapping Lookups

TypeSeverityLocation
Gas OptimizationChainLinkOracle.sol:L27, L32, L33

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

contracts/oracles/ChainLinkOracle.sol
26function consult(address _token) external view returns (uint256) {
27 require(tokenToPriceFeed[_token] != address(0), "No price feed available for this token");
28
29 uint256 price;
30 uint256 decimals;
31
32 (, int256 answer, , , ) = AggregatorV3Interface(tokenToPriceFeed[_token]).latestRoundData();
33 decimals = uint256(AggregatorV3Interface(tokenToPriceFeed[_token]).decimals());
34
35 price = uint256(answer) * 1e18 / (10 ** decimals);
36
37 return price;
38}

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

Alleviation (4325253d6de0ea91c1e9fb9e01d2e7e98f3d83a9):

The referenced mapping lookups have been optimized as advised, greatly reducing the gas cost of the referenced statements.