# Users are incorrectly refunded when liquidity is insufficient

swap_2_internal takes amount_in from swapper then also refunds original_amount-amount_in, letting users drain AMM pair funds via excess refunds.

- Payout: Audit contest
- Severity: high
- Program: Superposition
- Found by: ZanyBonzy
- Disclosed: 2025-01-07
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-10-superposition-findings/issues/5
- Library page: https://bugbountylibrary.com/r/c4%3A2024-10-superposition%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:rust technique:incomplete-fix technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Seawater AMM swap_2_internal_erc20 function
**Root cause:** The remediated code both pulls amount_in from the user and separately refunds original_amount - amount_in, so two conflicting fixes coexist and the refund is extraneous.
**Steps:**
1. Call swap_2_internal with original_amount > amount_in (first pool lacks liquidity so amount_in < original_amount).
2. Observe `erc20::take(from, amount_in, permit2)` pulls only amount_in from swapper.
3. Observe `if original_amount > amount_in` branch transfers `original_amount - amount_in` back to sender.
4. Net result: user receives refund they never paid for, draining pair funds.
**How found:** Code reading of swap_2_internal: noticed amount_in is taken from swapper yet a refund of original_amount - amount_in is still issued, indicating two overlapping fixes for the same problem.
**Exploitation:** Attacker repeatedly swaps with original_amount exceeding available liquidity so amount_in is reduced, collecting the difference as a refund each time, extracting funds from the AMM pair.
**Insight:** When auditing remediated code, check whether a fix was applied twice in conflicting ways: here pulling amount_in and refunding the difference both exist, so the refund becomes free money.

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

**Refund code in swap_2_internal**

```
erc20::take(from, amount_in, permit2)?;
        erc20::transfer_to_sender(to, amount_out)?;

>>      if original_amount > amount_in {
            erc20::transfer_to_sender(
                to,
                original_amount
>>                  .checked_sub(amount_in)
                    .ok_or(Error::TransferToSenderSub)?,
            )?;
        }
```

