Velodrome Finance contest - DavidGialdi's results

A base layer AMM on Optimism, inspired by Solidly.

General Information

Platform: Code4rena

Start Date: 23/05/2022

Pot Size: $75,000 USDC

Total HM: 23

Participants: 75

Period: 7 days

Judge: GalloDaSballo

Total Solo HM: 13

Id: 130

League: ETH

Velodrome Finance

Findings Distribution

Researcher Performance

Rank: 68/75

Findings: 1

Award: $50.15

🌟 Selected for report: 0

🚀 Solo Findings: 0

++i is cheaper than i++

Use prefix increment instead of postfix increment.

Unnecessary checked arithmetic in for loops

The i++ part of for loops can be surrounded by the unchecked keyword to save gas. For example:

for (uint256 i = 0; i < 10; i++) { ... }

can be optimized to:

for (uint256 i = 0; i < 10;) { ... unchecked { i++; } }

!= 0 is cheaper than > 0 (unsigned integers)

When dealing with unsigned integers the != 0 comparison is equivalent to > 0 comparison. It is also cheaper, therefore you should use it instead.

Cache Array Length Outside of Loop

You can optimize for loops that iterate over array elements by caching the array length in a local variable. This will reduce the number of MLOADs and will save gas. For example:

for (uint256 i = 0; i < array.length; i++) { ... }

can be optimized to:

uint256 length = array.length; for (uint256 i = 0; i < length; i++) { ... }

Change those for: Gauge:353: for (uint i = 0; i < tokens.length; i++) Minter:57: for (uint i = 0; i < claimants.length; i++) Pair:257: for (uint i = 0; i < _prices.length; i++) etc...

Repeated access to the current array element in for loops

Many for loops that iterate over array elements access the current element (array[i]) more than once in each iteration. This element can be accessed only once (and be cached in a local variable) in order to avoid the additional array-boundaries check that Solidity performs with each array access, and save gas. For example in Gauge.getReward function

for (uint i = 0; i < tokens.length; i++) { (rewardPerTokenStored[tokens[i]], lastUpdateTime[tokens[i]]) = _updateRewardPerToken(tokens[i]); uint _reward = earned(tokens[i], account); lastEarn[tokens[i]][account] = block.timestamp; userRewardPerTokenStored[tokens[i]][account] = rewardPerTokenStored[tokens[i]]; if (_reward > 0) _safeTransfer(tokens[i], account, _reward); emit ClaimRewards(msg.sender, tokens[i], _reward); }

Instead you can cached out the tokens[i] in a local variable.

#0 - GalloDaSballo

2022-06-30T00:00:45Z

Would save Hundreds of gas, and seems to be from c4udit

#1 - GalloDaSballo

2022-06-30T02:09:38Z

100 / 500

AuditHub

A portfolio for auditors, a security profile for protocols, a hub for web3 security.

Built bymalatrax © 2024

Auditors

Browse

Contests

Browse

Get in touch

ContactTwitter