By Austin Adams, Sara Reynolds, Kirill Naumov, and Rachel Eichenberger
Introduction to Virtual Liquidity in Uniswap v3
Uniswap v3 revolutionized decentralized finance by introducing concentrated liquidity, allowing liquidity providers (LPs) to allocate capital within specific price ranges. This creates virtual liquidityโa mathematical construct that mimics traditional AMM behavior within defined bounds.
Key differences from Uniswap v2:
- Capital Efficiency: $1M in a 0.999โ1.001 USDC/DAI range provides 500,000 USDC liquidity vs. just 200 USDC in v2.
- Customizable Ranges: LPs earn fees only when price trades within their set bounds.
- Virtual Reserves: The protocol calculates token amounts using modified
xy=kformulas adapted for bounded ranges.
๐ Explore Uniswap v3's architecture
Calculating LP Position Holdings
Step 1: Determine Position Status
A position is in-range when:
tickLower โค currentTick < tickUpperQuery these values from:
NonfungiblePositionManager:liquidity,tickUpper,tickLowerUniswapV3Pool:sqrtPriceX96(derivescurrentTick)
Step 2: Token Amount Calculations
In-Range Positions
Use virtual reserve formulas:
amount0 = liquidity ร (1/โP - 1/โP_upper)
amount1 = liquidity ร (โP - โP_lower)Out-of-Range Positions
- Below Range: Entirely in
token0 - Above Range: Entirely in
token1
Example: A WETH/USDC position at tick 201780 (out-of-range) holds only WETH.
Fee Accumulation Mechanics
Required Variables
| Variable | Source Contract | Purpose |
|---|---|---|
feeGrowthGlobal0X128 | Pool | Total fees per liquidity unit |
feeGrowthOutside0X128 | Pool (per tick) | Fees outside position bounds |
feeGrowthInside0LastX128 | PositionManager | Last recorded in-range fees |
Fee Calculation Formula
uncollectedFees = liquidity ร [fr(t1) - fr(t0)]Where:
fr(t1): Current fee growth inside position rangefr(t0): Fee growth at last interaction
๐ Advanced fee calculation examples
Practical Implementation
JavaScript Code Snippets
// Calculate current tick from sqrtPriceX96
function getTickAtSqrtPrice(sqrtPriceX96) {
return Math.floor(Math.log((sqrtPriceX96/Q96)**2)/Math.log(1.0001));
}
// Fee computation logic
const fees = (liquidity * (fr_t1 - fr_t0)) / Q128;Note: Always adjust for token decimals (e.g., divide by 10^18 for WETH).
FAQs
Q: How does virtual liquidity improve capital efficiency?
A: By concentrating funds where most trading occurs, LPs achieve higher fee income per dollar deployed compared to v2's full-range distribution.
Q: What happens when a position goes out-of-range?
A: The position converts entirely to one asset until the price re-enters the range, requiring manual rebalancing or fee collection.
Q: How often should I collect fees?
A: Frequent collection minimizes compounding errors from price movements, but balances gas costs against fee growth.
Conclusion
Uniswap v3's math enables precise liquidity management through:
- Virtual reserve calculations
- Dynamic position rebalancing
- Gas-optimized fee tracking
For deeper analysis, refer to the Passive Fee Returns Paper or join developer discussions in Uniswap's Discord.