Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loops can be implemented more efficiently #249

Open
code423n4 opened this issue Dec 19, 2021 · 0 comments
Open

Loops can be implemented more efficiently #249

code423n4 opened this issue Dec 19, 2021 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

0x0x0x

Vulnerability details

Proof of Concept

Example:


for (uint i = 0; i < arr.length; i++) {

//Operations not effecting the length of the array.

}

Loading length of array costs gas. Therefore, the length should be cached, if the length of the array doesn't change inside the loop. Furthermore, there is no need to assign the initial value 0. This csts extra gas.

Recommended implementation:


uint length = arr.length;

for (uint i; i < length; ++i) {

//Operations not effecting the length of the array.

}

By doing so the length is only loaded once rather than loading it as many times as iterations (Therefore, less gas is spent).

Occurences


./basket/contracts/callManagers/RebalanceManager.sol:218:        for (uint256 i; i < _swapsV2.length; i++) {
./basket/contracts/callManagers/RebalanceManager.sol:234:        for (uint256 i; i < _swapsV3.length; i++) {
./basket/contracts/callManagers/RebalanceManagerV2.sol:155:        for (uint256 i; i < _swapsV2.length; i++) {
./basket/contracts/callManagers/RebalanceManagerV3.sol:166:        for (uint256 i; i < _swapsV2.length; i++) {
./basket/contracts/callManagers/RebalanceManagerV3.sol:171:            for (uint256 j; j < trade.swaps.length; j++) {
./basket/contracts/facets/Basket/BasketFacet.sol:50:        for (uint256 i; i < bs.tokens.length; i++) {
./basket/contracts/facets/Basket/BasketFacet.sol:160:        for (uint256 i; i < bs.tokens.length; i++) {
./basket/contracts/facets/Basket/BasketFacet.sol:202:        for (uint256 i; i < bs.tokens.length; i++) {
./basket/contracts/facets/Basket/BasketFacet.sol:321:        for (uint256 i = 0; i < tokens.length; i++) {
./basket/contracts/facets/Basket/BasketFacet.sol:348:        for (uint256 i; i < bs.tokens.length; i++) {
./basket/contracts/facets/Basket/BasketFacet.sol:381:        for (uint256 i; i < bs.tokens.length; i++) {
./basket/contracts/facets/Call/CallFacet.sol:55:        for (uint256 i = 0; i < callStorage.callers.length; i++) {
./basket/contracts/facets/Call/CallFacet.sol:82:        for (uint256 i = 0; i < _targets.length; i++) {
./basket/contracts/facets/Call/CallFacet.sol:95:        for (uint256 i = 0; i < _targets.length; i++) {
./basket/contracts/factories/PieFactoryContract.sol:88:        for (uint256 i = 0; i < _tokens.length; i++) {
./basket/contracts/singleJoinExit/SingleNativeTokenExit.sol:69:        for (uint256 i; i < tokens.length; i++) {
./basket/contracts/singleJoinExit/SingleNativeTokenExitV2.sol:74:        for (uint256 i; i < _exitTokenStruct.trades.length; i++) {
./basket/contracts/singleJoinExit/SingleNativeTokenExitV2.sol:76:            for (uint256 j; j < trade.swaps.length; j++) {
./basket/contracts/singleJoinExit/SingleTokenJoin.sol:108:        for (uint256 i; i < tokens.length; i++) {
./basket/contracts/singleJoinExit/SingleTokenJoinV2.sol:86:        for (uint256 i; i < _joinTokenStruct.trades.length; i++) {
./basket/contracts/singleJoinExit/SingleTokenJoinV2.sol:91:            for (uint256 j; j < trade.swaps.length; j++) {
./basket/contracts/singleJoinExit/SingleTokenJoinV2.sol:100:            for (uint256 j; j < trade.swaps.length; j++) {
./basket/contracts/singleJoinExit/SingleTokenJoinV2.sol:117:        for (uint256 i; i < tokens.length; i++) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants