Open code423n4 opened 2 years ago
Minimize the Number of SLOADs by Caching State Variable
Invalid. It's read and write
Defined Variables Used Only Once
gatewayToken
should be moved out of the loop
Use Calldata instead of Memory for Read Only Function Parameters
Dup #7
Constant Value of a Call to keccak256() should Use Immutable
Dup #12
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Dup #7
Unnecessary Default Value Initialization
Dup #2
++i Costs Less Gas than i++
Dup #2
!= 0 costs less gass than > 0
Dup #17
Giving it 100 as sign of appreciation
This has been debunked for ages https://twitter.com/GalloDaSballo/status/1543729080926871557
60 for the array of bytes, for the rest pls provide benchmark next time
Rest is usual loop stuff, 300 gas
460
Really appreciated the custom finding, the rest is not impressive as other 50 submissions sent the same advice, recommend focusing on unique findings to get ahead
Table of Contents
Minimize the Number of SLOADs by Caching State Variable
Issue
SLOADs cost 100 gas where MLOADs/MSTOREs cost only 3 gas. Whenever function reads storage value more than once, it should be cached to save gas.
PoC
Mitigation
When certain state variable is read more than once, cache it to local variable to save gas.
Defined Variables Used Only Once
Issue
Certain variables is defined even though they are used only once. Remove these unnecessary variables to save gas. For cases where it will reduce the readability, one can use comments to help describe what the code is doing.
PoC
Mitigation
Don't define variable that is used only once. Details are listed on above PoC.
Use Calldata instead of Memory for Read Only Function Parameters
Issue
It is cheaper gas to use calldata than memory if the function parameter is read only. Calldata is a non-modifiable, non-persistent area where function arguments are stored, and behaves mostly like memory. More details on following link. link: https://docs.soliditylang.org/en/v0.8.15/types.html#data-location
PoC
Mitigation
Change memory to calldata
Constant Value of a Call to keccak256() should Use Immutable
Issue
When using constant it is expected that the value should be converted into a constant value at compile time. However when using a call to keccak256(), the expression is re-calculated each time the constant is referenced. Resulting in costing about 100 gas more on each access to this constant. link for more details: https://github.com/ethereum/solidity/issues/9232
PoC
Total of 15 issues found.
Mitigation
Change the variable from constant to immutable.
Using Elements Smaller than 32 bytes (256 bits) Might Use More Gas
Issue
Since EVM operates on 32 bytes at a time, it acctually cost more gas to use elements smaller than 32 bytes. Reference: https://docs.soliditylang.org/en/v0.8.15/internals/layout_in_storage.html
PoC
Total of 1 issue found.
Mitigation
I suggest using uint256 instead of anything smaller.
Unnecessary Default Value Initialization
Issue
When variable is not initialized, it will have its default values. For example, 0 for uint, false for bool and address(0) for address. Reference: https://docs.soliditylang.org/en/v0.8.15/control-structures.html#scoping-and-declarations
PoC
Total of 6 issues found.
Mitigation
I suggest removing default value initialization. For example,
++i Costs Less Gas than i++
Issue
Prefix increments/decrements (++i or --i) costs cheaper gas than postfix increment/decrements (i++ or i--).
PoC
Total of 5 issues found.
Mitigation
Change it to postfix increments/decrements. For example,