# swap_2 implementation will randomly revert due to improper check, root cause for failed test ethers_suite_uniswap_orchestrated_uniswap_two

swap_2_internal asserts interim USDC out equals USDC in, but UniswapV3 partial fills leave amount_remaining, so valid swaps revert.

- Payout: Audit contest
- Severity: medium
- Program: Superposition
- Found by: oakcobalt
- Disclosed: 2024-10-18
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-08-superposition-findings/issues/30
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-superposition%3AM-09
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** Seawater AMM pool swap_2_internal (Rust)
**Root cause:** swap_2_internal asserts interim_usdc_out == interim_usdc_in, but the second swap may consume only part of interim_usdc_out when the pool runs out of liquidity, leaving amount_remaining > 0.
**Steps:**
1. Run the failing test ethers_suite_uniswap_orchestrated_uniswap_two; error decodes to 'Internal swap amounts not matched' (Error::InterimSwapNotEq).
2. Create token0/token1 pools, enable, mint positions at ticks 39120-50100, update positions.
3. swap_904369_B_E(token0, true, 1000, U256::MAX) -> fusdc_out_0 = -58592.
4. swap_904369_B_E(token1, false, -fusdc_out_0, U256::MAX) -> fusdc_out_1 = 44866 (partial fill, liquidity exhausted).
5. assert_ne!(-fusdc_out_0, fusdc_out_1) confirms mismatch triggering the revert in swap_2_internal.
**How found:** Investigated the readme-listed failing test ethers_suite_uniswap_orchestrated_uniswap_two, decoded the revert byte array to Error::InterimSwapNotEq, then wrote a breakdown unit test splitting the two swaps to observe partial fill on the second swap.
**Insight:** When a swap wrapper asserts that intermediate amounts match across two hops, check UniswapV3 amount_remaining semantics: partial fills on the second hop break equality even when min_out is satisfied.

## Proof of concept (copied exactly from the original report)

**swap_2_internal strict equality check**

```
// swap in -> usdc
        let (amount_in, interim_usdc_out, final_tick_in) = pools.pools.setter(from).swap(
            true,
            amount,
            // swap with no price limit, since we use min_out instead
            tick_math::MIN_SQRT_RATIO + U256::one(),
        )?;
...
        // swap usdc -> out
        let (amount_out, interim_usdc_in, final_tick_out) = pools.pools.setter(to).swap(
            false,
            interim_usdc_out,
            tick_math::MAX_SQRT_RATIO - U256::one(),
        )?;
...
|>      assert_eq_or!(interim_usdc_out, interim_usdc_in, Error::InterimSwapNotEq);
```

