code-423n4 / 2021-12-amun-findings

0 stars 0 forks source link

Reuse operation results can save gas #213

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

https://github.com/code-423n4/2021-12-amun/blob/98f6e2ff91f5fcebc0489f5871183566feaec307/contracts/basket/contracts/factories/PieFactoryContract.sol#L91

for (uint256 i = 0; i < _tokens.length; i++) {
    IERC20 token = IERC20(_tokens[i]);
    token.safeTransferFrom(msg.sender, address(pie), _amounts[i]);
    pie.addToken(_tokens[i]);
}

tokens[i] at L91 is already cached in the local variable token at L89, reusing the result instead of doing the subscript operation again can save gas.