Platform: Code4rena
Start Date: 10/05/2022
Pot Size: $50,000 USDC
Total HM: 13
Participants: 100
Period: 5 days
Judge: HardlyDifficult
Total Solo HM: 1
Id: 122
League: ETH
Rank: 84/100
Findings: 1
Award: $30.12
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: IllIllI
Also found by: 0v3rf10w, 0x1f8b, 0x4non, 0xDjango, 0xNazgul, 0xf15ers, 0xkatana, 0xsanson, Bludya, BowTiedWardens, CertoraInc, Cityscape, DavidGialdi, FSchmoede, Fitraldys, Funen, Hawkeye, Kenshin, MadWookie, MaratCerby, MiloTruck, Picodes, RagePit, Tadashi, TerrierLover, TomFrenchBlockchain, VAD37, WatchPug, Waze, _Adam, antonttc, bobirichman, catchup, defsec, delfin454000, djxploit, ellahi, fatherOfBlocks, gzeon, hake, hansfriese, hickuphh3, horsefacts, ignacio, joestakey, jonatascm, mics, minhquanym, oyc_109, pmerkleplant, rfa, robee, rotcivegaf, samruna, shung, sikorico, simon135, z3s
30.1234 USDC - $30.12
Solidity does not recognize null as a value, so uint variables are initialized to zero. Setting a uint variable to zero is redundant and can waste gas.
There was one place where an int is initialized to zero https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L94 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L95 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L282 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/CallyNft.sol#L244
Remove the redundant zero initialization
uint256 i;
instead of uint256 i = 0;
Strings in solidity are handled in 32 byte chunks. A require string longer than 32 bytes uses more gas. Shortening these strings will save gas.
One instance was found https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/CallyNft.sol#L89
Shorten all require strings to less than 32 characters
Using a prefix increment (++i) instead of a postfix increment (i++) saves gas for each loop cycle and so can have a big gas impact when the loop executes on a large number of elements.
There is one example of this in for loops https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L244
Use prefix not postfix to increment in a loop
Using > 0
uses slightly more gas than using != 0
. Use != 0
when comparing uint variables to zero, which cannot hold values below zero
Locations where this was found include https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L170 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L283
Replace > 0
with != 0
to save gas
Identifying a function as payable saves gas. Functions that have the onlyOwner modifier cannot be called by normal users and will not mistakenly receive ETH. These functions can be payable to save gas. This is especially relevant because withdrawAll functions exist to withdraw any ETH accidentally sent.
There are many functions that have the onlyOwner modifier in the contracts. Some examples are https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L119 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L124
Add payable to these functions for gas savings
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. This saves gas.
This was found in one place https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L244
Cache the array length before the for loop
getPremium
caches the value of _vaults[vaultId] and then returns the value premiumOptions[vault.premiumIndex]. But everywhere that getPremium
is used, the _vaults[vaultId] value is already cached, so using the value premiumOptions[vault.premiumIndex] directly saves gas by removing a function call.
There are two places where this improvement can be made https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L223 https://github.com/code-423n4/2022-05-cudos/blob/main/solidity/contracts/Cally.sol#L464
Replace getPremium
calls with the value premiumOptions[vault.premiumIndex]