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

Gas Optimizations #49

Open
code423n4 opened this issue May 25, 2022 · 0 comments
Open

Gas Optimizations #49

code423n4 opened this issue May 25, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

BathHouse.sol

  • L87/104/110/111/143/147/156/171/237/241/280/281/395/397/403/409 - require can be replaced with an if and a custom Error.

  • L105 - The name could be defined directly in the storage or create an immutable variable, since it is not possible to modify it.

  • L110/111 - All validations should be performed first, since if you revert it is better not to execute any line of bonus code.

  • L111 - It is less expensive that the validation is _reserveRatio != 0.

  • L116 - If the storage variable bpsToStrategists is always equal to 20 and cannot be modified, it would be better if it were a constant.

  • L242 - It is less expensive to simply deny !IBathPair(_bathPairAddress).initialized().

  • L281 - It is less expensive for the validation to be rr != 0.

  • L383 - It is less expensive if instead of validating "outcome = true : outcome = false;" "outcome: !outcome;" is used.

  • L397 - It is not necessary to validate that it is != address(0) since underlyingERC20 complies with the ERC20 interface, therefore it cannot be zero.

BathPair.sol

  • L118/120/143/148/174/182/332/346/419/471/506/570 - require can be replaced with an if and a custom Error.

  • L119/121/126 - It is not necessary to create a variable for the msg.sender, it is more expensive to use a variable than the msg. sender

  • L149 - no need to check if IBathHouse(bathHouse).isApprovedStrategist(targetStrategist) is == true, without
    matching would already return a result and spending less gas.

  • L206 -It is less expensive ++last_stratTrade_id; inside _next_id();

  • L277 - Less expensive current[--current.length]; than current[current.length - 1];

  • L299/300 - It is not necessary to create a variable, the order() function could be returned directly.

  • L310 - It is not necessary to set the default value, it generates an extra gas cost.

  • L311/427/480/582 - It is not necessary to set the index to zero, since it is its default value, also in the if a
    index++ which can become less expensive if the last line of the for is set to unchecked{++index;}

  • L232/252/333/334/515/523/597 - It is less expensive to validate that: "variable != 0" than: "variable > 0".

BathToken.sol

  • L66 - The variable in storage DOMAIN_SEPARATOR is only set in the constructor and is never modified so it could be immutable.

  • L226/234/470/510/516/547/722/740 - require can be replaced with an if and a custom Error.

  • L228 - It is not necessary to set true, you can simply return IBathHouse(bathHouse).isApprovedPair(msg.sender);

  • L634 - Gas could be saved if instead of bonusTokens.length > 0 bonusTokens.length != 0 is used;

  • L635 - It is not necessary to set the index to zero, since it is its default value, also in the if a
    index++ which can become less expensive if the last line of the for is set to unchecked{++index;}

RubiconMarket.sol

  • L22/27/32 - It is not necessary to create a modifier to execute an internal view method, it would be better just in the first line
    from setOWner the function auth(msg.sender) and modify it so that it only reverts when it is not address(this) or owner.

  • L210/216/217/226/282/283/297/304/308/361/398-404/418 - require can be replaced with an if and a custom Error.

  • L233 - It is less expensive to validate offers[id].timestamp != 0; that >;

  • L400/918/942 - It is less expensive to validate pay_amt != 0; that >;

  • L402 - It is less expensive to validate buy_amt != 0; that >;

  • L837/876 - It is less expensive to validate pay_amt != 0; that >;

  • L985/1002 - It is less expensive to validate pid != 0; that >;

  • L1063 - It is less expensive to validate _best[address(t_buy_gem)][address(t_pay_gem)] != 0; that >;

  • L306/310/571/574 - Reduce the size of error messages (Long revert Strings)
    Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met.
    Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.

  • L436 - When executing _next_id() it is less expensive to execute ++last_offer_id;

  • L1197 - Gas is saved if instead of _span[pay_gem][buy_gem]--; use --_span[pay_gem][buy_gem];

  • L467 - isClosed() always returns false, so there is no point in validating it in require((msg.sender == getOwner(id)) || isClosed()).

  • L573 - isClosed() always returns false, so there is no point in validating it in require(isClosed() || msg.sender == getOwner(id) || id == dustId).

  • L526/566 - The default value of buyEnabled is true, it doesn't make sense to reset it in the initialize().

  • L527/565 - The default value of matchingEnabled is true, it doesn't make sense to reset it in initialize().

RubicoinRouter.sol

  • L82/83/226 - It is not necessary to set the default value of a variable, that generates an extra gas expense.

  • L85/169/227 - It is not necessary to set the index to zero, since it is its default value, also in the if a
    index++ which can become less expensive if the last line of the for is set to unchecked{++index;}

  • L227/251/258/259/526 - instead of variable - 1, it is less expensive --variable.

  • L338/392/506 - Reduce the size of error messages (Long revert Strings)
    Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met.
    Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.

  • L354 - It is less expensive to validate delta != 0; that >;

BathBuddy.sol

  • L102 - It is less expensive to validate releasable != 0; that >;
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

1 participant