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

2 stars 0 forks source link

Gas Optimizations #211

Closed code423n4 closed 1 year ago

code423n4 commented 1 year ago

Gas Report

Optimizations found [2]

Summary Statement The main gas improvements found come from seperating conditions rather than using the binary operation &&.

[G-01] Use nested if instead of &&

Findings:

Using nested if statements saves 3 gas per &&.

/contracts/JBTiered721DelegateStore.sol Line(s): 664, 668, 678, 1050

664:    if (_i != 0 && _tierToAdd.contributionFloor < _tiersToAdd[_i - 1].contributionFloor)
668:    if (_flags.lockVotingUnitChanges && _tierToAdd.votingUnits != 0)
678:    if (_flags.lockManualMintingChanges && _tierToAdd.allowManualMint)
1050:   if (_isManualMint && !_storedTier.allowManualMint) revert CANT_MINT_MANUALLY();

Example: from

if (_i != 0 && _tierToAdd.contributionFloor < _tiersToAdd[_i - 1].contributionFloor) {
    //Inner code
}

to

if (_i != 0) {
    if (_tierToAdd.contributionFloor < _tiersToAdd[_i - 1].contributionFloor) {
        //Inner code
    }
}

[G-02] Unless Used for Variable Packing uint8 May Be More Expensive Than Using uint256

The EVM operates 32 bytes at a time. Changing uint8 to uint256 unless used for variable packing will reduce gas costs.

Findings:

/contracts/libraries/JBIpfsDecoder.sol Line(s): 48

48: uint8 digitlength = 1;

Note: The parameter of _trunctate will also have to change

Picodes commented 1 year ago

2 - invalid, 1 is below 100 gas overall

c4-judge commented 1 year ago

Picodes marked the issue as grade-c