code-423n4 / 2021-10-tally-findings

0 stars 0 forks source link

Gas: minReceived check can be simplified #41

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

cmichel

Vulnerability details

The minimumAmountReceived check in Swap.swapByQuote is implemented like this:

require(
    (
        !signifiesETHOrZero(zrxBuyTokenAddress) &&
        boughtERC20Amount >= minimumAmountReceived
    ) ||
    (
        signifiesETHOrZero(zrxBuyTokenAddress) &&
        boughtETHAmount >= minimumAmountReceived
    ),
    "Swap::swapByQuote: Minimum swap proceeds requirement not met"
);

It can be simplified to this which performs less calls to signifiesETHOrZero and less logical operators:

require( (signifiesETHOrZero(zrxBuyTokenAddress) ? boughtETHAmount : boughtERC20Amount) >= minimumAmountReceived, "...");
Shadowfiend commented 2 years ago

And has the benefit of being clearer! Great find!