Canto contest - 0xkatana's results

Execution layer for original work.

General Information

Platform: Code4rena

Start Date: 14/06/2022

Pot Size: $100,000 USDC

Total HM: 26

Participants: 59

Period: 7 days

Judge: GalloDaSballo

Total Solo HM: 9

Id: 133

League: ETH

Canto

Findings Distribution

Researcher Performance

Rank: 54/59

Findings: 1

Award: $105.36

🌟 Selected for report: 0

🚀 Solo Findings: 0

Awards

41.2642 USDC - $41.26

396.9199 CANTO - $64.10

Labels

bug
G (Gas Optimization)

External Links

[G-01] 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.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L72 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L84 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L130 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L166 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L211 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L235 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1347 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1353 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1359 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1364 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1413 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/Comp.sol#L207 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L68 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L90 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L181 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L197 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L211 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ERC20.sol#L11 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L116 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L196 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L719 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L942 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L963

Remove the redundant zero initialization uint256 i; instead of uint256 i = 0;

[G-02] Split up require statements instead of &&

Combining require statement conditions with && logic uses unnecessary gas. It is better to split up each part of the logical statement into a separate require statements

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L33 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1003 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1411 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L42 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L115 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L164 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L138 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L228 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L961

Use separate require statements instead of concatenating with &&

[G-03] Cache array length before loop

Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. This saves gas.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L343 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L404 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L476 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L735 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L959 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1106 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1347 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1353 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1359 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1364 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L68 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L90 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L138 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L181 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L197 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L211

Cache the array length before the for loop

[G-04] 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

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L37 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L488 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L489 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L309 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L329 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L380 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1129 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1194 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1197 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L1200

Replace > 0 with != 0 to save gas

[G-05] Short require strings save gas

Strings in solidity are handled in 32 byte chunks. A require string longer than 32 bytes uses more gas. Shortening these strings will save gas.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/DAIInterestRateModelV3.sol#L55 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L16 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L43 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L45 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L54 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L77

Shorten all require strings to less than 32 characters

[G-06] 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.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L166 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L211 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L235 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L268 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L290 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L343 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L404 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L476 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L126 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L206 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L735

Use prefix not postfix to increment in a loop

[G-07] For loop incrementing can be unsafe

For loops that use i++ do not need to use safemath for this operation because the loop would run out of gas long before this point. Making this addition operation unsafe using unchecked saves gas.

Sample code to make the for loop increment unsafe

for (uint i = 0; i < length; i = unchecked_inc(i)) { // do something that doesn't change the value of i } function unchecked_inc(uint i) returns (uint) { unchecked { return i + 1; } }

Idea borrowed from https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc#the-increment-in-for-loop-post-condition-can-be-made-unchecked

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L166 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L211 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L235 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L268 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L290 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L343 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L404 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L476 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L126 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L206

Make the increment in for loops unsafe to save gas

[G-08] Use iszero assembly for zero checks

Comparing a value to zero can be done using the iszero EVM opcode. This can save gas

Source from t11s https://twitter.com/transmissions11/status/1474465495243898885

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L249 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Reservoir.sol#L85 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Accountant/AccountantDelegator.sol#L124 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ExponentialNoError.sol#L75 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L33 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L259 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L295 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L481 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CToken.sol#L745 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/BaseJumpRateModelV2.sol#L82 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L178 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L329 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L363

Use the assembly iszero evm opcode to compare values to zero

[G-09] Use abi.encodePacked() not abi.encode()

Changing abi.encode to abi.encodePacked can save gas. abi.encode pads extra null bytes at the end of the call data which is normally unnecessary. In general, abi.encodePacked is more gas-efficient.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L77 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L89 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L102 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L135 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Timelock.sol#L65 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Timelock.sol#L75 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Timelock.sol#L84 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/Comp.sol#L162 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/Comp.sol#L163 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L78 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L189

Change abi.encode to abi.encodePacked

[G-10] Add payable to functions that won't receive ETH

Identifying a function as payable saves gas. Functions that have a modifier like onlyOwner cannot be called by normal users and will not mistakenly receive ETH. These functions can be payable to save gas.

Locations where this was found include https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/Ownable.sol#L30 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/MiniChefV2.sol#L91 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/MiniChefV2.sol#L109 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/MiniChefV2.sol#L118 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/MiniChefV2.sol#L125 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiToken.sol#L12 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiMakerKashi.sol#L75 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L369 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L398 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L522 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L1029 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L1048 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiMaker.sol#L73

Add payable to these functions for gas savings

[G-11] Add payable to constructors that won't receive ETH

Identifying a constructor as payable saves gas. Constructors should only be called by the admin or deployer and should not mistakenly receive ETH. Constructors can be payable to save gas.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CErc20Delegate.sol#L15 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Reservoir.sol#L33 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Treasury/TreasuryDelegator.sol#L12 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Accountant/AccountantDelegator.sol#L16 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Maximillion.sol#L19 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CEther.sol#L22 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/WETH.sol#L13 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/BaseJumpRateModelV2.sol#L54 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Comptroller.sol#L90 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/JumpRateModel.sol#L47 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ERC20.sol#L29 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/DAIInterestRateModelV3.sol#L40 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L80 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Unitroller.sol#L33 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CErc20Delegator.sol#L25

Add payable to these functions for gas savings

[G-12] Use internal function in place of modifier

An internal function can save gas vs. a modifier. A modifier inlines the code of the original function but an internal function does not.

Source https://blog.polymath.network/solidity-tips-and-tricks-to-save-gas-and-reduce-bytecode-size-c44580b218e6#dde7

Locations where this was found include https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2Router02.sol#L19 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2Pair.sol#L37 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/Ownable.sol#L60 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiMakerKashi.sol#L86 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/KashiPairMediumRiskV1.sol#L129 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/KashiPairMediumRiskV1.sol#L969 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L398 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/BentoBoxV1.sol#L745 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiMaker.sol#L88

Use internal functions in place of modifiers to save gas.

[G-13] Use type(uint256).max

Using type(uint256).max uses less gas than other options for this constant value. Source https://forum.openzeppelin.com/t/using-the-maximum-integer-in-solidity/3000/13

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ERC20.sol#L18

Use type(uint256).max

[G-14] Bitshift for divide by 2

When multiply or dividing by a power of two, it is cheaper to bitshift than to use standard math operations.

There is a divide by 2 operation on these lines https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/libraries/Math.sol#L16 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/libraries/Math.sol#L19 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/SushiToken.sol#L171

Bitshift right by one bit instead of dividing by 2 to save gas

[G-15] Use uint not bool

Booleans are more expensive than uint256 or any type that takes up a full word because each write operation emits an extra SLOAD to first read the slot's contents, replace the bits taken up by the boolean, and then write back. This is the compiler's defense against contract upgrades and pointer aliasing, and it cannot be disabled.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L26 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L39 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L40 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L58 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L73 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CErc20Delegator.sol#L466 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerInterface.sol#L6 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/PriceOracle.sol#L8

Replace bool variables with uints

[G-16] Use uint256 not smaller ints

From the solidity docs

When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size.

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L27 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L28 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L262 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L283 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L284 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Lens/CompoundLens.sol#L474 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ExponentialNoError.sol#L83 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ExponentialNoError.sol#L85 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorBravoDelegate.sol#L18 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L88 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L253 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L267 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/Governance/GovernorAlpha.sol#L331 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ERC20.sol#L64 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CErc20Delegator.sol#L31 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CErc20Delegator.sol#L39

Replace smaller int or uint variables with uint256 variables

[G-17] Use newer solidity version

A solidity version before 0.8.X is used. The latest release of solidity includes changes that can provide gas savings. The improvements include:

  • Safemath by default from 0.8.0 (can be more gas efficient than some 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.
  • Solidity version 0.8.13 can save more gas with Yul IR pipeline

Source https://gist.github.com/hrkrshnn/ee8fabd532058307229d65dcd5836ddc#upgrade-to-at-least-084

Use solidity release 0.8.13 with Yul IR pipeline and other improvements for gas savings

[G-18] Use Solidity errors instead of require

Solidity errors introduced in version 0.8.4 can save gas on revert conditions https://blog.soliditylang.org/2021/04/21/custom-errors/ https://twitter.com/PatrickAlphaC/status/1505197417884528640

Locations where this was found include https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/DAIInterestRateModelV3.sol#L55 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L16 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L43 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/CNote.sol#L45 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L168 https://github.com/Plex-Engineer/lending-market/tree/ab31a612be354e252d72faead63d86b844172761/contracts/ComptrollerG7.sol#L227

Replace require blocks with new solidity errors described in https://blog.soliditylang.org/2021/04/21/custom-errors/

[G-19] Use simple comparison in trinary logic

The comparison operators >= and <= use more gas than >, <, or ==. Replacing the >= and ≤ operators with a comparison operator that has an opcode in the EVM saves gas

The existing code is https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/bentobox/KashiPairMediumRiskV1.sol#L1189

outNum = inNum >= 0 ? uint256(inNum) : (inNum == USE_VALUE1 ? value1 : value2);

A simple comparison can be used for gas savings by reversing the logic

outNum = inNum < 0 ? (inNum == USE_VALUE1 ? value1 : value2) : uint256(inNum);

Replace the comparison operator and reverse the logic to save gas using the suggestions above

[G-20] Use simple comparison in if statement

The comparison operators >= and <= use more gas than >, <, or ==. Replacing the >= and ≤ operators with a comparison operator that has an opcode in the EVM saves gas

The existing code is https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2Router02.sol#L51-L59

if (amountBOptimal <= amountBDesired) { require(amountBOptimal >= amountBMin, 'UniswapV2Router: INSUFFICIENT_B_AMOUNT'); (amountA, amountB) = (amountADesired, amountBOptimal); } else { uint amountAOptimal = UniswapV2Library.quote(amountBDesired, reserveB, reserveA); assert(amountAOptimal <= amountADesired); require(amountAOptimal >= amountAMin, 'UniswapV2Router: INSUFFICIENT_A_AMOUNT'); (amountA, amountB) = (amountAOptimal, amountBDesired); }

A simple comparison can be used for gas savings by reversing the logic

if (amountBOptimal > amountBDesired) { uint amountAOptimal = UniswapV2Library.quote(amountBDesired, reserveB, reserveA); assert(amountAOptimal <= amountADesired); require(amountAOptimal >= amountAMin, 'UniswapV2Router: INSUFFICIENT_A_AMOUNT'); (amountA, amountB) = (amountAOptimal, amountBDesired); } else { require(amountBOptimal >= amountBMin, 'UniswapV2Router: INSUFFICIENT_B_AMOUNT'); (amountA, amountB) = (amountADesired, amountBOptimal); }

Replace the comparison operator and reverse the logic to save gas using the suggestions above

[G-21] Non-public variables save gas

Many constant variables are public, but changing the visibility of these variables to private or internal can save gas.

Locations where this was found include https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/governance/Timelock.sol#L29 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/governance/Timelock.sol#L30 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/governance/Timelock.sol#L31 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2ERC20.sol#L10 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2ERC20.sol#L11 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2ERC20.sol#L12 https://github.com/Plex-Engineer/zeroswap/tree/03507a80322112f4f3c723fc68bed0f138702836/contracts/uniswapv2/UniswapV2ERC20.sol#L19

Declare some public variables as private or internal to save gas

[G-22] Write contracts in vyper

The contracts are all written entirely in solidity. Writing contracts with vyper instead of solidity can save gas.

Source https://twitter.com/eiber_david/status/1515737811881807876 doggo demonstrates https://twitter.com/fubuloubu/status/1528179581974417414?t=-hcq_26JFDaHdAQZ-wYxCA&s=19

Write some or all of the contracts in vyper to save gas

#0 - GalloDaSballo

2022-08-04T00:10:20Z

Will save less than 500 gas

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter