MathFin
Map / Valuation · Static replication and model-free bounds / MathFin.Markets.DeFi.UniswapMechanics

Uniswap v2/v3 AMM mechanics: impermanent loss, TWAP, fee invariant,

MathFin.Markets.DeFi.UniswapMechanicsMSC 91G80JEL G23 source

v3 real-reserves curve

This file extends the constant-product AMM formalization (MathFin/Markets/DeFi/ConstantProductAMM.lean, adapted from Pusceddu-Bartoletti 2024) with further closed-form identities from the Uniswap v2/v3 AMM designs:

Hayden Adams, Noah Zinsmeister, Dan Robinson, "Uniswap v2 Core", Uniswap whitepaper, 2020. <https://uniswap.org/whitepaper.pdf>

Hayden Adams et al., "Uniswap v3 Core", Uniswap whitepaper, 2021. <https://uniswap.org/whitepaper-v3.pdf>

Curve Finance's StableSwap invariant (Michael Egorov, "StableSwap - efficient mechanism for Stablecoin liquidity", 2019, <https://curve.fi/files/stableswap-paper.pdf>) is not formalized in this file; no StableSwap-specific result appears below.

This file formalises further closed-form results about automated market makers, building on the constant-product substrate in ConstantProductAMM.lean.

Main definitions

  • impermanentLossReservesX, impermanentLossReservesY: the rebalanced reserves √(x0² / r) and √(y0² r) after the external price moves by the factor r.
  • impermanentLoss: the closed form 2 √r / (1 + r) - 1.
  • twapAccumulator, twap: the running sum ∑_{i ≤ t} p i of a price series and the quotient of its increment by t2 - t1.
  • harmonicMean: |s| / ∑ i ∈ s, 1 / q i.
  • swapOutputWithFee: swapOutput x y (γ * Δx), the swap output when only γ Δx is credited.
  • lpFeeShare: 1 - √(k1 / k2).
  • realReserveX, realReserveY: the in-range real reserves L (1/√P - 1/√p_b) and L (√P - √p_a) of a Uniswap v3 position.
  • v3PositionValue: the value in token Y of a v3 position with liquidity L and range [a, b] in square-root price s, piecewise below, inside and above the range.
  • tickPrice: 1.0001 ^ i for an integer tick i.

Main results

  • impermanentLossReserves_invariant, impermanentLossReserves_price: for x0, y0, r > 0, the rebalanced reserves have product x0·y0 and price y1/x1 = r·y0/x0.
  • impermanentLoss_eq: for x0, y0, r > 0, the value of the rebalanced reserves at the moved price over the value of the held reserves, minus one, equals impermanentLoss r.
  • impermanentLoss_eq_neg_sq, impermanentLoss_nonpos, impermanentLoss_eq_zero_iff: for r ≥ 0, IL(r) = −(√r − 1)²/(1 + r), it is never positive, and it vanishes if and only if r = 1. Fees are not modelled.
  • twapAccumulator_sub_eq_sum, twap_eq_average: for t1 ≤ t2, the accumulator difference is the window sum ∑_{t1 < i ≤ t2} p_i, so twap p t1 t2 is that sum over t2 − t1 (a genuine average only for t1 < t2).
  • harmonicMean_reciprocal_eq_arithmeticMean: the reciprocal of the harmonic mean of 1/p over s is the arithmetic mean of p. The AM–HM inequality is not stated.
  • fee_invariant_growth: for x0, y0, Δx > 0 and 0 < γ ≤ 1, x0·y0 ≤ (x0 + Δx)(y0 − Δy) with Δy computed from the fee-discounted input γ·Δx.
  • realReserves_curve: for positive √P and √p_b, the in-range real reserves, shifted by L/√p_b and L√p_a, have product L².
  • v3PositionValue_at_lower, v3PositionValue_at_upper, v3PositionValue_nonneg: the position is worth all token X at s = a, all token Y at s = b (for a < b), and is nonnegative for L ≥ 0 and 0 < a ≤ b.
  • v3PositionValue_eq_coveredCall_of_le, v3PositionValue_eq_coveredCall_of_lt, v3PositionValue_le_coveredCall: with 0 < a ≤ b, the position's value equals the covered-call payoff x_max·(s² − max (s² − a·b) 0) for 0 ≤ s ≤ a and for s > b, and is bounded above by it for every s ≥ 0.
  • coveredCall_sub_v3PositionValue: for L ≥ 0 and a < s ≤ b, the covered-call payoff exceeds the position's value by exactly min (L(s − a)²/a) (L(b − s)²/b). The resulting Black–Scholes price bound is integral_discounted_v3PositionValue_le in MathFin/Markets/DeFi/UniswapV3CoveredCallPrice.lean.
  • tickPrice_ratio: tickPrice (i + 1) / tickPrice i = 1.0001.

References

Tags

impermanent loss, Uniswap v3, TWAP, concentrated liquidity, liquidity provider, AMM fee, covered call, MSC 91G80, JEL G23

Import neighbourhood

Above: what it imports. Below: what imports it.

Corpus entries

mf-cpmm-impermanent-loss-nonpositive · defi.json · full statement
Constant-Product Impermanent Loss Is Nonpositive

For a fee-free constant-product pool with deposit (x0, y0) and a price move by the factor r > 0, the LP-to-HODL value ratio minus one, computed from the rebalanced reserves, equals IL(r) = 2 sqrt(r)/(1 + r) - 1 = -(sqrt(r) - 1)^2/(1 + r); hence IL(r) <= 0, with IL(r) = 0 if and only if r = 1. Fees are not modelled.

mf-uniswap-v3-position-covered-call · defi.json · full statement
Uniswap v3 Position Value as a Covered Call

For a Uniswap v3 position with liquidity L >= 0 on the square-root price range [a, b] (0 < a < b), its value in token Y at square-root price s >= 0 (all token X below the range, the real reserves inside, all token Y above) is all X at s = a and all Y at s = b; it equals the covered-call payoff x_max * (s^2 - max(s^2 - ab, 0)) (long the full X holding x_max, short a call struck at sqrt(p_a p_b) = ab) outside the range, is at most that payoff everywhere, and inside the range falls short of it by exactly min(L (s - a)^2 / a, L (b - s)^2 / b). Fees are not modelled.

Declarations 37

defMathFin.DeFi.impermanentLossReservesXsource
ℝ → ℝ → ℝ → ℝ

After the external price moves by factor r (new price = r times the price at deposit), a constant-product pool with invariant k = x0 * y0 and deposit-time price y0 / x0 re-balances to the reserves that keep k fixed at the new price r * y0 / x0. This is the new reserve of token X: solving x1 * y1 = x0 * y0 and y1 / x1 = r * y0 / x0 for positive reserves gives x1 = sqrt (x0^2 / r).

defMathFin.DeFi.impermanentLossReservesYsource
ℝ → ℝ → ℝ → ℝ

The new reserve of token Y after the same price move: y1 = sqrt (y0^2 * r).

theoremMathFin.DeFi.impermanentLossReserves_invariantsource
∀ (x0 y0 r : ℝ), 0 < x0 → 0 < y0 → 0 < r → impermanentLossReservesX x0 y0 r * impermanentLossReservesY x0 y0 r = x0 * y0

The rebalanced reserves again satisfy the constant-product invariant.

theoremMathFin.DeFi.impermanentLossReserves_pricesource
∀ (x0 y0 r : ℝ), 0 < x0 → 0 < y0 → 0 < r → impermanentLossReservesY x0 y0 r / impermanentLossReservesX x0 y0 r = r * y0 / x0

The rebalanced reserves sit at the new price r * y0 / x0: this is the second defining equation, so with impermanentLossReserves_invariant the defined pair satisfies both x1 * y1 = x0 * y0 and y1 / x1 = r * y0 / x0, not merely the invariant. That it is the only positive solution is not stated.

theoremMathFin.DeFi.lpPortfolioValue_eqsource
∀ (x0 y0 r : ℝ), 0 < x0 → 0 < y0 → 0 < r → impermanentLossReservesX x0 y0 r * (r * y0 / x0) + impermanentLossReservesY x0 y0 r = 2 * y0 * √r

LP portfolio value at the new price p1 = r * y0 / x0, valued in token Y: x1 * p1 + y1. Derived from the rebalanced reserves above, this equals 2 * y0 * sqrt r — the algebraic core of the impermanent-loss computation.

theoremMathFin.DeFi.hodlPortfolioValue_eqsource
∀ (x0 y0 r : ℝ), 0 < x0 → x0 * (r * y0 / x0) + y0 = y0 * (r + 1)

HODL portfolio value at the same new price: x0 * p1 + y0, which simplifies to y0 * (r + 1) since x0 * (y0/x0) = y0.

defMathFin.DeFi.impermanentLosssource
ℝ → ℝ

Impermanent loss closed form: IL(r) = 2 * sqrt r / (1 + r) - 1.

theoremMathFin.DeFi.impermanentLoss_eqsource
∀ (x0 y0 r : ℝ), 0 < x0 → 0 < y0 → 0 < r → (impermanentLossReservesX x0 y0 r * (r * y0 / x0) + impermanentLossReservesY x0 y0 r) / (x0 * (r * y0 / x0) + y0) - 1 = impermanentLoss r

Impermanent loss theorem: the LP-vs-HODL value ratio, minus one, is exactly the closed form 2√r/(1+r) - 1, independent of the deposit amounts x0, y0.

theoremMathFin.DeFi.impermanentLoss_eq_neg_sqsource
∀ {r : ℝ}, 0 ≤ r → impermanentLoss r = -(√r - 1) ^ 2 / (1 + r)

Impermanent loss is a negative square: for r ≥ 0, IL(r) = 2√r/(1 + r) − 1 = −(√r − 1)²/(1 + r).

theoremMathFin.DeFi.impermanentLoss_nonpossource
∀ {r : ℝ}, 0 ≤ r → impermanentLoss r ≤ 0

Impermanent loss is never positive (AM–GM, 2√r ≤ 1 + r): a fee-free constant-product LP never ends a price move ahead of holding the deposit.

theoremMathFin.DeFi.impermanentLoss_eq_zero_iffsource
∀ {r : ℝ}, 0 ≤ r → (impermanentLoss r = 0 ↔ r = 1)

Impermanent loss vanishes exactly when the price returns: for r ≥ 0, IL(r) = 0 ↔ r = 1.

defMathFin.DeFi.twapAccumulatorsource
(ℕ → ℝ) → ℕ → ℝ

TWAP accumulator: the running sum a_t = Σ_{i ≤ t} p_i of a discrete price series with unit time steps — the special case of the Uniswap v2 price*CumulativeLast storage slots (which accumulate p·Δt over arbitrary block-timestamp gaps) where every step has Δt = 1.

theoremMathFin.DeFi.twapAccumulator_sub_eq_sumsource
∀ (p : ℕ → ℝ) (t1 t2 : ℕ), t1 ≤ t2 → twapAccumulator p t2 - twapAccumulator p t1 = ∑ i ∈ Finset.Ico (t1 + 1) (t2 + 1), p i

Telescoping-sum identity: the accumulator difference over (t1, t2] equals the sum of prices over that window.

used by 1: twap_eq_average
defMathFin.DeFi.twapsource
(ℕ → ℝ) → ℕ → ℕ → ℝ

Time-weighted average price over unit time steps, matching how Uniswap v2 oracles compute it in the equal-spacing case: p_{t1,t2} = (a_{t2} - a_{t1}) / (t2 - t1).

used by 1: twap_eq_average
theoremMathFin.DeFi.twap_eq_averagesource
∀ (p : ℕ → ℝ) (t1 t2 : ℕ), t1 ≤ t2 → twap p t1 t2 = (∑ i ∈ Finset.Ico (t1 + 1) (t2 + 1), p i) / (↑t2 - ↑t1)

The TWAP equals the arithmetic mean of the price series over the window (t1, t2], i.e. the telescoping accumulator identity really does compute a time-weighted average — for t1 < t2; at t1 = t2 both sides reduce to _ / 0 = 0 by Lean's junk-value convention for division, not a genuine average over an empty window.

defMathFin.DeFi.harmonicMeansource
Finset ℕ → (ℕ → ℝ) → ℝ

The harmonic mean |s| / ∑_{i∈s} 1/q_i of a finite family of values q (meant for a nonempty family of positive prices; neither hypothesis is built in).

theoremMathFin.DeFi.harmonicMean_reciprocal_eq_arithmeticMeansource
∀ (s : Finset ℕ) (p : ℕ → ℝ), (1 / harmonicMean s fun i => 1 / p i) = (∑ i ∈ s, p i) / ↑s.card

If q is the reciprocal price series (B-in-A, when p is A-in-B), the reciprocal of the harmonic mean of q equals the arithmetic mean of p. That this generally differs from the reciprocal of the arithmetic mean of q (the AM–HM inequality, with equality iff p is constant) is the caveat behind the name, and it is not stated here. Intended for strictly positive values (prices), but stated without that hypothesis: the identity is a div/div junk-value tautology that holds unconditionally for any finite family s, including the empty one (both sides reduce to 0).

defMathFin.DeFi.swapOutputWithFeesource
ℝ → ℝ → ℝ → ℝ → ℝ

Fee-adjusted swap output: Uniswap v2 charges a fee rate 1 - γ (e.g. γ = 0.997 for the standard 0.3% fee), applying the constant-product formula to only the γ-fraction of the input that is actually swapped; the remaining (1-γ) * Δx stays in the pool as fee revenue.

theoremMathFin.DeFi.fee_invariant_growthsource
∀ (x0 y0 Δx γ : ℝ), 0 < x0 → 0 < y0 → 0 < Δx → 0 < γ → γ ≤ 1 → x0 * y0 ≤ (x0 + Δx) * (y0 - swapOutputWithFee x0 y0 Δx γ)

Fee invariant growth: crediting the full input Δx to the X reserve (the fee portion is retained, not refunded) while only the fee-adjusted output Δy leaves the Y reserve makes the invariant grow (weakly) rather than merely hold:

x0 * y0 ≤ (x0 + Δx) * (y0 - Δy),

since (x0 + γΔx) * (y0 - Δy) = x0 * y0 exactly (the no-fee invariant) and x0 + Δx ≥ x0 + γΔx with y0 - Δy > 0.

defMathFin.DeFi.lpFeeSharesource
ℝ → ℝ → ℝ

LP fee share candidate: under the (unproved here) assumption that LP token value scales with sqrt k — the LP's claim on reserves scales reserves uniformly, so value scales with the geometric mean sqrt(x*y) — f = 1 - sqrt(k1/k2) would be the fraction of value growth attributable to the invariant moving from k1 to k2 purely from retained fees. This file only establishes the sanity property lpFeeShare_nonneg below; it does not prove f equals an actual LP value-growth ratio.

theoremMathFin.DeFi.lpFeeShare_nonnegsource
∀ (k1 k2 : ℝ), 0 < k2 → k1 ≤ k2 → 0 ≤ lpFeeShare k1 k2

lpFeeShare is nonnegative when the invariant has not shrunk, matching the fee economics (fees only grow k).

defMathFin.DeFi.realReserveXsource
ℝ → ℝ → ℝ → ℝ

Real reserve of token X in a Uniswap v3 concentrated-liquidity position with liquidity L, square-root price √P, and range [√p_a, √p_b] (v3 whitepaper §2, the x of eq. 2.2): x = L(1/√P − 1/√p_b). Adding L/√p_b gives the virtual reserve L/√P. This formula represents the position's in-range reserves only — valid while √p_a ≤ √P ≤ √p_b; once the price moves below the range the position's actual X reserve saturates instead of following this curve (and above the range it holds no X at all), which v3PositionValue below models (all X below the range, all Y above).

defMathFin.DeFi.realReserveYsource
ℝ → ℝ → ℝ → ℝ

Real reserve of token Y for the same position (the y of eq. 2.2): y = L(√P − √p_a). Adding L√p_a gives the virtual reserve L√P. As with realReserveX, this represents the in-range reserve only — valid while √p_a ≤ √P ≤ √p_b; outside the range the actual Y reserve saturates rather than following this curve, as v3PositionValue below models.

theoremMathFin.DeFi.realReserves_curvesource
∀ (L sqrtP sqrtPa sqrtPb : ℝ), 0 < sqrtP → 0 < sqrtPb → (realReserveX L sqrtP sqrtPb + L / sqrtPb) * (realReserveY L sqrtP sqrtPa + L * sqrtPa) = L ^ 2

Uniswap v3 real-reserves curve (v3 whitepaper §2, eq. 2.2): the position's real reserves, shifted by L/√p_b and L√p_a (that is, the virtual reserves L/√P and L√P), satisfy the same constant-product form as a v2 pool with liquidity L:

(x + L/√p_b) * (y + L√p_a) = L².

defMathFin.DeFi.v3PositionValuesource
ℝ → ℝ → ℝ → ℝ → ℝ

Value of a Uniswap v3 position, in token Y, at square-root price s, for liquidity L on the square-root price range [a, b] (a = √p_a, b = √p_b). Below the range the position holds only token X, its full amount realReserveX L a b, worth s² each; inside the range it holds the real reserves realReserveY L s a and realReserveX L s b; above the range it holds only token Y, its full amount realReserveY L b a (v3 whitepaper §6.4.1, eqs. 6.29–6.30).

theoremMathFin.DeFi.v3PositionValue_at_lowersource
∀ (L a b : ℝ), v3PositionValue L a b a = a ^ 2 * realReserveX L a b

All token X at the lower end of the range: at s = a the position is worth its full X holding realReserveX L a b at price a².

theoremMathFin.DeFi.v3PositionValue_at_uppersource
∀ (L : ℝ) {a b : ℝ}, a < b → v3PositionValue L a b b = realReserveY L b a

All token Y at the upper end of the range: at s = b the in-range X reserve is zero and the position is worth its full Y holding realReserveY L b a.

theoremMathFin.DeFi.v3PositionValue_nonnegsource
∀ {L a b s : ℝ}, 0 ≤ L → 0 < a → a ≤ b → 0 ≤ v3PositionValue L a b s

A v3 position has nonnegative value for L ≥ 0 and 0 < a ≤ b.

theoremMathFin.DeFi.v3PositionValue_const_mulsource
∀ (c L a b s : ℝ), v3PositionValue (c * L) a b s = c * v3PositionValue L a b s

A v3 position's value is linear in its liquidity. realReserveX/realReserveY are literally L times a function of the price range and current price alone, and the three-way case split in v3PositionValue does not depend on L — so scaling L by any real c (not just c ≥ 0) scales the position's value at every square-root price s by the same factor.

theoremMathFin.DeFi.v3PositionValue_eq_coveredCall_of_lesource
∀ {L a b s : ℝ}, 0 < a → a ≤ b → 0 ≤ s → s ≤ a → v3PositionValue L a b s = realReserveX L a b * (s ^ 2 - max (s ^ 2 - a * b) 0)

Below the range the position is the covered call: for 0 ≤ s ≤ a, v3PositionValue L a b s = x_max · (s² − max (s² − ab) 0) with x_max = realReserveX L a b (here s² ≤ ab, so the call is out of the money).

theoremMathFin.DeFi.v3PositionValue_eq_coveredCall_of_ltsource
∀ {L a b s : ℝ}, 0 < a → a ≤ b → b < s → v3PositionValue L a b s = realReserveX L a b * (s ^ 2 - max (s ^ 2 - a * b) 0)

Above the range the position is the covered call: for b < s, v3PositionValue L a b s = x_max · (s² − max (s² − ab) 0) = x_max · ab = L (b − a), the full Y holding (here s² > ab, so the call is in the money and caps the payoff).

theoremMathFin.DeFi.realReserveX_mul_sq_sub_inRangesource
∀ {L a b s : ℝ}, a ≠ 0 → b ≠ 0 → s ≠ 0 → realReserveX L a b * s ^ 2 - (realReserveY L s a + s ^ 2 * realReserveX L s b) = L * (s - a) ^ 2 / a

In-range identity behind coveredCall_sub_v3PositionValue: holding all of x_max at price s² beats the in-range position by L (s − a)² / a.

theoremMathFin.DeFi.realReserveX_mul_mul_sub_inRangesource
∀ {L a b s : ℝ}, a ≠ 0 → b ≠ 0 → s ≠ 0 → realReserveX L a b * (a * b) - (realReserveY L s a + s ^ 2 * realReserveX L s b) = L * (b - s) ^ 2 / b

In-range identity behind coveredCall_sub_v3PositionValue: the capped payoff x_max · ab beats the in-range position by L (b − s)² / b.

theoremMathFin.DeFi.coveredCall_sub_v3PositionValuesource
∀ {L a b s : ℝ}, 0 ≤ L → 0 < a → a < s → s ≤ b → realReserveX L a b * (s ^ 2 - max (s ^ 2 - a * b) 0) - v3PositionValue L a b s = min (L * (s - a) ^ 2 / a) (L * (b - s) ^ 2 / b)

Inside the range the covered call exceeds the position by the smaller squared gap. For L ≥ 0, 0 < a < s ≤ b: x_max · (s² − max (s² − ab) 0) − v3PositionValue L a b s = min (L(s − a)²/a) (L(b − s)²/b).

theoremMathFin.DeFi.v3PositionValue_le_coveredCallsource
∀ {L a b s : ℝ}, 0 ≤ L → 0 < a → a ≤ b → 0 ≤ s → v3PositionValue L a b s ≤ realReserveX L a b * (s ^ 2 - max (s ^ 2 - a * b) 0)

A v3 position is worth at most the covered call x_max · (s² − max (s² − ab) 0): long its full X holding x_max = realReserveX L a b, short a call struck at the geometric-mean price ab = √(p_a p_b). Equality holds outside the range (v3PositionValue_eq_coveredCall_of_le, v3PositionValue_eq_coveredCall_of_lt); inside it the gap is coveredCall_sub_v3PositionValue.

defMathFin.DeFi.tickPricesource
ℤ → ℝ

Tick price: Uniswap v3 discretizes the price axis into ticks i (integers) with p(i) = 1.0001^i, so every tick is 0.01% apart from its neighbor.

used by 1: tickPrice_ratio
theoremMathFin.DeFi.tickPrice_ratiosource
∀ (i : ℤ), tickPrice (i + 1) / tickPrice i = 1.0001

Consecutive ticks have the fixed ratio 1.0001.