Understanding Uniswap v3 Liquidity Math: Virtual Reserves, LP Holdings, and Fee Calculations

ยท

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:

๐Ÿ‘‰ Explore Uniswap v3's architecture


Calculating LP Position Holdings

Step 1: Determine Position Status

A position is in-range when:

tickLower โ‰ค currentTick < tickUpper

Query these values from:

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

Example: A WETH/USDC position at tick 201780 (out-of-range) holds only WETH.


Fee Accumulation Mechanics

Required Variables

VariableSource ContractPurpose
feeGrowthGlobal0X128PoolTotal fees per liquidity unit
feeGrowthOutside0X128Pool (per tick)Fees outside position bounds
feeGrowthInside0LastX128PositionManagerLast recorded in-range fees

Fee Calculation Formula

uncollectedFees = liquidity ร— [fr(t1) - fr(t0)]

Where:

๐Ÿ‘‰ 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:

  1. Virtual reserve calculations
  2. Dynamic position rebalancing
  3. Gas-optimized fee tracking

For deeper analysis, refer to the Passive Fee Returns Paper or join developer discussions in Uniswap's Discord.