A swap moves along the curve. Within one segment of constant active liquidity write
L≡La.
The swap law
Moving x→x+u exchanges
ΔQ=Lu,ΔR=L(e−x−e−(x+u)),
closed-form in one exponential. Equivalently, for input ΔQ of quote,
Rnew=Re−ΔQ/L; for input ΔR of base,
Qnew=Q−Llog(1+Δ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) and v:=ΔR/R, the
invariant gives one relation between them,
R′=Re−u=R(1+v)⟺u=−log(1+v),v=e−u−1,
and both legs are explicit:
ΔQcurve=Lu,ΔRcurve=Rv=pL(e−u−1).
They carry opposite signs automatically: u>0⇒v<0, quote in, base out, price up.
The four exact cases
Fees are ϕ on the quote leg, so the fee sits outside the curve — deducted from quote input, or
from quote output. Let A>0 be the user-specified amount.
case
solve for u
counter-amount
new price
exactIn quote (buy base)
u=L(1−ϕ)A
ΔRout=pL(1−e−u)
p′=peu
exactOut base (buy base)
u=−log(1−Ap/L)
ΔQin=1−ϕLu
p′=peu
exactIn base (sell base)
u=−log(1+Ap/L)
ΔQout=(1−ϕ)L∣u∣
p′=peu
exactOut quote (sell base)
u=−(1−ϕ)LA
ΔRin=pL(e∣u∣−1)
p′=peu
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<1 on exactOut base: the base
leg cannot be drained, which is the R=0 asymptote restated as a swap bound.
Average execution price
The realized price is the ratio of the two legs, pˉ=∣Lu∣/∣Rv∣.
Substituting v=e−u−1 and R=L/p collapses all four cases to a single function of the signed
displacement:
ppˉ=g(u):=1−e−uu=1+2u+12u2−720u4+⋯
the generating function of the Bernoulli numbers, with g(0)=1 and g′(0)=21. It is
correct on both sides: at u=−log(1+v) it reduces to log(1+v)/v<1, the discount a seller
receives. Multiply by (1−ϕ)∓1 for the fee-inclusive price.
So slippage is exactly half the log price impact, plus a u2/12 convexity term. At u=0.01:
price impact e0.01−1=1.005%, average fill 50.08 bp above spot.
Equivalently, in logs,
logpexec=x+2u−24u2+O(u4).
The fill lands near the log-midpoint of its own move. Numerically the fraction is 0.4996 at
u=0.001, 0.4979 at u=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
α=21.
Price impact does not depend on price
Δx=LΔQexactly, with no p on either side.
A quote-denominated trade moves x by ΔQ/L wherever price sits. This is
scale-covariance in trader-facing terms, and it fails for
every non-log V: in Uniswap v2 quote input is linear in p, so dp/p∝p−1/2dQ and
liquidity silently rescales as price moves.
Consequences: moving price 1% costs Llog(1.01)≈0.00995L of quote, and one basis point of
impact costs 10−4L. L 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
Rtotal and Qtotal are continuous across a floor crossing — a dormant position
holds Ri=Li/pmin,i, which equals Li/p exactly at activation, with Qi=0 — so only
the slopeLa jumps. Over segments j with endpoints (xj−1,xj):
ΔQ=j∑Lj(xj−xj−1),ΔR=j∑Lj(pj−11−pj1).
Both are exactly linear in the liquidity vector, with coefficients depending only on segment
endpoints. Three consequences:
The quote leg is the area under the La step function over [x0,x1] — piecewise linear
in x, so exact-in on the quote side is a piecewise-linear inversion. No search, no iteration.
Exact-in on the base side inverts segment-wise in closed form:
1/pj=1/pj−1−ΔR/Lj.
Fee attribution is free. Class j's share of the quote leg is LjΔxj, which is
exactly the LjΔF the accumulator would compute. Fee accounting is not a separate layer —
it is the same sum the swap already evaluates.