code-423n4 / 2022-05-factorydao-findings

1 stars 1 forks source link

Gas Optimizations #199

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Title: Use immutable variables

Impact

For any variable that is only set once, either use a constant or immutable to save gas. The compiler does not reserve a storage slot for these variables, and every occurrence is replaced by the respective value (Source: https://docs.soliditylang.org/en/v0.6.5/contracts.html#constant-and-immutable-state-variables)

Recommended Mitigation Steps

Change variables as immutable.

globalBeneficiary PermissionlessBasicPoolFactory.sol: https://github.com/code-423n4/2022-05-factorydao/blob/main/contracts/PermissionlessBasicPoolFactory.sol#L51

gateMaster MerkleEligibility.sol: https://github.com/code-423n4/2022-05-factorydao/blob/main/contracts/MerkleEligibility.sol#L16


Title: Use Solidity v0.8.4 because of Solidity Custom Errors

Impact

Starting from Solidity v0.8.4, there is a convenient and gas-efficient way to explain to users why an operation failed through the use of custom errors. Custom errors decrease both deploy and runtime gas costs. Source: https://blog.soliditylang.org/2021/04/21/custom-errors/

OpenZeppelin is also planing to use custom errors starting with next major release 5.0. Source release v5.0: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2961 Source OZ custom error issue: https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2839

Recommended Mitigation Steps

Refactor code and use Solidity custom errors.


Title: Unused .call() return data

Impact

Removing unused return variable improves code readability and can improve gas costs

Recommended Mitigation Steps

Change: (bool sent, bytes memory data) = gate.beneficiary.call{value: msg.value}(""); To: (bool sent, ) = gate.beneficiary.call{value: msg.value}(""); https://github.com/code-423n4/2022-05-factorydao/blob/main/contracts/SpeedBumpPriceGate.sol#L79

illuzen commented 2 years ago

All duplicates

gititGoro commented 2 years ago

For the customs error item, the code uses a newer version of solidity than 0.8.4. The suggestion will simply be treated as "use custom errors".