# If liquidity is insufficient, users may need to pay more tokens in swap2

Superposition swap2 charges users the full original_amount even when the first pool only consumes part of it, causing overpayment.

- Payout: Audit contest
- Severity: medium
- Program: Superposition
- Found by: shaflow2
- Disclosed: 2024-10-18
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-08-superposition-findings/issues/12
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-superposition%3AM-10
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:rust vuln:accounting-error vuln:business-logic

**Target:** Seawater AMM swap2 / swap_2_internal_erc20 (Rust)
**Root cause:** swap_2_internal returns amount_in actually consumed, but swap_2_internal_erc20 takes original_amount from the user without refunding the unconsumed difference.
**Steps:**
1. Call swap2 converting token1->token2 via fusdc with amount = original_amount.
2. First pool (token1-fusdc) has insufficient liquidity so swap returns amount_in < original_amount.
3. swap_2_internal_erc20 executes `erc20::take(from, original_amount, permit2)` charging full amount.
4. User receives amount_out based on amount_in, losing original_amount - amount_in tokens.
**How found:** Code reading of swap_2_internal_erc20 noticing original_amount is used for take() while swap_2_internal returns the actual consumed amount_in.
**Exploitation:** Any user swapping through a depleted first pool overpays; no attacker action needed, loss accrues to the swapper.
**Insight:** In multi-hop swap wrappers, verify the charged amount equals the amount actually consumed by the first hop, not the user-supplied input; partial fills from thin liquidity silently overcharge.

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

**swap_2_internal_erc20 takes original_amount instead of amount_in**

```
pub fn swap_2_internal_erc20(
        pools: &mut Pools,
        from: Address,
        to: Address,
        amount: U256,
        min_out: U256,
        permit2: Option<Permit2Args>,
    ) -> Result<(U256, U256), Revert> {

        let (
            original_amount,
            amount_in,
            amount_out,
            _interim_usdc_out,
            _final_tick_in,
            _final_tick_out,
        ) = Self::swap_2_internal(pools, from, to, amount, min_out)?;

        // transfer tokens
        erc20::take(from, original_amount, permit2)?;
        erc20::transfer_to_sender(to, amount_out)?;

        Ok((amount_in, amount_out))
    }
}
```

**swap_2_internal returns amount_in less than original_amount**

```
fn swap_2_internal(
        pools: &mut Pools,
        from: Address,
        to: Address,
        amount: U256,
        min_out: U256,
    ) -> Result<(U256, U256, U256, I256, i32, i32), Revert> {
        let original_amount = amount;

        let amount = I256::try_from(amount).map_err(|_| Error::SwapResultTooHigh)?;

        // 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(),
        )?;
```

