# When performing swap and the swap position does not cover swap amount, the base price of sqrt_price is set incorrectly

Superposition seawater swap sets pool sqrt_price to the user's price_limit instead of the last swapped tick price when positions don't cover the swap amount, corrupting the pool's base price for subsequent swaps.

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

**Target:** seawater AMM pool swap function (Rust, pool.rs)
**Root cause:** The swap loop advances state.price to price_limit when liquidity is exhausted, and after the loop self.sqrt_price.set(state.price) writes that limit price rather than the price of the last position actually swapped.
**Steps:**
1. Create pool with sqrt_price encode_sqrt_price(100,1) and enable it.
2. Mint a position with limited liquidity (lower_tick 39122, upper_tick 50108, liquidity 20000).
3. Call `swap(token, true, I256::try_from(100000_i32).unwrap(), U256::MAX)` so amount_remaining is not covered; loop runs until state.price == price_limit and sqrt_price is set to tick_math::MIN_SQRT_RATIO (4295128740).
4. Mint a second position with ample liquidity and call `swap(token, true, I256::try_from(100_i32).unwrap(), U256::MAX)`; it reverts with 'Price limit too low' because price_limit >= self.sqrt_price check fails against the corrupted base price.
**How found:** Code reading of the swap while-loop and the post-loop `self.sqrt_price.set(state.price)`; noticed state.price is updated each iteration and reaches price_limit when positions are insufficient, so the stored base price diverges from the last swapped price.
**Insight:** In AMM swap loops that mutate a running price toward a user-supplied limit, check what value is persisted after the loop: if the limit is written as the new spot price, the next swap's price-limit validation can revert or misprice. Track the last valid traded price separately.

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

**Mitigation patch tracking last_valid_price**

```
let mut last_valid_price = state.price;
             while !state.amount_remaining.is_zero() && state.price != price_limit {
                 iters += 1;
    -            debug_assert!(iters < 500);
    +            // debug_assert!(iters < 500);

                 let step_initial_price = state.price;

    @@ -479,6 +480,7 @@ impl StoragePool {
                         };

                         state.liquidity = liquidity_math::add_delta(state.liquidity, liquidity_net)?;
    +                    last_valid_price = state.price;
                     }

                     state.tick = match zero_for_one {
    @@ -493,7 +495,8 @@ impl StoragePool {

             // write state
             // update price and tick
    -        self.sqrt_price.set(state.price);
    +        self.sqrt_price.set(if state.price == price_limit { last_valid_price } else { state.price });
```

