A rigorous analysis of the algebraic foundations underlying decentralized exchange protocols
Abstract
Automated Market Makers (AMMs) represent one of the most consequential algorithmic innovations in decentralized finance. Rather than relying on a traditional order book, AMMs use deterministic mathematical functions to price assets and execute trades. This article derives the core invariant of the constant product market maker, analyzes the phenomenon of impermanent loss through calculus, and examines how Uniswap V3's concentrated liquidity model generalizes the classic formula. We provide formal derivations, numerical examples, and a discussion of the trade-offs inherent in each design.
1. Introduction
Traditional financial exchanges operate via an order book: a continuously updated ledger of buy and sell orders matched by price and time priority. While efficient in liquid markets, this model requires active market-making — counterparties willing to post and update limit orders. In decentralized, permissionless networks, coordinating such activity is costly and slow.
The Automated Market Maker eliminates the order book entirely. Instead, liquidity is pooled into a smart contract, and a deterministic pricing function governs every trade. The seminal realization — first popularized by Uniswap V1 (2018) and formalized in V2 (2020) — is that a simple algebraic invariant is sufficient to create a functional, manipulation-resistant exchange mechanism.
This article focuses primarily on the constant product market maker (CPMM), characterized by the invariant:
x · y = k
where x and y are the reserve quantities of two assets in a liquidity pool, and k is a constant preserved across all trades. We will derive its properties from first principles, analyze its economic implications, and describe how Uniswap V3 extends it with the concept of concentrated liquidity.
2. The Constant Product Invariant
2.1 Formal Definition
Let a liquidity pool hold reserves (x, y) of tokens X and Y respectively. The pool is governed by the invariant:
f(x, y) = x · y = k, where k > 0
A trade is an operation that modifies the reserves while preserving k. Suppose a trader submits Δx units of token X to the pool and receives Δy units of token Y in return. The post-trade state must satisfy:
(x + Δx)(y − Δy) = k = x · y
Solving for Δy:
y − Δy = (x · y) / (x + Δx)
Δy = y − (x · y) / (x + Δx) = (y · Δx) / (x + Δx)
This is the output formula for a CPMM trade. Note several important properties:
-
Δy < yfor all finiteΔx— the pool can never be fully drained. -
Δyis a concave function ofΔx— marginal returns diminish as trade size increases. - There is no separate pricing oracle; price is implicit in the reserve ratio.
2.2 The Spot Price
The spot price of token X in terms of token Y is the marginal cost of an infinitesimal trade, obtained by differentiating the output with respect to input:
P(X/Y) = lim(Δx → 0) of Δy/Δx
= d/d(Δx) [ (y · Δx) / (x + Δx) ] evaluated at Δx = 0
= y / x
Thus, the spot price is simply the ratio of reserves:
P(X/Y) = y / x
This is an elegant result: the pool's "opinion" of the relative value of X and Y is encoded entirely in the reserve ratio. As reserves shift through trading, the spot price adjusts continuously.
2.3 Price Impact and Slippage
For a trade of finite size Δx, the effective price paid per unit of X is:
P_eff = Δy / Δx = y / (x + Δx)
The price impact — the percentage deviation from spot price — is:
Price Impact = 1 − P_eff / P(X/Y)
= 1 − [y / (x + Δx)] / [y / x]
= 1 − x / (x + Δx)
= Δx / (x + Δx)
This shows that price impact is determined entirely by the relative trade size Δx / x. A pool with large reserves (deep liquidity) minimizes slippage for a given trade. This formalizes the intuitive relationship between liquidity depth and trading efficiency.
3. Liquidity Provider Economics and Impermanent Loss
3.1 The Liquidity Share Model
When a liquidity provider (LP) deposits assets into a CPMM pool, they receive LP tokens representing a fractional ownership of the pool. If the pool holds reserves (x, y) and the LP deposits (δx, δy) proportionally, they receive a share:
s = δx / x = δy / y
At any later time, the LP can redeem their share for (s · x', s · y') where (x', y') are the current pool reserves. This seems straightforward, but the reserve ratio may have shifted due to trading activity — leading to the phenomenon of impermanent loss.
3.2 Derivation of Impermanent Loss
Consider an LP who deposits at time t₀ when the price ratio is P₀ = y₀ / x₀, and withdraws at time t₁ when the external market price has moved to P₁.
The pool automatically adjusts its reserves to track the external price (via arbitrage). After arbitrage, the new reserves satisfy both the constant product invariant and the updated price:
x₁ · y₁ = k = x₀ · y₀ and y₁ / x₁ = P₁
Solving:
x₁ = √(k / P₁)
y₁ = √(k · P₁)
Let r = P₁ / P₀ be the price ratio. The LP's portfolio value at t₁, if they had simply held the initial assets (HODL strategy), would be:
V_hold = s · (x₀ · P₁ + y₀)
= s · (x₀ · r·P₀ + x₀·P₀)
= s · x₀ · P₀ · (r + 1)
The LP's actual value from the pool at t₁ is:
V_pool = s · (x₁ · P₁ + y₁)
= s · (√(k/P₁) · P₁ + √(k · P₁))
= 2s · √(k · P₁)
Since k = x₀ · y₀ = x₀² · P₀:
V_pool = 2s · x₀ · P₀ · √r
The impermanent loss (IL) is:
IL(r) = (V_pool − V_hold) / V_hold = 2√r / (r + 1) − 1
This formula is symmetric in r and 1/r (price doubling is as damaging as price halving). Key values:
| Price Ratio (r) | Impermanent Loss |
|---|---|
| 1.00× (no change) | 0.00% |
| 1.25× | −0.60% |
| 1.50× | −2.02% |
| 2.00× | −5.72% |
| 4.00× | −20.0% |
| 10.0× | −42.5% |
The loss is called "impermanent" because it is only realized upon withdrawal. If prices return to P₀, the LP's position is equivalent to the HODL strategy. In practice, fees earned during the LP period partially or fully offset impermanent loss.
4. Uniswap V3: Concentrated Liquidity
4.1 Motivation
In a standard CPMM, liquidity is distributed uniformly across the entire price range (0, ∞). In practice, most trading occurs within a narrow band around the current price. Capital deployed outside this band earns no fees and represents wasted capital efficiency.
Uniswap V3 (2021) introduced concentrated liquidity: LPs can specify a price range [Pₐ, P_b] within which their liquidity is active. This dramatically increases capital efficiency at the cost of added complexity.
4.2 The Virtual Reserve Formulation
Uniswap V3 achieves concentration by introducing a virtual liquidity parameter L, defined such that the standard CPMM invariant holds for virtual reserves x̃ and ỹ:
x̃ · ỹ = L²
The virtual reserves are offset from the real reserves by amounts that pin the curve to the desired price bounds:
x̃ = x + L / √P_b
ỹ = y + L · √Pₐ
This gives the Uniswap V3 pricing curve:
(x + L/√P_b)(y + L·√Pₐ) = L²
When the current price P = y/x is within [Pₐ, P_b], the position is active and earns fees. Outside this range, the position holds 100% of one token and earns nothing.
4.3 Liquidity Parameter and Position Sizing
The liquidity parameter L governs how much of each token is deposited. For a position at current price P ∈ [Pₐ, P_b]:
Δx = L · (1/√P − 1/√P_b)
Δy = L · (√P − √Pₐ)
These formulas allow LPs to compute the exact token quantities needed to provide a specific L over a chosen range. The capital efficiency ratio — compared to V2 — is approximately:
η = 1 / (1 − √(Pₐ / P_b))
For a ±5% price range (tight band), η ≈ 10×. For a ±50% range, η ≈ 2.5×. This quantifies the leverage that concentrated liquidity provides.
4.4 The Sqrt-Price Representation
Uniswap V3 internally represents all prices as √P using a fixed-point Q64.96 number. This choice simplifies the arithmetic — the core swap formulas become:
Δ(√P) = Δy / L
Δ(1/√P) = Δx / L
The use of √P instead of P ensures that the formulas for Δx and Δy are symmetric in structure, reducing the risk of numerical asymmetries in the smart contract implementation.
5. Beyond Constant Product: The Generalized AMM Landscape
The constant product formula is one instance of a broader class. Several alternative invariants have been proposed:
Constant Sum: x + y = k. Zero slippage but can be fully drained; suitable only for pegged assets.
Constant Mean (Balancer): xʷ · y^(1−w) = k for weight w ∈ (0, 1). Generalizes CPMM to non-50/50 pools. The spot price becomes:
P = (w_y · x) / (w_x · y)
StableSwap (Curve): A hybrid that interpolates between constant sum (low slippage near peg) and constant product (resistance to full drain):
A · nⁿ · Σxᵢ + D = A · nⁿ · D + D^(n+1) / (nⁿ · Πxᵢ)
where A is the amplification coefficient, n is the number of tokens, and D is the invariant. As A → 0, this approaches constant product; as A → ∞, it approaches constant sum.
Each invariant represents a different position on the liquidity-slippage trade-off curve, optimized for different asset classes and market conditions.
6. Fee Mechanics and Arbitrage Equilibrium
6.1 Fee Integration
In practice, Uniswap V2 applies a 0.3% fee to each trade. The fee is retained in the pool, incrementally increasing k over time. Formally, for a trade of Δx with fee rate γ ∈ (0, 1):
Δy = (y · γ · Δx) / (x + γ · Δx), where γ = 1 − fee rate
For Uniswap V2's 0.3% fee, γ = 0.997. This slight modification ensures fees accrue to LPs without disrupting the core invariant logic.
6.2 Arbitrage as Price Discovery
The AMM does not fetch external prices. Instead, it relies on arbitrageurs to keep pool prices aligned with external markets. If the pool's spot price P(X/Y) = y/x deviates from the market price P*, an arbitrageur can profit by trading against the pool until the prices converge. This is the mechanism by which AMMs participate in global price discovery, and why deep pools (large k) are valuable: they resist price manipulation by requiring large capital to shift the spot price.
The equilibrium condition is:
P(X/Y)_pool = P(X/Y)_market ⟺ y/x = P*
Any deviation creates a risk-free arbitrage opportunity (net of fees and gas costs), which market participants rapidly eliminate.
7. Conclusion
The constant product market maker is a remarkable piece of applied mathematics. From the simple constraint x · y = k, an entire ecosystem of automated, trustless trading emerges. We have derived the output formula for trades, characterized spot price and slippage, and formally quantified impermanent loss as a function of price divergence. We have also shown how Uniswap V3's concentrated liquidity generalizes the classic model via virtual reserves and a liquidity parameter L, achieving dramatic capital efficiency gains within user-defined price ranges.
These constructs are not merely academic curiosities. As of 2024, Uniswap alone has processed over $2 trillion in cumulative trading volume, all governed by these invariants encoded in a few hundred lines of Solidity. The mathematics of AMMs sits at the intersection of convex optimization, mechanism design, and financial engineering — and continues to evolve with new invariant designs, oracle integrations, and layer-2 deployments.
Understanding the underlying mathematics is not optional for practitioners in this space. The formulas presented here directly determine LP profitability, trading costs, and systemic risk — making rigorous analysis both intellectually rewarding and practically essential.
References
Adams, H., Zinsmeister, N., Salem, M., Keefer, R., & Robinson, D. (2021). Uniswap v3 Core. Uniswap Labs. https://uniswap.org/whitepaper-v3.pdf
Adams, H., & Zinsmeister, N. (2020). Uniswap v2 Core. Uniswap Labs. https://uniswap.org/whitepaper.pdf
Egorov, M. (2019). StableSwap — efficient mechanism for Stablecoin liquidity. Curve Finance. https://curve.fi/files/stableswap-paper.pdf
Angeris, G., Kao, H. T., Chiang, R., Noyes, C., & Chitra, T. (2019). An analysis of Uniswap markets. arXiv:1911.03380.
Pintail. (2020). Uniswap: A Good Deal for Liquidity Providers? Medium. https://medium.com/@pintail/uniswap-a-good-deal-for-liquidity-providers-104c0b6816f2
Martinelli, F., & Mushegian, N. (2019). Balancer: A Non-Custodial Portfolio Manager, Liquidity Provider, and Price Sensor. Balancer Labs. https://balancer.fi/whitepaper.pdf
Top comments (0)