Introduction
After reviewing Uniswap v2's code, I initially planned to write about its design and implementation. However, with the release of Uniswap v3, I've decided to dive into the newer version first. Given its heightened complexity compared to v2, this series aims to spark discussion and welcomes community insights.
Prerequisites:
- Familiarity with AMM concepts
- Basic understanding of v3 whitepaper
Key Improvements in Uniswap v3
1. Capital Efficiency
Uniswap v2’s liquidity pools suffered from low capital utilization—often below 25% for volatile pairs and worse for stablecoins. V3 solves this by allowing concentrated liquidity within customizable price ranges.
2. Flexible Price Ranges
Users can now provide liquidity within specific price intervals (e.g., 1300–2200 DAI/ETH). This is achieved through virtual reserves that maintain the $x \cdot y = k$ invariant without locking full amounts.
3. Enhanced Oracle
V3’s oracle reduces gas costs and improves accuracy by storing time-weighted average prices (TWAPs) for the last 9+ days.
4. Order Book Functionality
Liquidity providers (LPs) can emulate limit orders by setting narrow price ranges, effectively acting as market makers.
5. Tiered Fees
Three fee tiers (0.05%, 0.30%, 1.00%) cater to different pair volatilities:
- Stable pairs: Lower fees + tighter tick spacing (
tickSpacing=10). - Volatile pairs: Higher fees + wider spacing (
tickSpacing=200).
How Concentrated Liquidity Works
Virtual Reserves
When LPs allocate funds to a range $[a, b]$, virtual tokens ($x_{virtual}$, $y_{virtual}$) adjust the curve to maintain $k$:
$$ x = x_{real} + x_{virtual} \\ y = y_{real} + y_{virtual} $$
- Out-of-range: Liquidity is deactivated (e.g., if price < $a$, $y_{real}=0$).
- In-range: Virtual reserves ensure smooth pricing.
Example: ETH/DAI Pool
- Initial price: 1500 DAI/ETH.
- Range: 1300–2200 DAI/ETH.
- Utilization: 6.84% at 1300 DAI/ETH; 21.45% at 2200 DAI/ETH.
Price Ticks & Computational Efficiency
Tick System
V3 discretizes price ranges into ticks (spacing based on fee tier):
- Formula: $i = \log_{\sqrt{1.0001}} \sqrt{P}$
- Bounds: $-887272 \leq i \leq 887272$
👉 Explore how ticks optimize gas usage
Why Ticks?
- Avoids expensive floating-point math.
- Limits storage to active liquidity bounds (e.g., only tracks $tick_{lower}$ and $tick_{upper}$).
Architecture & Contracts
Core Contracts
- UniswapV3Factory: Deploys/tracks pools.
- UniswapV3Pool: Handles swaps, fees, and oracle data.
Peripheral Contracts
- SwapRouter: User-friendly swap interface.
- NonfungiblePositionManager: Manages ERC721-based liquidity positions.
FAQ
Q: How does v3 improve LP returns?
A: By concentrating liquidity near the current price, LPs earn more fees with less capital.
Q: Can I adjust my liquidity range later?
A: Yes, via NonfungiblePositionManager.
Q: Why ERC721 instead of ERC20 for liquidity tokens?
A: Positions are non-fungible due to unique price ranges.
Q: What’s the max tick spacing?
A: 200 for 1.00% fee pools.
Q: How are trades split across multiple ticks?
A: The router aggregates swaps across all active price ranges.
👉 Learn more about Uniswap v3’s gas optimizations
Up Next: Creating Pools & Adding Liquidity
Stay tuned for Part 2, where we’ll break down pool initialization and LP strategies.
**Keywords**: Uniswap v3, concentrated liquidity, AMM, capital efficiency, price ticks, tiered fees, virtual reserves.
**SEO Notes**: