# swapOut functions have invalid slippage check, causing user loss of funds

SeawaterAMM swapOut functions compare minOut against the input token amount instead of output, so slippage protection is ineffective and users can lose funds.

- Payout: Audit contest
- Severity: high
- 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/35
- Library page: https://bugbountylibrary.com/r/c4%3A2024-08-superposition%3AH-07
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:rust tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** SeawaterAMM.sol swapOut5E08A399 / swapOutPermit23273373B AMM swap functions
**Root cause:** swap_internal returns (amount_0, amount_1); for a token1->token0 swap the decoded swapAmountOut is actually the input token1 amount, but the require compares it to minOut instead of the token0 output.
**Steps:**
1. Call `swapOut5E08A399(token, amountIn, minOut)` where token is USDC (token1) and pool token0 is the output asset
2. Function delegatecalls `swap904369BE(token, false, int256(amountIn), type(uint256).max)`
3. Decode returned `(swapAmountIn, swapAmountOut)` = (amount_0, amount_1)
4. Observe `require(swapAmountOut >= int256(minOut))` checks token1 input amount, not token0 output
5. Swap executes with no effective slippage bound on the actual output
**How found:** Read the ISeawaterAMM docs stating swapOut should enforce slippage on the token0 output, then traced the delegatecall into the Rust swap_internal returning (amount_0, amount_1) and noticed the Solidity require used the wrong tuple element.
**Exploitation:** A user submits a swap expecting minOut protection; because the check validates the input-side amount, the swap can settle at an arbitrarily bad price (or revert incorrectly), and the user absorbs the loss. No attacker capital or MEV sequence is described.
**Insight:** When a Solidity wrapper decodes a tuple returned by a Rust/other-language core, verify the semantic meaning of each tuple position against the core's return convention rather than trusting variable names like swapAmountOut.

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

**swapOut5E08A399 incorrect slippage check**

```
function swapOut5E08A399(
        address token,
        uint256 amountIn, //@audit-info note: this is usdc(token1)
        uint256 minOut
    ) external returns (int256, int256) {
            (bool success, bytes memory data) = _getExecutorSwap().delegatecall(
            abi.encodeCall(
                ISeawaterExecutorSwap.swap904369BE,
                (token, false, int256(amountIn), type(uint256).max)
            )
        );
        require(success, string(data));
        //@audit-info note: swapAmountIn <=> token0 , swapAmountOut <=> token1
|>      (int256 swapAmountIn, int256 swapAmountOut) = abi.decode(
            data,
            (int256, int256)
        );
       //@audit This should use token0 value, not token1
|>     require(swapAmountOut >= int256(minOut), "min out not reached!");
       return (swapAmountIn, swapAmountOut);
    }
```

**swapOutPermit23273373B incorrect slippage check**

```
function swapOutPermit23273373B(address token, uint256 amountIn, uint256 minOut, uint256 nonce, uint256 deadline, uint256 maxAmount, bytes memory sig) external returns (int256, int256) {
...
|>     (int256 swapAmountIn, int256 swapAmountOut) = abi.decode(data, (int256, int256));
|>      require(swapAmountOut >= int256(minOut), "min out not reached!");
        return (swapAmountIn, swapAmountOut);
    }
```

