← All lessons

Learn · decentralised finance

Transaction ordering: the same swaps, different outcomes

A pool changes after each trade. A swap arriving next meets the reserves left by the trade before it.

Start with Uniswap V2: pools, swaps and LP returns if liquidity pools are new to you. This lesson uses fictional tokens and moves no funds.

A quote is a snapshot

An automated market maker (AMM) holds two token reserves. Buying TOK adds USDx to the pool and removes TOK. Another TOK buyer then gets fewer tokens for the same input. A TOK seller moves the reserves the other way.

Price impact is the change caused by your own trade. Slippage compares the execution with the quote you saw earlier. Other trades can move that execution in your favour or against you. Your quote already includes your trade’s price impact.

For an exact-input swap, a minimum output sets a floor on how much you receive. If the available output is smaller, the transaction reverts: its pool changes are undone. This protects the output floor; it does not guarantee execution or remove gas costs.

Source: Uniswap V2 pricing and slippage bounds.

Change the queue, keep the starting pool

First execute both default queues. Ada receives TOK in run A, but her swap reverts in run B after Ben’s larger purchase. Then move Cy’s sale to the front of run B. Predict whether Ada’s available output rises or falls before you execute it.

Same swaps. Different sequence.

Illustrative · computed in your browser

Both runs start at 100 TOK and 100,000 USDx. These are fictional tokens. Each swap pays 0.30% of its input into the pool. No gas, external markets, token rounding or real assets are modelled; values below are rounded to six decimals.

Move a trader earlier or later, then execute one transaction at a time. Changing a queue resets that run. Quotes and minimums were fixed before either run.

Run A · 0/3 processed

Current pool reserves

100 TOK
100,000 USDx

  1. 1. Ada

    Input: 1,000 USDx
    Minimum received: 0.97 TOK

    Original quote: 0.987158 TOK

    Waiting to execute

  2. 2. Ben

    Input: 5,000 USDx
    Minimum received: 4.4 TOK

    Original quote: 4.748297 TOK

    Waiting to execute

  3. 3. Cy

    Input: 3 TOK
    Minimum received: 2,800 USDx

    Original quote: 2,904.137255 USDx

    Waiting to execute

Run B · 0/3 processed

Current pool reserves

100 TOK
100,000 USDx

  1. 1. Ben

    Input: 5,000 USDx
    Minimum received: 4.4 TOK

    Original quote: 4.748297 TOK

    Waiting to execute

  2. 2. Ada

    Input: 1,000 USDx
    Minimum received: 0.97 TOK

    Original quote: 0.987158 TOK

    Waiting to execute

  3. 3. Cy

    Input: 3 TOK
    Minimum received: 2,800 USDx

    Original quote: 2,904.137255 USDx

    Waiting to execute

Compare what each trader receives

Compare the same trader across runs, not different output tokens. More tokens for the same input is a better execution, not proof of investment profit.

Ada · TOK

Run A: waiting

Run B: waiting

Ben · TOK

Run A: waiting

Run B: waiting

Cy · USDx

Run A: waiting

Run B: waiting

Follow the calculation

For each transaction, use the current input reserve x and output reserve y. Multiply the input by 0.997 to account for the 0.30% swap fee, then calculate the available output:

output = y × (input × 0.997) / (x + input × 0.997)

Compare that output with the trader’s fixed minimum. If it passes, add the full input to one reserve and subtract the output from the other. The fee remains in the pool, so the reserve product grows slightly. If it fails, leave both reserves unchanged and process the next transaction.

Sources: Uniswap V2 formula and swap fees. This model omits protocol-fee ownership, integer token units and routing across pools.

Sequencing has economic consequences

The lab gives you direct control of a queue. Real transaction ordering depends on the chain and its block-building process; sending first does not universally guarantee executing first. Changes can come from ordinary demand or deliberate ordering strategies.

A tighter minimum limits adverse execution but can cause more reverts. A looser minimum permits a worse output. Neither setting guarantees a profitable trade. The comparison above measures token execution only; gas, external prices and later sale proceeds would be needed to calculate profit.

Ada’s minimum is 0.97 TOK, but the available output is 0.90 TOK. What happens?