[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
ISSUE LIST
C4-001 : Adding unchecked directive can save gas [S]
C4-002 : Check if amount > 0 before token transfer can save gas [S]
C4-003 : There is no need to assign default values to variables [S]
C4-004 : Using operator && used more gas [S]
C4-005 : Non-strict inequalities are cheaper than strict ones [M]
C4-006 : Cache array length in for loops can save gas [S]
C4-007 : Use calldata instead of memory for function parameters [M]
C4-008 : ++i is more gas efficient than i++ in loops forwarding
C4-009 : > 0 can be replaced with != 0 for gas optimization
C4-010 : Keccak functions in constants waste gas [M]
C4-011 : Free gas savings for using solidity 0.8.10+ [S]
C4-001 : Adding unchecked directive can save gas
Impact
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.
Proof of Concept
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Tools Used
None
Recommended Mitigation Steps
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
Unchecked{i++};
C4-002 : Check if amount > 0 before token transfer can save gas
Impact
Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.
using &&:
function check(uint x)public view{
require(x == 0 && x < 1 );
}
// gas cost 21630
using double require:
require(x == 0 );
require( x < 1);
}
}
// gas cost 21622
C4-005 : Non-strict inequalities are cheaper than strict ones
Impact
Strict inequalities add a check of non equality which costs around 3 gas.
Proof of Concept
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Tools Used
Code Review
Recommended Mitigation Steps
Use >= or <= instead of > and < when possible.
C4-006 : Cache array length in for loops can save gas
Impact
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Proof of Concept
Navigate to the following smart contract line.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Tools Used
None
Recommended Mitigation Steps
Consider to cache array length.
C4-008 : Use calldata instead of memory for function parameters
Impact
In some cases, having function arguments in calldata instead of
memory is more optimal.
Consider the following generic example:
contract C {
function add(uint[] memory arr) external returns (uint sum) {
uint length = arr.length;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i];
}
}
}
In the above example, the dynamic array arr has the storage location
memory. When the function gets called externally, the array values are
kept in calldata and copied to memory during ABI decoding (using the
opcode calldataload and mstore). And during the for loop, arr[i]
accesses the value in memory using a mload. However, for the above
example this is inefficient. Consider the following snippet instead:
contract C {
function add(uint[] calldata arr) external returns (uint sum) {
uint length = arr.length;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i];
}
}
}
In the above snippet, instead of going via memory, the value is directly
read from calldata using calldataload. That is, there are no
intermediate memory operations that carries this value.
Gas savings: In the former example, the ABI decoding begins with
copying value from calldata to memory in a for loop. Each iteration
would cost at least 60 gas. In the latter example, this can be
completely avoided. This will also reduce the number of instructions and
therefore reduces the deploy time cost of the contract.
In short, use calldata instead of memory if the function argument
is only read.
Note that in older Solidity versions, changing some function arguments
from memory to calldata may cause "unimplemented feature error".
This can be avoided by using a newer (0.8.*) Solidity compiler.
Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.
C4-009 : ++i is more gas efficient than i++ in loops forwarding
Impact
++i is more gas efficient than i++ in loops forwarding.
Proof of Concept
Navigate to the following contracts.
2022-07-axelar-main/contracts/auth/AxelarAuthWeighted.sol::116 => for (uint256 i; i < accounts.length - 1; ++i) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::114 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::168 => for (uint256 i; i < refundTokens.length; i++) {
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::204 => for (uint256 i; i < refundTokens.length; i++) {
Tools Used
Code Review
Recommended Mitigation Steps
It is recommend to use unchecked{++i} and change i declaration to uint256.
C4-010 : > 0 can be replaced with != 0 for gas optimization
Impact
!= 0 is a cheaper operation compared to > 0, when dealing with uint.
Proof of Concept
Navigate to the following contract sections.
2022-07-axelar-main/contracts/deposit-service/AxelarDepositService.sol::165 => if (addressForNativeDeposit(salt, refundAddress, destinationChain, destinationAddress).balance > 0 && msg.sender != refundAddress)
2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::23 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance);
2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::51 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance);
2022-07-axelar-main/contracts/deposit-service/ReceiverImplementation.sol::71 => if (address(this).balance > 0) refundAddress.transfer(address(this).balance);
Tools Used
None
Recommended Mitigation Steps
Consider to replace > 0 with != 0 for gas optimization.
C4-011 : Keccak functions in constants waste gas
Impact
The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.
Solidity 0.8.13 has some improvements too but not well tested.
Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist
[S]: Suggested optimation, save a decent amount of gas without compromising readability;
[M]: Minor optimation, the amount of gas saved is minor, change when you see fit;
[N]: Non-preferred, the amount of gas saved is at cost of readability, only apply when gas saving is a top priority.
ISSUE LIST
C4-001 : Adding unchecked directive can save gas [S]
C4-002 : Check if amount > 0 before token transfer can save gas [S]
C4-003 : There is no need to assign default values to variables [S]
C4-004 : Using operator && used more gas [S]
C4-005 : Non-strict inequalities are cheaper than strict ones [M]
C4-006 : Cache array length in for loops can save gas [S]
C4-007 : Use calldata instead of memory for function parameters [M]
C4-008 : ++i is more gas efficient than i++ in loops forwarding
C4-009 :
> 0
can be replaced with!= 0
for gas optimizationC4-010 : Keccak functions in constants waste gas [M]
C4-011 : Free gas savings for using solidity 0.8.10+ [S]
C4-001 : Adding unchecked directive can save gas
Impact
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.
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider applying unchecked arithmetic where overflow/underflow is not possible. Example can be seen from below.
C4-002 : Check if amount > 0 before token transfer can save gas
Impact
Since _amount can be 0. Checking if (_amount != 0) before the transfer can potentially save an external call and the unnecessary gas cost of a 0 token transfer.
Proof of Concept
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider checking amount != 0.
C4-003 : There is no need to assign default values to variables
Impact - Gas Optimization
Uint is default initialized to 0. There is no need assign false to variable.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
uint x = 0 costs more gas than uint x without having any different functionality.
C4-004 : Using operator && used more gas
Impact
Using double require instead of operator && can save more gas.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Example
C4-005 : Non-strict inequalities are cheaper than strict ones
Impact
Strict inequalities add a check of non equality which costs around 3 gas.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
Use >= or <= instead of > and < when possible.
C4-006 : Cache array length in for loops can save gas
Impact
Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider to cache array length.
C4-008 : Use calldata instead of memory for function parameters
Impact
In some cases, having function arguments in calldata instead of memory is more optimal.
Consider the following generic example:
In the above example, the dynamic array arr has the storage location memory. When the function gets called externally, the array values are kept in calldata and copied to memory during ABI decoding (using the opcode calldataload and mstore). And during the for loop, arr[i] accesses the value in memory using a mload. However, for the above example this is inefficient. Consider the following snippet instead:
In the above snippet, instead of going via memory, the value is directly read from calldata using calldataload. That is, there are no intermediate memory operations that carries this value.
Gas savings: In the former example, the ABI decoding begins with copying value from calldata to memory in a for loop. Each iteration would cost at least 60 gas. In the latter example, this can be completely avoided. This will also reduce the number of instructions and therefore reduces the deploy time cost of the contract.
In short, use calldata instead of memory if the function argument is only read.
Note that in older Solidity versions, changing some function arguments from memory to calldata may cause "unimplemented feature error". This can be avoided by using a newer (0.8.*) Solidity compiler.
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Some parameters in examples given above are later hashed. It may be beneficial for those parameters to be in memory rather than calldata.
C4-009 : ++i is more gas efficient than i++ in loops forwarding
Impact
++i is more gas efficient than i++ in loops forwarding.
Proof of Concept
Tools Used
Code Review
Recommended Mitigation Steps
It is recommend to use unchecked{++i} and change i declaration to uint256.
C4-010 :
> 0
can be replaced with!= 0
for gas optimizationImpact
!= 0
is a cheaper operation compared to> 0
, when dealing with uint.Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Consider to replace
> 0
with!= 0
for gas optimization.C4-011 : Keccak functions in constants waste gas
Impact
The contracts assigns two constants to the result of a keccak operation, which results in gas waste since the expression is computed each time the constant is accessed.
See this issue for more context: ethereum/solidity#9232 (https://github.com/ethereum/solidity/issues/9232)
Proof of Concept
Tools Used
None
Recommended Mitigation Steps
Replace the constant directive with immutable, or assign the already hashed value to the constants.
C4-012 : Free gas savings for using solidity 0.8.10+
Impact
Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.
Proof of Concept
Solidity 0.8.13 has a useful change which reduced gas costs of external calls which expect a return value: https://blog.soliditylang.org/2021/11/09/solidity-0.8.10-release-announcement/
Solidity 0.8.13 has some improvements too but not well tested.
Code Generator: Skip existence check for external contract if return data is expected. In this case, the ABI decoder will revert if the contract does not exist
All Contracts
Tools Used
None
Recommended Mitigation Steps
Consider to upgrade pragma to at least 0.8.13.