code-423n4 / 2021-09-yaxis-findings

0 stars 0 forks source link

Gas: Loop in `StablesConverter.convert` can be avoided #123

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

cmichel

Vulnerability details

The two loops in StablesConverter.convert can be avoided by using the indices mapping.

Recommended Mitigation Steps

Instead of the for loop, simply get the index i by doing:

if (_output == address(token3CRV)) {
  uint256 i = indices[_input];
  amounts[i] = _inputAmount;
} else if (_input == address(token3CRV)) {
  uint256 i = indices[_output];
  remove_liquidity_one_coin(_, i);
}

This replaces all the tokens[i] sloads with a single one.

GalloDaSballo commented 2 years ago

Using indices instead of looping will save gas, Sponsor mitigated, LG