Platform: Code4rena
Start Date: 03/07/2023
Pot Size: $40,000 USDC
Total HM: 14
Participants: 74
Period: 7 days
Judge: alcueca
Total Solo HM: 9
Id: 259
League: ETH
Rank: 32/74
Findings: 1
Award: $58.47
🌟 Selected for report: 0
🚀 Solo Findings: 0
🌟 Selected for report: SM3_SS
Also found by: 0x11singh99, 0xAnah, 0xSmartContract, 0xn006e7, 0xprinc, DavidGiladi, ElCid, JCN, K42, MIQUINHO, Raihan, Rolezn, SAAJ, SY_S, Strausses, TheSavageTeddy, bigtone, erebus, hunter_w3b, josephdara, lsaudit, mahdirostami, oakcobalt, peanuts, pfapostol, seth_lawson
58.4732 USDC - $58.47
_getIJ
Current implementation of looking for I
and J
tokens is very ineffective. It always loops through the whole array, even when I
and J
are at the beginning of the array.
File: /src/Well.sol function _getIJ( IERC20[] memory _tokens, IERC20 iToken, IERC20 jToken ) internal pure returns (uint256 i, uint256 j) { bool foundI = false; bool foundJ = false; for (uint256 k; k < _tokens.length; ++k) { if (iToken == _tokens[k]) { i = k; foundI = true; } else if (jToken == _tokens[k]) { j = k; foundJ = true; } } if (!foundI) revert InvalidTokens(); if (!foundJ) revert InvalidTokens(); }
_getIJ
should be reimplemented to save gas (loop's iterations) for scenarios where I
and J
are at the beginning of the array. Firstly, loop once, until you find either I
or J
token (assume one of them has been found at index X
of the array). Break
from the loop and enter another loop from X + 1
to the end of array. If you find either I
or J
again, break
once more (as this means that both I
and J
are found).
This approach will save a lot of gas when I and J are at the beginning of the array. Otherwise, (in the worst case scenario: either I or J is the last element) - you will still loop over the whole array - as in the current implementation.
#0 - c4-pre-sort
2023-07-13T12:49:48Z
141345 marked the issue as high quality report
#1 - c4-sponsor
2023-07-24T14:49:52Z
publiuss marked the issue as sponsor confirmed
#2 - c4-judge
2023-08-05T11:18:18Z
alcueca marked the issue as grade-b
#3 - c4-judge
2023-08-05T11:18:23Z
alcueca marked the issue as grade-a