Blockchain
AMM: Uniswap and Constant Product
Imagine you want to swap ETH for USDC at 3 AM on a Sunday. On a regular exchange you need a counterparty who posted an order at a matching price. What if there is nobody? In traditional finance, you wait until Monday. In DeFi, you trade with a mathematical formula that runs 24/7 without intermediaries. In 2024 alone, more than $700 billion in trading volume passed through this one formula. How did a single equation from school algebra replace an entire industry of market makers?
- **Uniswap** processes $1–3 billion in volume daily - more than Coinbase. The protocol is governed by ~50 lines of core code, not thousands of employees
- **Impermanent Loss** cost LPs more than $100M in 2023 alone. Understanding IL is the difference between profitable farming and losing money
- **Concentrated Liquidity** (Uniswap V3) increased capital efficiency up to 4000x but turned passive LP into active position management - a new profession in DeFi
Предварительные знания
Constant Product: the x * y = k formula
On centralized exchanges (Binance, NYSE), trading works through an **order book**. Buyers place bids (buy prices), sellers place asks (sell prices). A trade occurs when a bid and ask match. This model requires **market makers** - professional participants with large capital who continuously post orders on both sides.
On-chain order books work poorly. Every order placement and cancellation is a transaction that costs gas. At a base fee of 30 gwei, one Uniswap operation costs roughly $14 in gas. A market maker updating orders 100 times per second would pay tens of thousands of dollars per minute. In 2018, Hayden Adams proposed a radically different approach: **replace the order book with a mathematical formula**.
An **Automated Market Maker (AMM)** is a smart contract that holds reserves of two tokens and determines the price using a formula. The Uniswap V1/V2 formula is called **constant product**: **x * y = k** where `x` is the reserve of token X in the pool, `y` is the reserve of token Y, and `k` is a constant that must not decrease during a swap.
**Arbitrage keeps the price in check.** If ETH is $3,000 on Binance but $3,060 in a Uniswap pool after a large trade, an arbitrageur will buy cheap ETH on Binance and sell the expensive ETH on Uniswap, equalizing prices. This happens every block, often through MEV bots. The AMM does not "know" the market price - it relies on arbitrageurs.
The **0.3% fee** (Uniswap V2) is taken from every swap and stays in the pool, increasing `k`. In 2024, Uniswap V2/V3 generated over $600M in fees for liquidity providers. This makes AMM a self-sustaining protocol: the more trading volume, the more profitable it is to provide liquidity.
In an ETH/USDC pool (100 ETH, 300,000 USDC), a trader buys ETH for 30,000 USDC. What happens to the price of ETH in this pool?
Liquidity Pools: becoming a market maker
Where do the token reserves in an AMM come from? They are provided by ordinary users - **liquidity providers (LPs)**. Anyone can deposit a pair of tokens into a pool and earn a share of fees from every swap. In traditional finance, market making is accessible only to institutional players with licenses and millions of dollars. In DeFi - it is a single transaction.
In Uniswap V2, liquidity is deposited **in the ratio of the current price**. If the ETH/USDC pool trades at $3,000, you need to deposit tokens of equal value: for example, 1 ETH + 3,000 USDC. In return, the LP receives **LP tokens** - ERC-20 tokens representing their share in the pool.
The **Factory pattern** is a key architectural choice in Uniswap. The `UniswapV2Factory` contract creates pools from a template using the `CREATE2` opcode. Each token pair has exactly one pool, whose address can be computed deterministically without querying the blockchain. By 2025, more than 300,000 pools had been created through Uniswap Factory.
**WETH - why wrap ETH?** Native ETH is not an ERC-20 token and does not have a `transferFrom()` method needed for the approve-and-transfer pattern. WETH (Wrapped ETH) is an ERC-20 wrapper where 1 WETH = 1 ETH. All ETH pools actually use a WETH/TOKEN pair.
**Total Value Locked (TVL)** is the primary metric for DeFi protocols. It is the sum of all assets locked in smart contracts. At its peak in November 2021, Uniswap TVL reached $10 billion. By 2025, Uniswap consistently holds $4–6 billion TVL, processing $1–3 billion in daily trading volume - more than many centralized exchanges.
A pool contains 50 ETH and 150,000 USDC. Total LP tokens = 2,738. Bob wants to add $6,000 in liquidity. Which token pair should he deposit?
Impermanent Loss: the hidden cost of liquidity
Providing liquidity has a hidden problem that is not immediately visible: **impermanent loss (IL)** - the difference between the value of assets in the pool and the value of those same assets had the LP simply held them in their wallet (HODL).
Why does this happen? When the price of one token rises, arbitrageurs take the appreciated token out of the pool, replacing it with the cheaper one. In the end, the LP has more of the cheap token and less of the expensive one - exactly the opposite of what an investor would want.
**IL becomes "permanent" when the LP withdraws liquidity.** While the position is open, the loss is "impermanent" - the price may return and the IL will disappear. But if the LP withdraws tokens at a changed price, the loss is locked in forever. The strategy of "wait until price comes back" does not work in a trending market.
**Fees vs IL: the main LP tradeoff.** A pool with high trading volume generates more fees, which can offset IL. For stablecoin pairs (USDC/USDT, DAI/USDC), IL is minimal because prices barely diverge. For volatile pairs (ETH/SHIB), IL can be enormous, and only very high trading volume would justify the risk.
| Pool type | Volatility | IL risk | Typical APR from fees |
|---|---|---|---|
| USDC/USDT | Minimal | ~0% | 2-5% |
| ETH/USDC | Medium | 5-15% | 10-30% |
| ETH/BTC | Medium (correlated) | 3-8% | 5-15% |
| ETH/MEME | Extreme | 20-90% | 100-1000%+ |
**IL minimization strategy:** pairs of correlated assets (ETH/stETH, USDC/DAI) have virtually no IL while generating steady fee income. This is "conservative" DeFi farming.
An LP deposited 1 ETH + 3,000 USDC at a price of $3,000/ETH. The price of ETH rose to $12,000 (4x). What impermanent loss will the LP incur?
Concentrated Liquidity: the Uniswap V3 breakthrough
In Uniswap V2, liquidity is spread evenly across the **entire** price range - from 0 to infinity. This is catastrophically inefficient. If ETH trades at $3,000, liquidity in the $0.01–$100 and $100,000–$∞ ranges is never used. Estimates suggest that in V2 only **0.5% of liquidity** actively participates in trading.
**Uniswap V3** (May 2021) solved this problem through **concentrated liquidity** - the LP chooses a price range [pa, pb] in which their liquidity will be active. If the price moves outside this range, the position becomes inactive (earns no fees). But within the range, capital efficiency soars.
A revolutionary consequence: LP positions in V3 are **NFTs**, not interchangeable ERC-20 tokens. Each position is unique (its own range, its own liquidity), so Uniswap V3 issues ERC-721 (NFT) instead of LP tokens. This changed the entire ecosystem: LP positions can be sold, used as collateral in lending protocols, or wrapped into structured products.
**JIT (Just-In-Time) Liquidity** is an MEV strategy that exploits V3. A bot sees a large swap in the mempool, adds liquidity in a narrow range right before the swap, earns a large share of the fee, and instantly removes liquidity after. Regular LPs lose income because the bot dilutes their share at the moment of fee collection.
**Curve V2** proposed an alternative approach: an AMM with an **internal price oracle** and automatic rebalancing. Instead of the LP manually choosing a range, the protocol itself concentrates liquidity around the current market price and smoothly shifts it as the price changes. This reduces the need for active management.
**Uniswap V4** (2024) added a system of **hooks** - arbitrary code that executes before and after swaps, liquidity additions, and other operations. This transforms Uniswap from a fixed AMM into a **platform for building AMMs**: dynamic fees, on-chain limit orders, automatic rebalancing - all implemented through hooks without changing the core protocol.
Concentrated liquidity is always better than a V2 full-range position - just pick a narrow range for maximum efficiency
Concentrated liquidity **amplifies** both fee income and impermanent loss. A narrow range = high fees when price is inside, but also rapid full conversion to one token when price exits. If the price moves out of range - income drops to zero and IL is locked in. For most non-professional LPs, a full-range position is safer.
DeFi protocol marketing highlights "4000x capital efficiency" but omits the risks. Real research shows that more than 50% of LPs in Uniswap V3 earn less than if they had simply held their assets. Concentrated liquidity is a tool for professionals with active management, not a passive "set-and-forget" strategy.
An LP deployed $10,000 in Uniswap V3 in the range $2,900–$3,100 when the current ETH price was $3,000. The price of ETH rose to $3,500. What happened to the LP's position?
Key takeaways
- **Constant product (x * y = k)** replaces order books with a mathematical formula: price is determined by the ratio of reserves in the pool, every swap moves the price along the hyperbola, arbitrageurs align pool price with the market
- **Liquidity pools** democratize market making: anyone deposits a token pair in proportion, receives LP tokens, earns 0.3% from every swap proportional to their share
- **Impermanent Loss = 2√r / (1+r) - 1** - the hidden cost of liquidity. At 2x price change IL = 5.7%, at 5x = 25%. Fees can offset IL but are not guaranteed to
- **Concentrated Liquidity** (V3) lets you choose a price range, increasing efficiency up to 4000x, but requires active management and amplifies IL
- The school-algebra formula mentioned at the start has indeed replaced market makers: x * y = k processes billions of dollars daily, but understanding slippage, IL, and concentrated liquidity is a prerequisite for not losing money
Related topics
AMM connects token standards with advanced DeFi mechanisms and MEV:
- ERC standards — LP tokens are ERC-20, V3 positions are ERC-721 (NFT). WETH (ERC-20 wrapper for ETH) is required for all pools
- Lending protocols — Aave and Compound use AMM pools for liquidations. LP tokens can be used as collateral for loans - composability in action
- MEV: extractable value — Arbitrage between AMMs and CEX, JIT liquidity, sandwich attacks - AMMs are the primary source of MEV on Ethereum
- Gas: the computation model — Every swap costs ~150,000 gas. Rebalancing V3 positions requires additional gas, limiting the frequency of active management
Вопросы для размышления
- If an AMM works without an order book and determines price by a formula - who exactly "brings" the market price into the pool? What would happen if arbitrageurs disappeared?
- Concentrated liquidity in V3 requires active management. Does this bring us back to the same problem that AMMs were solving - the need for professional market makers?
- If more than 50% of LPs in Uniswap V3 earn less than with HODL - why do people continue providing liquidity? What motivations might they have beyond fees?