Platform: Code4rena
Start Date: 26/09/2022
Pot Size: $50,000 USDC
Total HM: 13
Participants: 113
Period: 5 days
Judge: 0xean
Total Solo HM: 6
Id: 166
League: ETH
Rank: 102/113
Findings: 1
Award: $24.02
π Selected for report: 0
π Solo Findings: 0
π Selected for report: IllIllI
Also found by: 0x1f8b, 0x5rings, 0xNazgul, 0xRoxas, 0xSmartContract, 0xbepresent, 0xmatt, Aeros, Amithuddar, Awesome, Aymen0909, B2, Bnke0x0, ChristianKuri, CodingNameKiki, Deivitto, Diraco, Fitraldys, HardlyCodeMan, JC, Mukund, Noah3o6, Olivierdem, RaymondFam, ReyAdmirado, RockingMiles, Rolezn, Ruhum, Saintcode_, Shinchan, SnowMan, TomJ, Tomio, Tomo, V_B, Waze, __141345__, ajtra, asutorufos, aysha, beardofginger, bobirichman, brgltd, bulej93, c3phas, ch0bu, cryptonue, defsec, delfin454000, dharma09, durianSausage, emrekocak, erictee, fatherOfBlocks, francoHacker, gianganhnguyen, gogo, imare, kaden, karanctf, ladboy233, lukris02, m_Rassska, martin, medikko, mics, natzuu, oyc_109, peiw, rbserver, ret2basic, rotcivegaf, saian, shark, slowmoses, tnevler, trustindistrust, zeesaw, zishansami
24.0205 USDC - $24.02
If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). Explicitly initializing it with its default value is an anti-pattern and wastes gas.
Instances include: DataStorage.sol#L307
for (uint256 i; i < secondsAgos.length; ++i)
In the context of a for-loop that iterates over an array, it costs less gas to cache the array's length in a variable and read from this variable rather than use the arrays .length property. Reading the .length property for on the array will cause a recalculation of the array's length on each iteration of the loop which is a more expensive operation than reading from a stack variable.
For example, the following code:
for (uint i; i < arr.length; ++i) { // ... }
This extra costs can be avoided by caching the array length (in stack): should be changed to:
uint length = arr.length; for (uint i; i < length; ++i) { // ... }
!= 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled (6 gas)
For uints the minimum value would be 0 and never a negative value. Since it cannot be a negative value, then the check > 0 is essentially checking that the value is not equal to 0 therefore >0 can be replaced with !=0 which saves gas.
While it may seem that > 0 is cheaper than !=, this is only true without the optimizer enabled and outside a require statement. If you enable the optimizer at 10k AND youβre in a require statement, this will save gas.
Instances include: PriceMovementMath.sol#L52 PriceMovementMath.sol#L53
require(price != 0); require(liquidity != 0);
AlgebraPool.sol#L224 AlgebraPool.sol#L434 AlgebraPool.sol#L454 AlgebraPool.sol#L454 AlgebraPool.sol#L454 AlgebraPool.sol#L454 AlgebraPool.sol#L454 AlgebraPool.sol#L454
&&
operator can be broken down in multiple require statements to save gas.Instances include: DataStorageOperator.sol#L46 changes:
require(_feeConfig.gamma1 != 0, 'Gammas must be > 0'); require(_feeConfig.gamma2 != 0, 'Gammas must be > 0'); require(_feeConfig.volumeGamma != 0, 'Gammas must be > 0');
require(gamma1 != 0, 'Gammas must be > 0'); require(gamma2 != 0 , 'Gammas must be > 0'); require(volumeGamma != 0, 'Gammas must be > 0');
AlgebraPool.sol#L739 AlgebraPool.sol#L743
require(limitSqrtPrice < currentPrice, 'SPL'); require(limitSqrtPrice > TickMath.MIN_SQRT_RATIO, 'SPL');
AlgebraPool.sol#L953 AlgebraPool.sol#L968
require((communityFee0 <= Constants.MAX_COMMUNITY_FEE); require(communityFee1 <= Constants.MAX_COMMUNITY_FEE));