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

1 stars 1 forks source link

Gas Optimizations #248

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas

Make variable uninitialized save sstore 25,000 gas saved https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleDropFactory.sol#L17 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleEligibility.sol#L31 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleVesting.sol#L16 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/VoterID.sol#L69 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleDropFactory.sol#L17 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleVesting.sol#L16 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleResistor.sol#L24 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/VoterID.sol#L69 Save gas by making memory variable instead of adding a storage variable then you can make it = to a storage variable Use : Pool storage pool = pools[++_numPools]; numPools=_numPools;

Check gas with remix gas profiler With the change : 1643738 Without the change : 1644998

Revert string in require longer than 32 bytes (1 byte a character) https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/PermissionlessBasicPoolFactory.sol#L114 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleDropFactory.sol#L92 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/PermissionlessBasicPoolFactory.sol#L114

Make constant variable immutable to save gas and its only write on deployment and not on transaction https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleResistor.sol#L59 Make events with 3 or more variables indexed (except strings and bytes) https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleResistor.sol#L62 Tested in remix in gas profiler Without change 1002003
With change 1000275

Dont use ++numtrees on storing the uint of mappings instead use a value from memory and you save alot of gas bec you dont use sload alot and instead your using mload saves alot of gas https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleResistor.sol#L86 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/FixedPricePassThruGate.sol#L30

Function with onlyManagement function should use payable saves gas because your not checking msg.value==0 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleEligibility.sol#L85 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleIdentity.sol#L60 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleIdentity.sol#L67 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleIdentity.sol#L75

Instead of numGates += 1; ++numGates; Saves alot of gas bec of not making multiple assignments https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleEligibility.sol#L91 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleEligibility.sol#L47 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleEligibility.sol#L93 https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/MerkleLib.sol#L22 Should not have anything with storage in a for loop can dos and cost alot of gas https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/PermissionlessBasicPoolFactory.sol#L118 Iszero instruction is more gas efficient than x ==0 in require statement require(owners[thisToken] assembly { owners[thisToken]:=iszero(thisOwner)}, 'Token already exists'); https://github.com/code-423n4/2022-05-factorydao/blob/fc85803cdd6f63c966721855ba52acc04878a716/contracts/VoterID.sol#L124

illuzen commented 2 years ago

all duplicates

gititGoro commented 2 years ago

bonus points for including gas numbers.