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

0 stars 0 forks source link

Gas Optimizations #212

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Velodrome Gas Optimization

Use ++ i instead of i++ to Save Gas

When using i++ this uses more gas since Solidity 0.8.0 due to the way the compiler handles incrementing in safemath. This is less efficient than using ++i which uses less gas because of how Solidity implements Safemath, even they though both accomplish the same thing within a for loop. The following lines' use of i++ can be replaced with ++i.

For example, take line 57 (Minter.sol) which utilizes i++ which can be made more efficient with the use of ++i instead

[line57] for (uint i = 0; i < claimants.length; i++)

alter this to

[line 57] for (uint i = 0; i < claimants.length; ++i)

There are many for loops through the code, so using ++i can add up substantially in gas. Replace i++ with ++i in the following lines.

Gauge.sol

Changes are reccomended on lines 179, 353, 405, 426, 448, 484

Line 179 - change the i++ in for (uint i = 0; i < numRewards; i++) to ++i

Line 353 - change the i++ in for (uint i = 0; i < tokens.length; i++) to ++i

Line 405- change the i++ in for (uint i = _startIndex; i < _endIndex; i++) to ++i

Line 426 - change the i++ in for (uint i; i < [ength; i++) to i++

Line 448 - change the i++ in for (uint i = _startIndex; i < _endIndex; i++) to ++i

Line 484 - change the i++ in for (uint i = _startIndex; i < _endIndex; i++) to ++i

Pair.sol

Changes are recommended on lines 257, 389

Line 257 - change the i++ in for (uint i = 0; i < _prices.length; i++) to ++i

Line 389 - change the i++ in [line389] for (uint i = 0; i < 255; i++) to ++i

Rewards Distributor.sol

Changes are recommended on lines 75, 105, 121, 148, 195, 252, 301

Line 75 - change the i++ in for (uint i = 0; i < 20; i++) to ++i

Line 105 - change the i++ in for (uint i = 0; i < 128; i++)

Line 121 - change the i++ in for (uint i = 0; i < 128; i++) to ++i

Line 148 - change the i++ infor (uint i = 0; i < 20; i++) to ++i

Line 195 - change the i++ in for (uint i = 0; i < 50; i++ to ++i

Line 252 - change the i++ in for (uint i = 0; i < 50; i++ to ++i

Line 301 - change the i++ in for (uint i = 0; i < _tokenIds.length; i++) to ++i

Router.sol

Changes are recommended on lines 90, 316

Line 90 - change the i++ in for (uint i = 0; i < routes.length; i++) to ++i

Line 316 - change the i++ in for (uint i = 0; i < routes.length; i++) to ++i

VelodromeLibrary.sol

Changes are recommmended on line 24

Line 24 - change the i++ in for (uint i = 0; i < 255; i++) to ++i

Voter.sol

Changes are recommended on lines 76, 103, 128, 143, 147, 266, 272, 304, 310, 340, 346

Line 76 - change the i++ in for (uint i = 0; i < _tokens.length; i++) to ++i

Line 103 - change the i++ in for (uint i = 0; i < _poolVoteCnt; i++) to ++i

Line 128 - change the i++ in for (uint i = 0; i < _poolCnt; i++) to ++i

Line 143 - change the i++ in for (uint i = 0; i < _poolCnt; i++) to ++i

Line 147 - change the i++ in for (uint i = 0; i < _poolCnt; i++) to ++i

[Line 266]((https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Voter.sol#L266) - change the i++ in for (uint i = 0; i < _gauges.length; i++) to ++i

Line 272 - change the i++ in for(uint i = start; i < end; i++) to ++i

Line 304 - change the i++ in for (uint i = 0; i < _gauges.length; i++) to ++i

Line 310 - change the i++ in for (uint i = 0; i < _gauges.length; i++) to ++i Line 340 - change the i++ in for (uint x = start; x < finish; x++) to ++i

Line 346 - change the i++ in for (uint x = 0; x < _gauges.length; x++) to ++i

Voting Escrow.sol

Changes are recommended on lines 1146, 1193, 1125, 1249, 1295, 1320, 1325

Line 1146 - change the i++ in for (uint i = 0; i < _tokenIds.length; i++) to ++i

Line 1193 - change the i++ in for (uint i = 0; i < _tokenIds.length; i++) to ++i

Line 1225- change the i++ in for (uint i = 0; i < srcRepOld.length; i++) to ++i

Line 1249 - change the i++ in for (uint i = 0; i < dstRepOld.length; i++) to ++i

Line 1295- change the i++ in for (uint i = 0; i < srcRepOld.length; i++) to ++i

Line 1320- change the i++ in for (uint i = 0; i < dstRepOld.length; i++) to ++i

Line 1325 - change the i++ in for (uint i = 0; i < ownerTokenCount; i++) to ++i

footnote - consider declaring all public functions as payable as it shouldn't interfere with any functionality, but will remove a 5 gas runtime check for each call.

GalloDaSballo commented 2 years ago

Would save hundreds of gas

GalloDaSballo commented 2 years ago

100