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

Potential issue with testPathIndependenceForSwaps test from part 3/4 #15

Open
aviggiano opened this issue Jun 29, 2023 · 3 comments · May be fixed by #16
Open

Potential issue with testPathIndependenceForSwaps test from part 3/4 #15

aviggiano opened this issue Jun 29, 2023 · 3 comments · May be fixed by #16

Comments

@aviggiano
Copy link

aviggiano commented Jun 29, 2023

Hello,

I am fuzzing Uniswap v2 and I believe I have found a potential issue with the testPathIndependenceForSwaps test from part 3/4.

When running echidna with testLimit equals to 100k and 4 workers, the test breaks:

swapExactTokensForTokensPathIndependence(uint256): failed!💥
  Call sequence:
    testProvideLiquidityInvariants(7222,148)
    testRemoveLiquidityInvariants(1412263)
    testSwapTokens(878955802143800039497958245)
    testPathIndependenceForSwaps(0)

I believe this is caused by a wrong assumption that swapping back and forth only costs 3% in fees.

    /*
    Swapping x of testToken1 for y token of testToken2 and back should (roughly) give user x of testToken1.
    The following function checks this condition by assessing that the resulting x is no more than 3% from the original x.
    ...
    */
    ...

        assert((x - xOut) * 100 <= 3 * x); // (x - xOut) / x <= 0.03; no more than 3% loss of funds

Instead, each swap takes 3%, so the back-and-forth maximum loss should be 100% - (100%-3%)*(100%-3%) = 5.91%

aviggiano added a commit to aviggiano/echidna-streaming-series that referenced this issue Jun 29, 2023
@aviggiano aviggiano linked a pull request Jun 29, 2023 that will close this issue
aviggiano added a commit to aviggiano/fuzzer-evaluation that referenced this issue Jun 29, 2023
@technovision99
Copy link
Contributor

The test doesn't concern the fees taken by uniswap, which is 0.30%, not 3%. The test attempts to test the invariant "swapping X for Y and back should leave the user with X tokens" (bar some precision loss, currently set to 3%). So I believe the test is correct, although it's interesting you found an edge case where this doesn't hold. Could you zip and send your corpus? This might have to do with the pool conditions being strange (it seems there's low and imbalanced liquditity judging from the call sequence, but I'm not 100% sure)

@aviggiano
Copy link
Author

Thank you for your response, I stand corrected.

About 3%, was this precision loss defined? Just empirically?

About the corpus, I actually changed a bunch of stuff on the tests, so maybe this won't work out of the box. I'll try to repro on this repo and will send it here if I'm able to do it.

@technovision99
Copy link
Contributor

Yeah the precision loss is defined empirically. I'm not sure if there's any specification that could detail the potential precision loss, but I think 3% is a generous bound given that only a 0.3% fee is taken from each swap. I also think this is a good number to use because it's generally the recommended slippage tolerance

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

Successfully merging a pull request may close this issue.

2 participants