Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas Optimizations #35

Open
code423n4 opened this issue Mar 12, 2022 · 0 comments
Open

Gas Optimizations #35

code423n4 opened this issue Mar 12, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

C4-001 : Upgrade pragma to at least 0.8.4

Impact

Using newer compiler versions and the optimizer gives gas optimizations
and additional safety checks are available for free.

The advantages of versions 0.8.* over <0.8.0 are:

  • Safemath by default from 0.8.0 (can be more gas efficient than
    library based safemath.)
  • Low level inliner : from 0.8.2, leads to cheaper runtime gas. Especially relevant when the contract has small functions. For example, OpenZeppelin libraries typically have a lot of small helper functions and if they are not inlined, they cost an additional 20 to 40 gas because of 2 extra jump instructions and additional stack operations needed for function calls.
  • Optimizer improvements in packed structs: Before 0.8.3, storing packed structs, in some cases used an
    additional storage read operation. After EIP-2929, if the slot was already cold, this means unnecessary stack operations and extra deploy time costs. However, if the slot was already warm, this means
    additional cost of 100 gas alongside the same unnecessary stack operations and extra deploy time costs.
  • Custom errors from 0.8.4, leads to cheaper deploy time cost and run time cost. Note: the run time cost is only relevant when the revert condition is met. In short, replace revert strings by custom errors.

Proof of Concept

  1. The contest repository contracts contain floating pragma 0.8.0. The contracts pragma version can be updated to 0.8.4 for the gas optimization.

All Contracts

Tools Used

None

Recommended Mitigation Steps

Consider to upgrade pragma to at least 0.8.4.

C4-002 : ABI Coder V2 For Gas Optimization

Impact

From Pragma 0.8.0, ABI coder v2 is activated by default. The pragma abicoder v2 can be deleted from the repository. That will provide gas optimization.

Proof of Concept

  1. Navigate to the following contract

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityPool.sol#L4

Tools Used

None

Recommended Mitigation Steps

ABI coder v2 is activated by default. It is recommended to delete redundant codes.

From Solidity v0.8.0 Breaking Changes https://docs.soliditylang.org/en/v0.8.0/080-breaking-changes.html

C4-003 : Cache array length in for loops can save gas

Impact - Gas Optimization

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

  1. Navigate to the following contracts.
https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/WhitelistPeriodManager.sol#L228

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/token/LPToken.sol#L77

Tools Used

Code Review

Recommended Mitigation Steps

Consider to cache array length.

C4-004 : uint is always >= 0

Impact

These checks are pretty much useless as uint can never be negative. Remove them to save some gas.

Example :

for( uint i; i<5;i++)

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/WhitelistPeriodManager.sol#L228

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityFarming.sol#L233

Tools Used

None

Recommended Mitigation Steps

Consider to replace uint=0 to uint.

C4-005 : > 0 can be replaced with != 0 for gas optimization

Impact - Gas Optimization

!= 0 is a cheaper operation compared to > 0, when dealing with uint.

Proof of Concept

  1. Navigate to the following contract function and lines.
https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityFarming.sol#L318

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityFarming.sol#L132

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityProviders.sol#L182

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityProviders.sol#L239

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityProviders.sol#L283

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityProviders.sol#L410

Tools Used

Code Review

Recommended Mitigation Steps

Use "!=0" instead of ">0" for the gas optimization.

C4-006 : Exponential is more costly

Impact

In the solidity exponential is more costly than 1e18 definition.

Proof of Concept

  1. Navigate to the following contract.

https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/LiquidityProviders.sol#L27

Tools Used

None

Recommended Mitigation Steps

Consider changing 10**18 definition with 1e18.

C4-007 : Unused Function Parameters

Impact

During the code review, It has been observed that the the following contract code is not used. Unused/redundant parameters can be deleted for the gas optimization.

Proof Of Concept

Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
   --> src/hyphen/token/svg-helpers/SvgHelperBase.sol:126:29:
    |
126 |     function getDescription(uint256 _suppliedLiquidity, uint256 _totalSuppliedLiquidity)
    |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^



Warning: Unused function parameter. Remove or comment out the variable name to silence this warning.
   --> /src/hyphen/token/svg-helpers/SvgHelperBase.sol:126:57:
    |
126 |     function getDescription(uint256 _suppliedLiquidity, uint256 _totalSuppliedLiquidity)
    |                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Tools Used

None

Recommended Mitigation Steps

Delete unused function parameters.

C4-008: Redundant External Contract Import

Impact

In the LPToken.sol, The external contract is imported.

https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/token/LPToken.sol#L5

However, the contract is already available in the metatx directory.

Proof of Concept

  1. Navigate to the following contract.
https://github.com/code-423n4/2022-03-biconomy/blob/db8a1fdddd02e8cc209a4c73ffbb3de210e4a81a/contracts/hyphen/token/LPToken.sol#L5
  1. The following import has been used.
import "@openzeppelin/contracts-upgradeable/metatx/ERC2771ContextUpgradeable.sol";
  1. However the contract is available on the following location.
https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/metatx/ERC2771ContextUpgradeable.sol

Tools Used

Code Review

Recommended Mitigation Steps

Consider to use available contracts instead of importing external ones.

C4-009 : Use of _msgSender()

Impact

The use of _msgSender() when there is no implementation of a meta transaction mechanism that uses it, such as EIP-2771, very slightly increases gas consumption.

Proof of Concept

_msgSender() is utilized three times where msg.sender could have been used in the following function.

https://github.com/code-423n4/2022-03-biconomy/blob/main/contracts/hyphen/LiquidityPool.sol#L77

Tools Used

None

Recommended Mitigation Steps

Replace _msgSender() with msg.sender if there is no mechanism to support meta-transactions like EIP-2771 implemented.

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Mar 12, 2022
code423n4 added a commit that referenced this issue Mar 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

1 participant