← Back to Articles
DeFi Engineering • Quantitative Finance

DeFi AMM Mathematics & Concentrated Liquidity: Invariant Curves & Tick Spacing

DeFi AMM Mathematics & Concentrated Liquidity: Invariant Curves & Tick Spacing
Uniswap v3 Concentrated Liquidity Hyperbola and Virtual Reserve Bounds
Executive Summary & Key Security Takeaways
  • Constant Product Invariant: Understand the core bonding curve equation (x * y = k) and marginal price calculation (P = y / x).
  • Concentrated Liquidity: Allocate capital within custom price intervals [p_lower, p_upper] to achieve up to 4000x capital efficiency over Uniswap v2.
  • Virtual Reserves Math: Shift bonding curves along the coordinate axes using real reserves (x, y) and virtual reserves (x_v, y_v).
  • Tick Index Calculation: Discretize price space into geometric price ticks (p(i) = 1.0001^i) with Q64.96 fixed-point arithmetic.

1. Constant Product Foundations & Price Impact Derivation

Automated Market Makers (AMMs) replace traditional centralized order books with deterministic mathematical invariant curves. In Uniswap v2, liquidity pools hold two tokens ($ and $) satisfying the Constant Product formula: \cdot y = k$, where $ remains constant throughout swap operations.

The marginal spot price of Token $ in terms of Token $ is defined as the negative derivative of the invariant curve: = rac{y}{x}$. When a trader swaps $\Delta x$ tokens into the pool, the output $\Delta y$ received by the trader is derived by preserving the constant product:

\cdot (y - \Delta y) = k \implies \Delta y = rac{y \cdot \Delta x}{x + \Delta x}$.

As transaction size $\Delta x$ increases relative to total reserves $, the effective execution price degrades relative to the initial spot price, generating Price Impact and Slippage.

// Python: Derivation of Exact Swap Output with Trading Fees (gamma = 1 - fee)
def compute_amm_swap_output(reserve_x, reserve_y, delta_x, fee_tier=0.003):
    gamma = 1.0 - fee_tier
    delta_x_with_fee = delta_x * gamma
    delta_y = (reserve_y * delta_x_with_fee) / (reserve_x + delta_x_with_fee)
    new_price = (reserve_y - delta_y) / (reserve_x + delta_x)
    
    return {
        "amount_out_y": delta_y,
        "new_reserve_x": reserve_x + delta_x,
        "new_reserve_y": reserve_y - delta_y,
        "effective_price": delta_x / delta_y,
        "marginal_price_after": new_price
    }

2. Concentrated Liquidity & Virtual Reserves in Uniswap v3

In constant product pools (Uniswap v2), liquidity is distributed uniformly across the infinite price range \in (0, \infty)$. Because real trading pairs fluctuate within bounded price intervals, over 99% of deposited capital sits idle in extreme tails, earning minimal fees.

Uniswap v3 introduces Concentrated Liquidity: Liquidity Providers (LPs) allocate capital within bounded price ranges 0$. Inside the range, the pool behaves as a standard constant product curve with magnified Virtual Reserves (, y_v$), shifting the curve so that reserves deplete exactly when the price crosses the range boundaries.

The relationship between real reserves $, liquidity = \sqrt{x_v \cdot y_v}$, and price bounds $ is governed by the core virtual reserve equations:

= L \cdot \left( rac{1}{\sqrt{P}} - rac{1}{\sqrt{p_b}} ight), \quad y = L \cdot \left(\sqrt{P} - \sqrt{p_a} ight)$.

// Solidity: Calculating Liquidity L from Price Bounds (Q64.96 Fixed-Point)
pragma solidity ^0.8.24;

library SqrtPriceMath {
    // Compute liquidity from token0 amount: L = amount0 * (sqrtRatioA * sqrtRatioB) / (sqrtRatioB - sqrtRatioA)
    function getLiquidityForAmount0(
        uint160 sqrtRatioAX96,
        uint160 sqrtRatioBX96,
        uint256 amount0
    ) internal pure returns (uint128 liquidity) {
        if (sqrtRatioAX96 > sqrtRatioBX96) {
            (sqrtRatioAX96, sqrtRatioBX96) = (sqrtRatioBX96, sqrtRatioAX96);
        }
        uint256 intermediate = (uint256(sqrtRatioAX96) * sqrtRatioBX96) >> 96;
        liquidity = uint128((amount0 * intermediate) / (sqrtRatioBX96 - sqrtRatioAX96));
    }
}

3. Geometric Price Ticks & Q64.96 Fixed-Point Arithmetic

To track liquidity across multiple overlapping positions without looping through thousands of orders, Uniswap v3 discretizes the continuous price domain into discrete geometric Ticks: (i) = 1.0001^i$, where $ is an integer tick index.

Each tick $ corresponds to an exact $\sqrt{P}$ value represented in Q64.96 binary fixed-point format (a 160-bit unsigned integer with 64 bits of integer and 96 bits of fraction).

When a swap pushes the current spot price across a tick boundary, the AMM updates active liquidity $ instantaneously by adding or subtracting the $\Delta L$ delta stored in the tick bitmap data structure.

// Python: Tick to SqrtPriceX96 Conversion
def tick_to_sqrt_price_x96(tick_index):
    price = 1.0001 ** tick_index
    sqrt_price = price ** 0.5
    sqrt_price_x96 = int(sqrt_price * (2 ** 96))
    return sqrt_price_x96

def sqrt_price_x96_to_tick(sqrt_price_x96):
    import math
    sqrt_price = sqrt_price_x96 / (2 ** 96)
    price = sqrt_price ** 2
    tick = math.log(price) / math.log(1.0001)
    return round(tick)

4. Impermanent Loss Dynamics & Quantitative LP Risk Profiling

While concentrated liquidity amplifies fee earnings by up to 4000x for stable pairs, it proportionally magnifies Impermanent Loss (IL) risk if market price moves outside the allocated range 0$. Once the price exits the range, the LP position is 100% converted into the depreciating asset and stops accumulating swap fees.

Professional DeFi market makers utilize dynamic hedging algorithms (such as perpetual futures deltas or options straddles) to neutralize directional market exposure and harvest pure swap fee yield.

// Python: Concentrated Impermanent Loss Model
def compute_concentrated_il(price_ratio, p_lower, p_upper):
    import math
    # Ratio of current price over initial price
    r = price_ratio
    r_a = p_lower
    r_b = p_upper
    
    v_hold = 0.5 * (1.0 + r)
    v_lp = (2 * math.sqrt(r) - math.sqrt(r_a) - r / math.sqrt(r_b)) / (2 - math.sqrt(r_a) - 1.0 / math.sqrt(r_b))
    
    impermanent_loss = (v_lp / v_hold) - 1.0
    return impermanent_loss

Frequently Asked Questions (FAQ)

How does concentrated liquidity increase capital efficiency for LPs?

By restricting liquidity to a tight price interval where trading actually occurs (e.g. 0.999 - 1.001 for stablecoin pairs), LPs achieve the same market depth as a Uniswap v2 pool with a fraction of the capital, multiplying swap fee yield proportionally.

What is Q64.96 fixed-point notation used in Uniswap v3?

Q64.96 is a binary fixed-point number format with 64 bits before the decimal point and 96 bits after. It provides high precision without floating-point rounding errors in EVM integer arithmetic.

What happens when the spot price exits an LP concentrated range?

The position is completely converted into the less valuable of the two tokens (all Token X if price drops below the range, all Token Y if price exceeds the range) and ceases to earn swap fees until price returns inside the range.

Zyekh Abdul Qadir Jailani

Written by Zyekh Abdul Qadir Jailani

Digital Forensics & Incident Response (DFIR) Specialist & Security Researcher specializing in Linux kernel hardening, threat hunting, and system security research.

Utility Security Tools Related to this Article:

Gunakan KPR Calculator dan Hash Generator untuk membantu alur kerja konfigurasi keamanan Anda secara privasi di browser.