Automated Market Makers Explained
Before diving into Uniswapās specific implementation, letās understand what an Automated Market Maker (AMM) is and how it works.
What is an AMM?
Traditional exchanges use an order book where buyers and sellers place orders at specific prices. AMMs take a completely different approach:
- No order books or matching engines
- Liquidity is provided to a pool by users (Liquidity Providers or LPs)
- Prices are determined by a mathematical formula
- Trades happen directly against the pool, not with other users
The Constant Product Formula
The simplest and most famous AMM model is the constant product formula, which Uniswap v2 uses:
x * y = k
Where:
x
is the amount of token X in the pooly
is the amount of token Y in the poolk
is a constant that should remain the same after every trade
Letās see how this works with a simple example:
Example: ETH/USDC Pool
Imagine a pool with:
- 10 ETH (x)
- 20,000 USDC (y)
The constant product is: 10 * 20,000 = 200,000
The current price of ETH is: 20,000 / 10 = 2,000 USDC
Now, if someone wants to buy 1 ETH from this pool:
- We need to maintain
x * y = k
(200,000) - After the trade, weāll have
9 ETH
in the pool (xā) - To maintain the constant product:
9 * y' = 200,000
- So
y' = 200,000 / 9 ā 22,222 USDC
- The pool had 20,000 USDC, now it has 22,222 USDC
- The user must put in:
22,222 - 20,000 = 2,222 USDC
So the price for 1 ETH is 2,222 USDC, which is higher than the initial price of 2,000 USDC. This difference is called slippage.
Price Impact and Slippage
The larger your trade relative to the pool size, the more the price moves against you. This is called price impact.
For small trades in large pools, the price impact is minimal. For large trades in small pools, it can be significant.
Visualizing the Bonding Curve
The constant product formula creates a hyperbolic curve called a bonding curve:
As you move along this curve:
- The price of tokens changes
- The product of the token quantities remains constant
- Larger trades cause more slippage
From Uniswap v2 to v3: Concentrated Liquidity
Uniswap v2 spreads liquidity across the entire price range from 0 to infinity, which is inefficient. Most trading happens within a narrow price range.
Uniswap v3 introduced concentrated liquidity, allowing LPs to provide liquidity within specific price ranges. This makes liquidity provision much more capital-efficient.
In the next section, weāll dive into Uniswap v3ās implementation and understand the key concepts like ticks and sqrtPriceX96.