Skip to content
log(swap)

Swaps

A swap moves along the curve. Within one segment of constant active liquidity write L≡LaL \equiv L_a.

The swap law

Moving x→x+ux \to x + u exchanges

ΔQ=L u,ΔR=L(e−x−e−(x+u)),\Delta Q = L\,u, \qquad \Delta R = L\left(e^{-x} - e^{-(x+u)}\right),

closed-form in one exponential. Equivalently, for input ΔQ\Delta Q of quote, Rnew=R e−ΔQ/LR_{\text{new}} = R\,e^{-\Delta Q/L}; for input ΔR\Delta R of base, Qnew=Q−Llog⁡(1+ΔR/R)Q_{\text{new}} = Q - L\log(1 + \Delta R/R).

One exp or one log per leg. No Newton iteration, and no invariant check beyond arithmetic rounding.

Signed form

With positive = into the pool, and using u:=Δx=log⁡(p′/p)u := \Delta x = \log(p'/p) and v:=ΔR/Rv := \Delta R/R, the invariant gives one relation between them,

R′=R e−u=R(1+v)⟺u=−log⁡(1+v),v=e−u−1,R' = R\,e^{-u} = R(1+v) \qquad\Longleftrightarrow\qquad u = -\log(1+v), \quad v = e^{-u} - 1,

and both legs are explicit:

ΔQcurve=L u,ΔRcurve=R v=Lp(e−u−1).\Delta Q^{\text{curve}} = L\,u, \qquad \Delta R^{\text{curve}} = R\,v = \frac{L}{p}\big(e^{-u} - 1\big).

They carry opposite signs automatically: u>0⇒v<0u > 0 \Rightarrow v < 0, quote in, base out, price up.

The four exact cases

Fees are ϕ\phi on the quote leg, so the fee sits outside the curve — deducted from quote input, or from quote output. Let A>0A > 0 be the user-specified amount.

casesolve for uucounter-amountnew price
exactIn quote (buy base)u=(1−ϕ)ALu = \dfrac{(1-\phi)A}{L}ΔRout=Lp(1−e−u)\Delta R_{\text{out}} = \dfrac{L}{p}\big(1 - e^{-u}\big)p′=p eup' = p\,e^{u}
exactOut base (buy base)u=−log⁡(1−Ap/L)u = -\log\big(1 - Ap/L\big)ΔQin=Lu1−ϕ\Delta Q_{\text{in}} = \dfrac{Lu}{1-\phi}p′=p eup' = p\,e^{u}
exactIn base (sell base)u=−log⁡(1+Ap/L)u = -\log\big(1 + Ap/L\big)ΔQout=(1−ϕ)L∣u∣\Delta Q_{\text{out}} = (1-\phi)L\lvert u\rvertp′=p eup' = p\,e^{u}
exactOut quote (sell base)u=−A(1−ϕ)Lu = -\dfrac{A}{(1-\phi)L}ΔRin=Lp(e∣u∣−1)\Delta R_{\text{in}} = \dfrac{L}{p}\big(e^{\lvert u\rvert} - 1\big)p′=p eup' = p\,e^{u}

Every case is one exp or one log, including the exact-out directions, which on curves without a closed-form inverse require iteration. The only constraint is Ap/L<1Ap/L < 1 on exactOut base: the base leg cannot be drained, which is the R=0R = 0 asymptote restated as a swap bound.

Average execution price

The realized price is the ratio of the two legs, pˉ=∣Lu∣/∣Rv∣\bar p = \lvert Lu\rvert/\lvert Rv\rvert. Substituting v=e−u−1v = e^{-u}-1 and R=L/pR = L/p collapses all four cases to a single function of the signed displacement:

pˉp  =  g(u)  :=  u1−e−u  =  1+u2+u212−u4720+⋯\frac{\bar p}{p} \;=\; g(u) \;:=\; \frac{u}{1 - e^{-u}} \;=\; 1 + \frac{u}{2} + \frac{u^2}{12} - \frac{u^4}{720} + \cdots

the generating function of the Bernoulli numbers, with g(0)=1g(0) = 1 and g′(0)=12g'(0) = \tfrac12. It is correct on both sides: at u=−log⁡(1+v)u = -\log(1+v) it reduces to log⁡(1+v)/v<1\log(1+v)/v < 1, the discount a seller receives. Multiply by (1−ϕ)∓1(1-\phi)^{\mp 1} for the fee-inclusive price.

So slippage is exactly half the log price impact, plus a u2/12u^2/12 convexity term. At u=0.01u = 0.01: price impact e0.01−1=1.005%e^{0.01} - 1 = 1.005\%, average fill 50.0850.08 bp above spot.

Equivalently, in logs,

log⁡pexec=x+u2−u224+O(u4).\log p_{\text{exec}} = x + \frac{u}{2} - \frac{u^2}{24} + O(u^4).

The fill lands near the log-midpoint of its own move. Numerically the fraction is 0.4996 at u=0.001u = 0.001, 0.4979 at u=0.05u = 0.05, and still 0.459 at a move of 1.0. This is not a curiosity: it is what fixes the memory of the volatility estimator at α=12\alpha = \tfrac12.

Price impact does not depend on price

Δx=ΔQLexactly, with no p on either side.\Delta x = \frac{\Delta Q}{L} \qquad\text{exactly, with no } p \text{ on either side.}

A quote-denominated trade moves xx by ΔQ/L\Delta Q/L wherever price sits. This is scale-covariance in trader-facing terms, and it fails for every non-log VV: in Uniswap v2 quote input is linear in p\sqrt p, so dp/p∝p−1/2dQdp/p \propto p^{-1/2}dQ and liquidity silently rescales as price moves.

Consequences: moving price 1% costs Llog⁡(1.01)≈0.00995 LL\log(1.01) \approx 0.00995\,L of quote, and one basis point of impact costs 10−4L10^{-4}L. LL is the linear liquidity coefficient in quote units — the same number already shown to the LP as base exposure.

Crossing floors: the sum is still linear

RtotalR_{\text{total}} and QtotalQ_{\text{total}} are continuous across a floor crossing — a dormant position holds Ri=Li/pmin⁡,iR_i = L_i/p_{\min,i}, which equals Li/pL_i/p exactly at activation, with Qi=0Q_i = 0 — so only the slope LaL_a jumps. Over segments jj with endpoints (xj−1,xj)(x_{j-1}, x_j):

ΔQ=∑jLj (xj−xj−1),ΔR=∑jLj(1pj−1−1pj).\Delta Q = \sum_j L_j\,(x_j - x_{j-1}), \qquad \Delta R = \sum_j L_j\left(\frac{1}{p_{j-1}} - \frac{1}{p_j}\right).

Both are exactly linear in the liquidity vector, with coefficients depending only on segment endpoints. Three consequences:

  1. The quote leg is the area under the LaL_a step function over [x0,x1][x_0, x_1] — piecewise linear in xx, so exact-in on the quote side is a piecewise-linear inversion. No search, no iteration.
  2. Exact-in on the base side inverts segment-wise in closed form: 1/pj=1/pj−1−ΔR/Lj1/p_j = 1/p_{j-1} - \Delta R/L_j.
  3. Fee attribution is free. Class jj's share of the quote leg is LjΔxjL_j\Delta x_j, which is exactly the LjΔFL_j\Delta F the accumulator would compute. Fee accounting is not a separate layer — it is the same sum the swap already evaluates.