Uniswap v3 Key Concepts
Uniswap v3 introduced several important innovations, with concentrated liquidity being the most significant. To understand how it works, we need to learn about ticks, sqrtPrice, and other key concepts.
Ticks and Price Ranges
In Uniswap v3, the price space is divided into discrete āticks.ā Each tick represents a specific price point, and liquidity can be provided within a range of ticks.
What is a Tick?
A tick is a logarithmic representation of price. The price at a given tick i
is calculated as:
price = 1.0001^i
This means:
- Tick 0 corresponds to a price of 1.0
- Each increment of 1 tick increases the price by 0.01%
- Moving 100 ticks changes the price by approximately 1%
Ticks allow Uniswap to represent the entire price range from 0 to infinity in a manageable way.
The Mystery of sqrtPriceX96
One of the most confusing aspects of Uniswap v3 is the sqrtPriceX96
value. Letās break it down:
Why Square Root of Price?
Uniswap v3 uses the square root of price (āP
) instead of the actual price (P
) for several reasons:
- It simplifies the math for calculating liquidity and price movements
- It makes liquidity calculations more efficient
- It allows for more precise price tracking between ticks
What is X96?
The X96
part means the value is scaled by 2^96 (a very large number). This is done to maintain precision when working with fixed-point arithmetic in Solidity, which doesnāt support floating-point numbers.
So sqrtPriceX96
is:
sqrtPriceX96 = āP Ć 2^96
Converting Between Price and sqrtPriceX96
To convert a price to sqrtPriceX96:
uint160 sqrtPriceX96 = uint160(sqrt(price) * 2**96);
To convert sqrtPriceX96 back to price:
uint256 price = uint256(sqrtPriceX96) * uint256(sqrtPriceX96) / (2**192);
Liquidity Concentration
In Uniswap v3, liquidity providers specify a price range (lower tick and upper tick) for their liquidity. This creates āvirtualā liquidity thatās only active when the price is within that range.
Benefits of Concentrated Liquidity
- Capital Efficiency: LPs can concentrate their capital where itās most likely to be used
- Custom Risk Profiles: Different price ranges can be chosen based on market expectations
- Higher Returns: Concentrated liquidity can earn more fees with the same amount of capital
Example
If you believe ETH will trade between $1,800 and $2,200:
- You can provide liquidity just within that range
- Your capital is used much more efficiently than in v2
- You earn more fees per dollar of liquidity provided
- But if the price moves outside your range, your liquidity becomes inactive
Position Management
In Uniswap v3, each liquidity position is represented as an NFT (ERC-721 token). This is because each position is unique, defined by:
- The token pair
- The fee tier
- The price range (lower and upper ticks)
- The amount of liquidity
In the next section, weāll look at how to initialize a Uniswap v3 pool and provide liquidity with code examples.