code-423n4 / 2022-07-axelar-findings

0 stars 0 forks source link

Gas Optimizations #154

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Optimazations

[G-01] Use prefix not postfix in loops

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 are 5 occurrences

Recommended Mitigation Steps

Use prefix not postfix to increment in a loop

[G-02] Redundant zero initialization

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 are 6 occurrences

Recommended Mitigation Steps

Remove the redundant zero initialization

uint256 amount;

[G-03] Use != 0 instead of > 0

Using > 0 uses slightly more gas than using != 0. Use != 0 when comparing uint variables to zero, which cannot hold values below zero.

There are 9 occurrences

Recommended Mitigation Steps

Replace > 0 with != 0 to save gas

[G-04] Adding unchecked directive can save gas

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

There are 11 occurrences

Recommended Mitigation Steps

Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.

    for (uint256 i; i < length;) {
        // ...
        unchecked { ++i };
    }

[G-05] Use Custom Errors instead of revert()/require()

Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met)

Revert function names are used as strings instead of the error functions.

There are 15 occurrences

Recommended Mitigation Steps

Recommended to replace revert strings with custom errors.

//current
if (axelarToken == address(0)) revert('NotAxelarToken()');

//fix
if (axelarToken == address(0)) revert NotAxelarToken();
re1ro commented 2 years ago

Dup #2 #3

GalloDaSballo commented 2 years ago

Saves less than 300 gas