code-423n4 / 2022-02-aave-lens-findings

0 stars 0 forks source link

Gas Optimizations #84

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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

Zer0dot commented 2 years ago

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 commented 2 years ago

point 5 addressed here: https://github.com/aave/lens-protocol/pull/67