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

Open
code423n4 opened this issue May 1, 2022 · 1 comment
Open

Gas Optimizations #91

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

Comments

@code423n4
Copy link
Contributor

There are multiple variables that can be immutable and their use can be "optimized".

First of all, aToken, rewardsController and poolAddressesProviderRegistry could be immutable: there are no setters for them and all of them are proxies: in case of a change, the implementation address will change, not the proxy address.

In the case of the poolAddressesProviderRegistry:

poolAddressesProviderRegistry is only needed to fetch the pool address, but there is no need to store this address, only the address of the pool (and again, is a proxy). So, there are 2 options:
- Instead of passing thepoolAddressesProviderRegistry, you can pass the poolProviderAddress directly and store its address as immutable.
- You can pass the poolAddressesProviderRegistry, but only store the poolProviderAddress (making the call at the constructor and not in every call).

So, in supplyTokenTo() and redeemToken() there is a call to _pool() which calls _poolProvider() and getPool() --> 4 calls + 1 SLOAD in total. This can be done with 0 calls and 0 SLOAD just storing the pool address at the constructor.

In the case of aToken:

The aToken address does not change (proxy) and its underlying wont never change. In supplyTokenTo() and redeemToken() there is a call to _tokenAddress() that can be skipped (1 SLOAD + 1 CALL) just storing the underlying address as immutable (doing it at the constructor; the aToken address can also be stored as immutable, just for transparency).

Gas comparison reducing the cost as much as possible: https://gist.github.com/miguelmtzinf/fb6d5266fb2fd3d057028b84fc0f66e1
(also provides files with changes)

These changes reduces the gas cost across the board, and makes the most important functions way cheaper:

  • supplyTokenTo: 40k less
  • redeemToken: 40k less

Bonus track (not applied in the gist):

  • The function balanceOfToken() can be view.
  • Typo in L154. Instead of (inhereted ERC20), `(inherited ERC20).
  • In transferERC20() at L225 you are not using the already built _requireNotAToken() internal function. This function could have a more generic revert message and be used also here.
@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 4, 2022
@PierrickGT
Copy link
Member

First of all, aToken, rewardsController and poolAddressesProviderRegistry could be immutable

Duplicate of #1

poolAddressesProviderRegistry is only needed to fetch the pool address

Duplicate of #17

The aToken address does not change (proxy) and its underlying wont never change.

Duplicate of #19

The function balanceOfToken() can be view.

Can't because we inherit from the yield source interface.

Typo in L154. Instead of (inhereted ERC20), `(inherited ERC20).

Duplicate of #22

In transferERC20() at L225 you are not using the already built _requireNotAToken() internal function.

Duplicate of #4

@PierrickGT PierrickGT added the duplicate This issue or pull request already exists label May 4, 2022
@JeeberC4 JeeberC4 removed the duplicate This issue or pull request already exists label May 6, 2022
@JeeberC4 JeeberC4 reopened this May 6, 2022
@JeeberC4 JeeberC4 added duplicate This issue or pull request already exists and removed duplicate This issue or pull request already exists labels May 20, 2022
@JeeberC4 JeeberC4 reopened this May 20, 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

4 participants