code-423n4 / 2022-05-rubicon-findings

5 stars 2 forks source link

Gas Optimizations #394

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[G-01] The withdrawal function applies checks after transfer and event emissions

A lot of gas can be saved on failures by applying the following check prior to the token transfer and event emissions.

        require(
            assetsReceived >= assets,
            "You cannot withdraw the amount of assets you expected"
        );

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathToken.sol#L516-L519

[G-02] Use multiple requires instead of multiple &&

Using multiple require statements can be more performant than a string of &&'s.

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathPair.sol#L471-L477

[G-03] For loop optimization

Gas can be saved when performing for loops by following this pattern. The pattern moves the increment of i to an unchecked block removing the overflow checks, while also changing the increment to ++i.

for (uint i = 0; i < length;) {
    doStuff();
    unchecked {
        ++i;
    }
}

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathPair.sol#L480-L490

[G-04] Unnecessary initialization of variable

The initialization of variable newOne is unnecessary. Since the function returns newBathToken, it is not required to temporarily store this in newOne. Simply store it in newBathToken.

https://github.com/code-423n4/2022-05-rubicon/blob/8c312a63a91193c6a192a9aab44ff980fbfd7741/contracts/rubiconPools/BathHouse.sol#L202