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

contracts/base and libraries: replace "if (A && B)" to save gas fees #351

Open
BurtonQin opened this issue Jun 29, 2023 · 1 comment
Open

Comments

@BurtonQin
Copy link

The solidity compiler generates more code for “if (A && B) {}” than for “if (A) {if (B) {} }”.
Our experiment shows that switching from “if (A && B) {}” to “if (A) {if (B) {} }” can save gas fee.

There are five such cases:

if (token == WETH9 && address(this).balance >= value) {

Replacing

if (token == WETH9 && address(this).balance >= value) {

with

if (token == WETH9) {
    if (address(this).balance >= value) {

can save gas fees and does not hurt code readability.

Similarly, the following code snippets can be improved:

if (params.decimalIndex > 0 && params.sigfigIndex == params.decimalIndex) {

if (difference > 0 && difference <= 18) {

if (params.refundAsETH && params.token0 == WETH9) {

if (params.refundAsETH && params.token1 == WETH9) {

@AbulKhayer2244
Copy link

#351 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants