Uniswap V3 Concentrated Liquidity Market Making: Math and Implementation
Uniswap V3 Concentrated Liquidity Market Making: Math and Implementation
Uniswap V3's concentrated liquidity is a paradigm shift for on-chain market making. Instead of spreading capital across all prices, you focus it on a specific range — dramatically increasing capital efficiency.
The Core Math
In Uniswap V3, you provide liquidity between price bounds [p_a, p_b]. Your capital efficiency compared to V2 is:
Efficiency = 1 / (1 - sqrt(p_a / p_b))
For a +-5% range: efficiency ≈ 20x. You earn 20x the fees with the same capital.
Calculating Position Parameters
import math
def calculate_position(
current_price: float,
lower_price: float,
upper_price: float,
token0_amount: float = 0,
token1_amount: float = 0,
) -> dict:
"""Calculate Uniswap V3 position parameters."""
sqrt_price = math.sqrt(current_price)
sqrt_lower = math.sqrt(lower_price)
sqrt_upper = math.sqrt(upper_price)
# Calculate liquidity from token amounts
if current_price <= lower_price:
# All token0 (e.g., ETH)
liquidity = token0_amount * (
sqrt_lower * sqrt_upper
) / (sqrt_upper - sqrt_lower)
elif current_price >= upper_price:
# All token1 (e.g., USDC)
liquidity = token1_amount / (sqrt_upper - sqrt_lower)
else:
# Both tokens
liq_0 = token0_amount * (
sqrt_price * sqrt_upper
) / (sqrt_upper - sqrt_price)
liq_1 = token1_amount / (sqrt_price - sqrt_lower)
liquidity = min(liq_0, liq_1)
# Capital efficiency vs V2
efficiency = 1 / (1 - math.sqrt(lower_price / upper_price))
return {
'liquidity': liquidity,
'efficiency_vs_v2': round(efficiency, 1),
'range_width_pct': round(
(upper_price - lower_price) / current_price * 100, 2
),
}
# Example: Provide ETH/USDC liquidity around $3,500
pos = calculate_position(
current_price=3500,
lower_price=3300,
upper_price=3700,
token0_amount=1.0, # 1 ETH
token1_amount=3500, # 3500 USDC
)
print(f"Liquidity: {pos['liquidity']:,.0f}")
print(f"Capital efficiency: {pos['efficiency_vs_v2']}x vs V2")
Estimating Fee Income
def estimate_daily_fees(
pool_volume_24h: float,
pool_tvl: float,
your_liquidity: float,
pool_fee_tier: float = 0.003, # 0.3%
your_range_width: float = 0.10, # +-5%
) -> dict:
"""Estimate daily fee income for a concentrated position."""
total_fees = pool_volume_24h * pool_fee_tier
# Your share of in-range liquidity
# Rough estimate: narrower range = larger share of fees
# when price is within your range
in_range_probability = min(your_range_width * 5, 1.0)
concentration_multiplier = 1 / your_range_width
your_share = (
your_liquidity / pool_tvl
) * concentration_multiplier * in_range_probability
daily_fees = total_fees * your_share
apr = (daily_fees * 365) / (your_liquidity) * 100
return {
'daily_fees_usd': round(daily_fees, 2),
'monthly_fees_usd': round(daily_fees * 30, 2),
'estimated_apr': round(apr, 1),
}
fees = estimate_daily_fees(
pool_volume_24h=50_000_000,
pool_tvl=100_000_000,
your_liquidity=10_000,
your_range_width=0.10,
)
print(f"Estimated daily fees: ${fees['daily_fees_usd']}")
print(f"Estimated APR: {fees['estimated_apr']}%")
Impermanent Loss Calculator
def impermanent_loss(
price_ratio: float,
) -> float:
"""
Calculate impermanent loss for a given price change.
price_ratio = new_price / original_price
"""
return 2 * math.sqrt(price_ratio) / (1 + price_ratio) - 1
# Examples
for change in [0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 2.0]:
il = impermanent_loss(change) * 100
print(f"Price {change:>4.1f}x → IL: {il:>6.2f}%")
Output:
Price 0.8x → IL: -0.62%
Price 0.9x → IL: -0.14%
Price 1.0x → IL: 0.00%
Price 1.1x → IL: -0.14%
Price 1.3x → IL: -0.62%
Price 1.5x → IL: -1.70%
Price 2.0x → IL: -5.72%
Concentrated liquidity market making is one of the most sophisticated DeFi strategies — and it's exactly the kind of strategy that benefits from ClawDUX's verification engine, which can independently calculate expected fee returns and impermanent loss to validate seller claims.
The core logic discussed in this article has been integrated into the ClawDUX API. Access ClawDUX-core for full permissions, or browse the marketplace to discover verified trading strategies.