Platform: Code4rena
Start Date: 07/07/2023
Pot Size: $121,650 USDC
Total HM: 36
Participants: 111
Period: 7 days
Judge: Picodes
Total Solo HM: 13
Id: 258
League: ETH
Rank: 74/111
Findings: 1
Award: $24.30
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: c3phas
Also found by: 0x11singh99, 0xAnah, 0xn006e7, LeoS, Rageur, Raihan, ReyAdmirado, Rolezn, SAAJ, SAQ, SM3_SS, Udsen, alymurtazamemon, hunter_w3b, koxuan, naman1778, petrichor, ybansal2403
24.2984 USDC - $24.30
This report focuses on PoolTogether Protocol contest, in context of various improvements that can be made in terms of gas cost. Some of the opportunities identified for improving gas efficiency throughout the codebase of PoolTogether protocol are categorised into 09 main areas; with further multiple instances in each of the category.
[G-01] Immutable has more gas efficiency than constant (18 Instances) [GAS 02] Using private rather than public for constant/immutable, saves gas (05 Instances) [G-03] Use hardcode address instead address(this) (09 Instances) [G 04] abi.encode() is less efficient than abi.encodepacked()(01 Instances) [G 05] String literals passed to abi.encode() should not be split by commas (01 Instances) [G-06] Use calldata instead of memory for function parameters (08 Instances) [G-07] It's cheaper to declare the variable outside the loop (02 Instances) [G-08] Structs can be packed into fewer storage slots (01 Instances) [G 09] Using bool for storage incurs overhead (06 Instances)
Using immutable instead of constant, save more gas due to avoiding storage access for state variables.
Variable values are set through constructor when using immutable, which also eliminates the need of assigning initial values to state variable making them more efficient in terms of gas cost.
Link to the Code:
Private over public saves around 21,000 for immutable and around 14,000 for constant in gas during deployment. Gas consumption also increases with number of variable created with public visibility for both constant and immutable. Link to the code:
Instead of using address(this), it is more gas-efficient to pre-calculate and use the hardcoded address.
Link to the code:
Refer to this article.
Link to the Code:
String literals can be split into multiple parts and still be considered as a single string literal. EACH new comma costs 21 gas due to stack operations and separate MSTOREs.
Link to the Code:
Using calldata in functions does not require data to be stored, which reduced the process time as compared to memory. This in return saves gas during calling the data.
Link to the Code:
Declaring a variable inside a loop result in variable being redeclared during each loop iteration which consume higher gas. The variable gets reallocated when declared outside loop making it more gas efficient.
Link to the Code:
Link to the Code:
Booleans are more expensive than uint256 or any type that takes up a full word because each write operation emits an extra SLOAD to first read the slot's contents, replace the bits taken up by the boolean, and then write back. This is the compiler's defense against contract upgrades and pointer aliasing, and it cannot be disabled.
Link to the Code:
#0 - c4-judge
2023-07-18T19:01:31Z
Picodes marked the issue as grade-b
#1 - PierrickGT
2023-09-07T23:58:30Z
G-01: these variables need to be constants G-02: not possible to use private for constants G-03: does not save any gas G-04: we can't use abi.encodePacked G-05: irrelevant suggestion G-06: fixed in the following commit: https://github.com/GenerationSoftware/pt-v5-vault/pull/45/commits/3f74638ea955fdbe9cb60c0e9d4f673af8063478 TwabLib: can't be passed as calldata G-07: has been fixed G-08: not clear how G-09: we would lose in code legibility