code-423n4 / 2022-10-juicebox-findings

2 stars 0 forks source link

Gas Optimizations #164

Open code423n4 opened 1 year ago

code423n4 commented 1 year ago

1. Explicitly assingning default values to variables is a waste of gas

Use uint256 i; instead of uint256 i = 0;

contracts/libraries/JBIpfsDecoder.sol: L49 L68 L76 L84

2. Cache storage variables in function call stack to save gas

contracts/JBTiered721Delegate.sol: L143-L152

3. Prefix incrementing and decrementing costs around 6 gas less than the postfix ones

e.g. ++var is cheaper than var++

contracts/libraries/JBIpfsDecoder.sol: L68 L76 L84

contracts/JBTiered721DelegateStore.sol: L1108

4. use x = x + y instead of x+= y

contracts/JBTiered721DelegateStore.sol: L827

5. Calldata is cheaper than memory for function input

contracts/abstract/JB721Delegate.sol: L206 L311 L323

contracts/libraries/JBIpfsDecoder.sol: L22 L44 L74 L82

contracts/libraries/JBBitmap.sol: L29 L59

contracts/JBTiered721DelegateDeployer.sol: L71

contracts/JBTiered721DelegateStore.sol: L628 L1091 L1227

contracts/JBTiered721Delegate.sol: L205 L208 L210 L211 L264 L290 L480 L598 L652 L789

contracts/JB721TieredGovernance.sol: L147 L313

contracts/JB721GlobalGovernance.sol: L55

contracts/JBTiered721DelegateProjectDeployer.sol: L72 L73 L109 L110 L152 L191 L218

6. Use x < y + 1 in stead of x <= y

contracts/JB721TieredGovernance.sol: L133

contracts/JBTiered721DelegateStore.sol: L903

7. When comparing variables of type uint, use require(x != 0) instead of require(x > 0)

contracts/libraries/JBIpfsDecoder.sol: L57

contracts/JBTiered721DelegateStore.sol: L1254

8. Use constant and immutable for constants

contracts/JBTiered721Delegate.sol: L48

9. Consider marking functions as payable if there is no risk of sending value through them

This change will save gas each time a function is called

contracts/JBTiered721Delegate.sol: L370 L402 L418

10. Place i++ in an unchecked blocks in for-loops

contracts/libraries/JBIpfsDecoder.sol: L49 L51 L68 L76

contracts/JBTiered721Delegate.sol: L341 L355

c4-judge commented 1 year ago

Picodes marked the issue as grade-b