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

Open
code423n4 opened this issue Feb 16, 2022 · 2 comments
Open

Gas Optimizations #84

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

Comments

@code423n4
Copy link
Contributor

#AAVEGasFindings
1--
-using storage instead memory to declare snapshot struct
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L135
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L177
instead of caching snapshot per loop just read it directly from storage. Its just called at most three times (or may be once) per loop and read it from storage
cost less gas:

   Snapshot storage snapshot = _snapshots[user][center];

2--
-unnecesary previous var declaration
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L257
previous just used once. just pass _snapshots[to][toSnapshotCount - 1].value; directly below it and remove line 257

// remove this line (L 257)
uint128 newValue = uint128(_snapshots[to][toSnapshotCount - 1].value + amount);

3--
-using require() for gas optimisation
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L261-L264
instead of using else and if to check the condition, using require() to replace if and removing else can save gas

require(from != address(0));

4--
-declare delSupplySnapshotCount & previousDelSupply once instead of twice
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L231-L274
by declaring delSupplySnapshotCount & previousDelSupply once at line 243 and removing all of them inside if & else can save gas(better declare it once instead twice since they have the same value)

	if(to!=address(0)){
 		uint256 delSupplySnapshotCount = _delSupplySnapshotCount;
                uint128 previousDelSupply = _delSupplySnapshots[delSupplySnapshotCount - 1].value;
...

5--
-unused interface
https://github.com/code-423n4/2022-02-aave-lens/blob/main/contracts/core/FollowNFT.sol#L12
IERC721Metadata.sol is never used in FollowNFTcontract

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 16, 2022
code423n4 added a commit that referenced this issue Feb 16, 2022
@Zer0dot
Copy link
Collaborator

Zer0dot commented Mar 18, 2022

Variable declarations like "previous" in this QA report help code readability. 4th point is invalid as well because in cases where neither the from or to addresses are the zero address, then we would be declaring the variables and reading from storage for nothing. 5th point is valid though!

@Zer0dot
Copy link
Collaborator

Zer0dot commented Mar 18, 2022

point 5 addressed here: lens-protocol/core#67

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

2 participants