Platform: Code4rena
Start Date: 12/09/2022
Pot Size: $75,000 USDC
Total HM: 19
Participants: 110
Period: 7 days
Judge: HardlyDifficult
Total Solo HM: 9
Id: 160
League: ETH
Rank: 104/110
Findings: 1
Award: $35.35
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: CertoraInc
Also found by: 0x1f8b, 0x4non, 0x5rings, 0x85102, 0xNazgul, 0xSmartContract, 0xkatana, Amithuddar, Aymen0909, B2, Bnke0x0, CRYP70, Chom, ChristianKuri, CodingNameKiki, Deivitto, Diraco, Fitraldys, Funen, IgnacioB, JAGADESH, JC, Lambda, LeoS, Matin, Metatron, MiloTruck, Noah3o6, Ocean_Sky, Olivierdem, PaludoX0, RaymondFam, ReyAdmirado, Rohan16, Rolezn, Saintcode_, Sm4rty, SnowMan, StevenL, Tomio, Tomo, V_B, Waze, __141345__, ajtra, asutorufos, aysha, brgltd, bulej93, c3phas, ch0bu, d3e4, delfin454000, dharma09, djxploit, erictee, fatherOfBlocks, francoHacker, gianganhnguyen, gogo, got_targ, ignacio, jag, karanctf, ladboy233, leosathya, lukris02, m_Rassska, malinariy, martin, natzuu, pashov, peanuts, peiw, pfapostol, prasantgupta52, robee, simon135, slowmoses, sryysryy, tnevler
35.348 USDC - $35.35
The easiest thing to do is to optimize every for
loop, the objective is to replace those like the following example:
for (uint256 i = 0; i < array.length; i++) { //do something }
to
uint256 len = array.length; for (uint256 i; i < len;) { //do something unchecked{ ++i; } }
By doing so, the length
is cached which is cheaper than looking at it every loop, i = 0
is not initialized since uint
have already a default value of 0
and the increment is transformed to a cheaper form since it can't overflow. (This is cheaper because without unchecked
there is a check for overflow at each calculation and with the post-increment, the EVM need to store the value with and without increment, but the pre-increment only store the value with increment.)
3 instances:
Consider optimizing for
loop.
With those change, these evolutions in gas average report can be observed:
CollectionBuyCrowdfund: Deployment: 2582486 -> 2581279 (-1207) CollectionBuyCrowdfund: buy: 42786 -> 42771 (-15) TokenDistributor: Deployment: 1492622 -> 1490022 (-2600)
calldata
instead of memory
for read only argument in external functionIf a function parameter is read only, it is cheaper in gas to use calldata
instead of memory
.
7 instance:
https://github.com/PartyDAO/party-contracts-c4/blob/main/contracts/party/Party.sol#L33
https://github.com/PartyDAO/party-contracts-c4/blob/main/contracts/party/PartyFactory.sol#L28-L30
Consider changing memory
to calldata
in these lines
With those change, these evolutions in gas average report can be observed:
AuctionCrowdfund: Deployment: 2790146 -> 2817977 (+27831) AuctionCrowdfund: initialize: 209495 -> 207640 (-1855) AuctionCrowdfund: finalize: 30202 -> 29426 (-776) CollectionBuyCrowdfund: Deployment: 2582486 -> 2613717 (+31231) CollectionBuyCrowdfund: buy: 42786 -> 42736 (-50) Party: Deployment: 4528700 -> 4570961 (+42261) Party: initialize: 182337 -> 177648 (-4689) PartyFactory: Deployment: 655926 -> 637307 (-18619) PartyFactory: createParty: 297056 -> 291766 (-5290)