Skip to content

Instantly share code, notes, and snippets.

@Picodes
Created October 18, 2022 17:30
Show Gist options
  • Select an option

  • Save Picodes/8472670f5660077e64a5331d080ec6c0 to your computer and use it in GitHub Desktop.

Select an option

Save Picodes/8472670f5660077e64a5331d080ec6c0 to your computer and use it in GitHub Desktop.

Report

Gas Optimizations

Issue Instances
[GAS-1] Cache array length outside of loop 14
[GAS-2] Use != 0 instead of > 0 for unsigned integer comparison 13
[GAS-3] Don't initialize variables with default value 1
[GAS-4] Use shift Right/Left instead of division/multiplication if possible 1
[GAS-5] Using bools for storage incurs overhead 2
[GAS-6] Using private rather than public for constants, saves gas 18

[GAS-1] Cache array length outside of loop

Instances (14):

File: LBPair.sol

274:             for (uint256 i; i < _ids.length; ++i) {

496:             for (uint256 i; i < _ids.length; ++i) {

623:             for (uint256 i; i < _ids.length; ++i) {

701:             for (uint256 i; i < _ids.length; ++i) {
File: LBQuoter.sol

100:                 for (uint256 j; j < LBPairsAvailable.length; j++) {

177:                 for (uint256 j; j < LBPairsAvailable.length; j++) {
File: LBRouter.sol

674:             for (uint256 i; i < depositIds.length; ++i) {

711:         for (uint256 i = _pairs.length; i != 0; i--) {

778:             for (uint256 i; i < _pairs.length; ++i) {

831:             for (uint256 i; i < _pairs.length; ++i) {

878:             for (uint256 i; i < _pairs.length; ++i) {

951:             for (uint256 i; i < pairs.length; ++i) {
File: LBToken.sol

90:             for (uint256 i; i < _accounts.length; ++i) {

163:             for (uint256 i; i < _ids.length; ++i) {

[GAS-2] Use != 0 instead of > 0 for unsigned integer comparison

Instances (13):

File: LBFactory.sol

161:             if (_nbPresets > 0) {

191:             if (_nbAvailable > 0) {
File: LBQuoter.sol

79:             if (quote.pairs[i] != address(0) && quote.amounts[i] > 0) {

82:                 if (reserveIn > 0 && reserveOut > 0) {

82:                 if (reserveIn > 0 && reserveOut > 0) {

99:             if (LBPairsAvailable.length > 0 && quote.amounts[i] > 0) {

99:             if (LBPairsAvailable.length > 0 && quote.amounts[i] > 0) {

154:         for (uint256 i = swapLength; i > 0; i--) {

157:             if (quote.pairs[i - 1] != address(0) && quote.amounts[i] > 0) {

160:                 if (reserveIn > 0 && reserveOut > quote.amounts[i]) {

176:             if (LBPairsAvailable.length > 0 && quote.amounts[i] > 0) {

176:             if (LBPairsAvailable.length > 0 && quote.amounts[i] > 0) {
File: libraries/Math128x128.sol

71:                 for (int256 delta = int256(1 << (LOG_SCALE_OFFSET - 1)); delta > 0; delta >>= 1) {

[GAS-3] Don't initialize variables with default value

Instances (1):

File: libraries/Samples.sol

19:     uint256 private constant _OFFSET_INITIALIZED = 0;

[GAS-4] Use shift Right/Left instead of division/multiplication if possible

Instances (1):

File: libraries/Oracle.sol

162:                 _middle = (_low + _high) / 2;

[GAS-5] Using bools for storage incurs overhead

Use uint256(1) and uint256(2) for true/false to avoid a Gwarmaccess (100 gas), and to avoid Gsset (20000 gas) when changing from ‘false’ to ‘true’, after having been ‘true’ in the past. See source.

Instances (2):

File: LBFactory.sol

39:     bool public override creationUnlocked;
File: LBToken.sol

21:     mapping(address => mapping(address => bool)) private _spenderApprovals;

[GAS-6] Using private rather than public for constants, saves gas

If needed, the values can be read from the verified contract source code, or if there are multiple values there can be a single getter function that returns a tuple of the values of all currently-public constants. Saves 3406-3606 gas in deployment gas due to the compiler not having to create non-payable getter functions for deployment calldata, not having to store the bytes of the value outside of where it's used, and not adding another entry to the method ID table

Instances (18):

File: LBFactory.sol

25:     uint256 public constant override MAX_FEE = 0.1e18; // 10%

27:     uint256 public constant override MIN_BIN_STEP = 1; // 0.01%

28:     uint256 public constant override MAX_BIN_STEP = 100; // 1%, can't be greater than 247 for indexing reasons

30:     uint256 public constant override MAX_PROTOCOL_SHARE = 2_500; // 25%

32:     address public override LBPairImplementation;

34:     address public override feeRecipient;

36:     uint256 public override flashLoanFee;

39:     bool public override creationUnlocked;

41:     ILBPair[] public override allLBPairs;
File: LBPair.sol

50:     ILBFactory public immutable override factory;

54:     IERC20 public override tokenX;

55:     IERC20 public override tokenY;
File: LBQuoter.sol

21:     address public immutable routerV2;

23:     address public immutable factoryV1;

25:     address public immutable factoryV2;
File: LBRouter.sol

28:     ILBFactory public immutable override factory;

29:     IJoeFactory public immutable override oldFactory;

30:     IWAVAX public immutable override wavax;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment