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 #40

Open
code423n4 opened this issue May 1, 2022 · 2 comments
Open

Gas Optimizations #40

code423n4 opened this issue May 1, 2022 · 2 comments
Assignees
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

  1. Removing SafeMath usage minorly improves contract gas consumption (as shown for redeemToken and supplyTokenTo)

Removing using SafeMath for uint256; and making adjustments accordingly to the contract's arithmetic as:
L262: uint256 _balanceDiff = _afterBalance - _beforeBalance;
L361: return _supply == 0 ? _tokens : _tokens * _supply / aToken.balanceOf(address(this));
L373: return _supply == 0 ? _shares : _shares * aToken.balanceOf(address(this)) / _supply;

Results in lesser gas usage ! (shown below)

Reason: starting Solidity >= 0.8.0, arithmetic operations revert on underflow and overflow.
OZ's SafeMath v4.4.1 for Solidity >= 0.8.0 relies on compiler's checks for sub, mul and div functions anyway, making the use of the library completely redundant.

with SafeMath:

·-------------------------------------------------------|----------------------------|-------------|-----------------------------·
|                 Solc version: 0.8.10                  ·  Optimizer enabled: false  ·  Runs: 200  ·  Block limit: 30000000 gas  │
························································|····························|·············|······························
|  Methods                                              ·               100 gwei/gas               ·       2772.21 usd/eth       │
·····························|··························|··············|·············|·············|···············|··············
|  Contract                  ·  Method                  ·  Min         ·  Max        ·  Avg        ·  # calls      ·  usd (avg)  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  claimRewards            ·       57098  ·      59291  ·      58195  ·            4  ·      16.13  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  decreaseERC20Allowance  ·       40154  ·      42359  ·      41620  ·            3  ·      11.54  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  increaseERC20Allowance  ·       61935  ·      64452  ·      64041  ·            7  ·      17.75  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  mint                    ·       52265  ·      69377  ·      62788  ·           13  ·      17.41  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  redeemToken             ·           -  ·          -  ·     116178  ·            1  ·      32.21  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  setManager              ·           -  ·          -  ·      48335  ·            4  ·      13.40  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  supplyTokenTo           ·      115041  ·     157449  ·     146161  ·            6  ·      40.52  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  transferERC20           ·       60271  ·      62452  ·      61362  ·            2  ·      17.01  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  approve                 ·       27329  ·      47229  ·      43912  ·            6  ·      12.17  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  mint                    ·       52286  ·      69698  ·      66593  ·           12  ·      18.46  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  transferFrom            ·           -  ·          -  ·      50967  ·            1  ·      14.13  │
·····························|··························|··············|·············|·············|···············|··············
|  Deployments                                          ·                                          ·  % of limit   ·             │
························································|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness                             ·     4670575  ·    4670587  ·    4670585  ·       15.6 %  ·    1294.78  │
························································|··············|·············|·············|···············|··············
|  ERC20Mintable                                        ·           -  ·          -  ·    2256052  ·        7.5 %  ·     625.42  │
·-------------------------------------------------------|--------------|-------------|-------------|---------------|-------------·

after SafeMath removed and adjusted:

·-------------------------------------------------------|----------------------------|-------------|-----------------------------·
|                 Solc version: 0.8.10                  ·  Optimizer enabled: false  ·  Runs: 200  ·  Block limit: 30000000 gas  │
························································|····························|·············|······························
|  Methods                                              ·               100 gwei/gas               ·       2787.95 usd/eth       │
·····························|··························|··············|·············|·············|···············|··············
|  Contract                  ·  Method                  ·  Min         ·  Max        ·  Avg        ·  # calls      ·  usd (avg)  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  claimRewards            ·       57098  ·      59291  ·      58195  ·            4  ·      16.22  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  decreaseERC20Allowance  ·       40154  ·      42359  ·      41620  ·            3  ·      11.60  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  increaseERC20Allowance  ·       61935  ·      64452  ·      64041  ·            7  ·      17.85  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  mint                    ·       52265  ·      69377  ·      62788  ·           13  ·      17.50  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  redeemToken             ·           -  ·          -  ·     115989  ·            1  ·      32.34  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  setManager              ·           -  ·          -  ·      48335  ·            4  ·      13.48  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  supplyTokenTo           ·      114915  ·     157449  ·     146119  ·            6  ·      40.74  │
·····························|··························|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness  ·  transferERC20           ·       60271  ·      62452  ·      61362  ·            2  ·      17.11  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  approve                 ·       27329  ·      47229  ·      43912  ·            6  ·      12.24  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  mint                    ·       52286  ·      69698  ·      66593  ·           12  ·      18.57  │
·····························|··························|··············|·············|·············|···············|··············
|  ERC20Mintable             ·  transferFrom            ·           -  ·          -  ·      50967  ·            1  ·      14.21  │
·····························|··························|··············|·············|·············|···············|··············
|  Deployments                                          ·                                          ·  % of limit   ·             │
························································|··············|·············|·············|···············|··············
|  AaveV3YieldSourceHarness                             ·     4648841  ·    4648853  ·    4648851  ·       15.5 %  ·    1296.08  │
························································|··············|·············|·············|···············|··············
|  ERC20Mintable                                        ·           -  ·          -  ·    2256052  ·        7.5 %  ·     628.98  │
·-------------------------------------------------------|--------------|-------------|-------------|---------------|-------------·
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 1, 2022
code423n4 added a commit that referenced this issue May 1, 2022
@PierrickGT PierrickGT self-assigned this May 3, 2022
@PierrickGT
Copy link
Member

Great report by this warden, he should get extra points.

@PierrickGT
Copy link
Member

Duplicate of #11

@PierrickGT PierrickGT marked this as a duplicate of #11 May 3, 2022
@PierrickGT PierrickGT added the duplicate This issue or pull request already exists label May 3, 2022
@JeeberC4 JeeberC4 removed the duplicate This issue or pull request already exists label May 6, 2022
@JeeberC4 JeeberC4 reopened this May 6, 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

3 participants