code-423n4 / 2022-04-phuture-findings

0 stars 0 forks source link

Gas Optimizations #91

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

2022-04-phuture gas optimization

1 use != 0 instead of > 0. != is cheaper than > 0.

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/libraries/NAV.sol#L49 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L61 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L98 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L56 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhutureIndex.sol#L64

if (i != 0) {} if (newWeight != 0) { if (shares != 0) {}

2 missing input validation for _balance in burn. If _balance is 0, the amount will be always 0. You have validation for the amount. However, if you have validation for _balance, you can avoid unnecessary execution.

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/libraries/NAV.sol#L56

require(_balance != 0, “error message”);

3 use unchecked for the following calculation. The underflow is already checked in if sentence, so you can use the unchecked to save gas costs.

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndex.sol#L50-L52

if (totalWeight < IndexLibrary.MAX_WEIGHT) { unchecked { weightOf[maxCapitalizationAsset] += IndexLibrary.MAX_WEIGHT - totalWeight; } }

4 use cache for weightOf[maxCapitalizationAsset] in initialize. weightOf[maxCapitalizationAsset] will be called in event too. You can save gas costs by using cache.

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndex.sol#L50-L53

uint8 _weigtOf = IndexLibrary.MAX_WEIGHT - totalWeight; if (totalWeight < IndexLibrary.MAX_WEIGHT) { weightOf[maxCapitalizationAsset] += _weightOf; } emit UpdateAnatomy(maxCapitalizationAsset, _weightOf);