code-423n4 / 2022-05-velodrome-findings

0 stars 0 forks source link

Gas Optimizations #74

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

G-1 VISIBILITY

Making some constant as non-public to save gas.

If you switch from public to private or internal you can save gas when a constant isn't used outside of its contract. https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=uint%20internal%20constant,1%20days%3B

G-2 ++1 cost less gas compared to i++ or i+= 1 I suggest using ++i instead of i++ to increment the value of an uint variable.

G-3 Comparisons > 0 is less efficient than != 0 for unsigned integers. != 0 cost less gas compared to > 0 with require statements. https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Router.sol#:~:text=require(amountA%20%3E,Router%3A%20INSUFFICIENT_LIQUIDITY%27)%3B

G-4 Storage: Emitting storage values The values emitted shouldn't be read from storage. https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Router.sol#:~:text=amountA%20%3D%20liquidity%20*%20reserveA%20/%20_totalSupply%3B,_totalSupply%3B%20//%20using%20balances%20ensures%20pro%2Drata%20distribution amountA = liquidity * reserveA / _totalSupply; // using balances ensures pro-rata distribution amountB = liquidity * reserveB / _totalSupply;// using balances ensures pro-rata distribution emit Inited(amountA ,amountB);

G-5 For-Loops: Array's Length should be chached to save gas in for-loops One can save 3 gas per each itteration (3 mload and 3 to place memory_offset) By caching the array length in the stack one can save 3 gas. https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=for%20(uint%20i%20%3D%200%3B%20i%20%3C%20_tokens.length%3B%20i%2B%2B)%20%7B

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=)%20external%20%7B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20_gauges.length%3B%20i%2B%2B)%20%7B,-_updateFor(_gauges%5Bi

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=_tokens)%20external%20%7B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20_gauges.length%3B%20i%2B%2B)%20%7B,-IGauge(_gauges%5Bi%5D).

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=_gauges)%20external%20%7B-,for%20(uint%20i%20%3D%200%3B%20i%20%3C%20_gauges.length%3B%20i%2B%2B)%20%7B,-IGauge(_gauges%5Bi%5D).

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#:~:text=for%20(uint%20x%20%3D%200%3B%20x%20%3C%20_gauges.length%3B%20x%2B%2B)%20%7B

G-6 Variables: No need to initialize variables that have default variables. Variables that are not set or initialize variables will have default variables like bool with false and address(0) for address.

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Bribe.sol#:~:text=require(gauge%20%3D%3D%20address(0)%2C%20%22gauge%20already%20set%22)%3B

https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Minter.sol#:~:text=initializer%20%3D%20address(0)%3B

GalloDaSballo commented 2 years ago

Avoiding the extra SLOAD would save 94 gas each.

The report would save between 100 / 500 gas