code-423n4 / 2022-06-notional-coop-findings

1 stars 1 forks source link

Gas Optimizations #147

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1. Save gas by stored traded market in array in DateTime.sol

Impact

In DateTime.getTradedMarket() function, it used 7 if to get result which mean it’s cost at most 7 check every time this function is called.

In DateTime.getMarketIndex() function, it loop from 1 to maxMarketIndex and call getTradedMarket() each time. Instead if we stored result of getTradedMarket() in a list like below we can save gas (at most 7*7 = 49 checks)

uint256[] list = [QUARTER, 2 * QUARTER, YEAR, 2 * YEAR, … ]

Proof of concept

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/lib/DateTime.sol#L23

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/lib/DateTime.sol#L64

Recommended Mitigation Steps

Precompute and store result of getTradedMarket() in a list