Submitted by oakcobalt, also found by pipidu83, prapandey031, and Silvermist
swap_2 implementation will randomly revert due to improper check, root cause for failed test ethers_suite_uniswap_orchestrated_uniswap_two.
The audit readme listed an unsolved failed test ethers_suite_uniswap_orchestrated_uniswap_two(). The failed test is due to an improper check in swap_2_internal(), which sometimes causes a valid swap_2 to revert.
We see that in swap_2_internal, interim_usdc_out is required to equal interim_usdc_in. interim_usdc_out is the fusdc swapped out in 1st swap. interim_usdc_in is the actual fusdc swapped in 2nd swap. In any swap, not all user input amount has to be used to achieve a desirable output. Especially in uniswapV3 swap logic, amount_remaining can be greater than 0, which means not all input tokens are used.
https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/lib.rs#L241
The equality check will cause revert when 2nd swap runs out of liquidity but still satisfies the users min_out. A valid swap can be reverted.
Test ethers_suite_uniswap_orchestrated_uniswap_two() analysis:
When running the test, the output error byte array [73, 110, 116, 101, 114, 110, 97, 108, 32, 115, 119, 97, 112, 32, 97, 109, 111, 117, 110, 116, 115, 32, 110, 111, 116, 32, 109, 97, 116, 99, 104, 101, 100] decodes(ASII) into Internal swap amounts not matched, corresponding to Error::InterimSwapNotEq. This custom error is only used in swap_2_internal.
I added a unit test to break down the failed reason ethers_suite_orchestrated_uniswap_two_breakdown:
Test result:
As seen above, due to in the 2nd swap (fusdc  token1), the token1 pool ran out of liquidity, only a portion of fusdc_out_0 is used to perform the swap. fusdc_out_0  fusdc_out_1 reverted the original ethers_suite_uniswap_orchestrated_uniswap_two test.
VScode
Consider removing/relaxing the strict equality check.
af-afk (Superposition) confirmed
0xsomeone (judge) commented:
