You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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).
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.
The text was updated successfully, but these errors were encountered:
There are multiple variables that can be immutable and their use can be "optimized".
First of all,
aToken
,rewardsController
andpoolAddressesProviderRegistry
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 the
poolAddressesProviderRegistry
, you can pass thepoolProviderAddress
directly and store its address as immutable.- You can pass the
poolAddressesProviderRegistry
, but only store thepoolProviderAddress
(making the call at the constructor and not in every call).So, in
supplyTokenTo()
andredeemToken()
there is a call to_pool()
which calls_poolProvider()
andgetPool()
--> 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. InsupplyTokenTo()
andredeemToken()
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; theaToken
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 lessredeemToken
: 40k lessBonus track (not applied in the gist):
balanceOfToken()
can be view.(inhereted ERC20)
, `(inherited ERC20).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.The text was updated successfully, but these errors were encountered: