How Uniswap actually works — AMM system design
Deep dive into the Automated Market Maker (AMM) system architecture, the constant product formula (x * y = k), liquidity pool states, and arbitrage boundaries.

Traditional financial systems rely on an Order Book model. Buyers submit bid orders ("I want to buy 1 ETH for $3,000") and sellers submit ask orders. A centralized matching engine matches these bids and asks in real-time.
On a public blockchain, running a matching engine is impossible. The computational complexity of maintaining an order book — with constant updates, cancellations, and matching runs — would consume gigabytes of gas, freezing the network.
Uniswap bypassed this entire order book model by inventing the Automated Market Maker (AMM) system design.
Instead of matching individual buyers and sellers, Uniswap pools tokens together in smart contracts (Liquidity Pools) and allows traders to swap directly against the contract using a simple mathematical invariant: Constant Product Formula.
1. The Math: Constant Product (x * y = k)
A Uniswap pool contains balances of two tokens, which we will call x and y (for example, ETH and USDC).
The fundamental invariant governing the pool is:
Where:
- x is the reserve balance of Token A (ETH).
- y is the reserve balance of Token B (USDC).
- k is a constant invariant. This value must remain unchanged during swaps (ignoring fees).
When a trader swaps Token A for Token B:
-
They deposit a quantity delta_x into the contract.
-
The reserve of Token A becomes x + delta_x.
-
To keep k constant, the contract computes the new reserve of Token B (y - delta_y) such that:
(x + delta_x) * (y - delta_y) = k -
Rearranging the formula, the trader receives delta_y tokens:
delta_y = y - (k / (x + delta_x)) = (y * delta_x) / (x + delta_x)
This ensures that as a token becomes scarcer in the pool, its relative price automatically increases, creating a natural supply-and-demand curve inside the code.
2. The Architecture: Factories and Pairs
Uniswap V2 is designed around two primary contracts: the Factory and the Pair (Pool).
- The Factory Contract: Serves as the registry. It keeps a mapping of token pairs to their deployed pool contract addresses. If you want to swap a new ERC-20 token, you call
createPair()on the factory, which deploys a new pool contract using deterministicCREATE2opcodes. - The Pair Contract: The actual custody engine. It holds the physical reserves of the two tokens and exposes functions for swapping, depositing liquidity (
mint), and withdrawing liquidity (burn).
3. Price Slippage and Arbitrage Boundaries
Because Uniswap prices assets based purely on internal ratios (x / y), it does not use a pricing oracle. The pool has no idea what the "real" price of ETH is in the outside world (e.g. on Binance).
If a trader makes a massive purchase of ETH from the pool, they push the reserve ratio out of balance, resulting in Price Slippage. The price of ETH inside the pool rises far above the external market rate.
This creates an Arbitrage opportunity:
- Arbitrage bots spot the price discrepancy.
- They buy ETH cheaper on Binance.
- They swap it for USDC in the Uniswap pool, pocketing the difference.
- This action pushes the reserves back into balance, aligning the pool price with global market value.
Arbitrage is not an exploit; it is the design boundary that forces decentralized AMMs to track real-world asset prices.
The Low Liquidity Sandbox Trap: During testing, I set up a mock swap pool for a custom token and deposited only $50 worth of tokens. I then attempted to execute a swap for $30. Because the pool liquidity was extremely low, my swap represented a percentage of the pool reserves that was too large. The transaction succeeded, but the constant product math caused a 90% price slippage. I received only $3 worth of output tokens. Always verify pool depth and implement slippage tolerances in your client-side routers!
Suppose an ETH/USDC pool has 10 ETH (x) and 20,000 USDC (y).
- What is the value of the invariant k?
- If a trader swaps 2 ETH into the pool, how much USDC will they receive? (Assume zero swap fees).
- Calculate the new reserves and verify that the product remains k.
Was this lesson helpful?
Let us know what you think of this specification. (submitting anonymously)
