Platform: Code4rena
Start Date: 03/05/2023
Pot Size: $60,500 USDC
Total HM: 25
Participants: 114
Period: 8 days
Judge: Picodes
Total Solo HM: 6
Id: 234
League: ETH
Rank: 98/114
Findings: 1
Award: $22.28
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: JCN
Also found by: 0x73696d616f, 0xSmartContract, 0xnev, Audit_Avengers, Aymen0909, Blckhv, Eurovickk, K42, Kenshin, Rageur, Raihan, ReyAdmirado, SAAJ, SAQ, Shubham, Tomio, Walter, ayden, codeslide, descharre, dicethedev, hunter_w3b, j4ld1na, kaveyjoe, okolicodes, patitonar, petrichor, pontifex, yongskiws
22.2767 USDC - $22.28
This report focuses on Ajna 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 Ajna protocol are categorised into 12 main areas; with further multiple instances in each of the category.
Use latest version of solidity i.e., 0.8.19 for getting additional gas saving for reference check this article.
Link to the code:
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:
Instead of using address(this), it is more gas-efficient to pre-calculate and use the hardcoded address.
Link to the code:
Compound assignment operators (+= / -=) are more expensive in terms of gas consumption and needs to be avoided.
Operator assignments (a = a + b / a - b) are preferable in terms of gas optimization.
Link to the Code:
Link to the Code: https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-grants/src/grants/base/StandardFunding.sol#L129 https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-grants/src/grants/base/StandardFunding.sol#L641
External call to a public library function is very expensive for reference checks this article. When library has only internal functions it can be used as internal library.
Link to the code:
https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-grants/src/grants/libraries/Maths.sol
Link to the Code:
https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/RewardsManager.sol#L96
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:
If not cached, the solidity compiler will always read the length of the array during each iteration. That is, if it is a storage array, this is an extra sload operation (100 additional extra gas for each iteration except for the first) and if it is a memory array, this is an extra mload operation (3 additional gas for each iteration except for the first).
Link to the Code: https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-grants/src/grants/base/Funding.sol#L62
Using calldata in external function 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:
Functions with public visibility, if not called within the contract needed to be changed external.
Link to the Code:
https://github.com/code-423n4/2023-05-ajna/blob/main/ajna-core/src/PositionManager.sol#L517
#0 - c4-judge
2023-05-17T12:20:27Z
Picodes marked the issue as grade-b