--- title: AMM Slippage & IL — Variables, Prices, r, and Slippage tags: AMM, DeFi, Slippage, Impermanent Loss, Uniswap v2 --- [TOC] # Contents Compact mathematical reference for: - **AMM state variables** - **pool reserves and prices** - **constant-product relations** - **price-move factor** $r$ and **slippage formulas** (with/without fees), # Variables & Notation ## AMM state (constant-product, Uniswap v2-style) - $X$ — reserve of token **A** in the pool. - $Y$ — reserve of token **B** in the pool. - $k$ — invariant: $k = X\cdot Y$ - **Price** of token A in units of token B: $P = \dfrac{Y}{X}$ ## Initial \& Final States - Initial pool state (pre-trade, time $t = 0$): $$\left(X_0, \ Y_0, \ P_0=\dfrac{Y_0}{X_0} \right) \ \mathrm{with} \ \ k=X_0\cdot Y_0$$ - Final pool state (post-trade, time $t = 1$): $$ \left(X_1, \ Y_1, \ P_1=\dfrac{Y_1}{X_1}\right) \ \mathrm{with} \ k = X_1\cdot Y_1 = X_0\cdot Y_0$$ - **Price ratio** and spot return: $$ r =\frac{P_1}{P_0} \ \ \ (\mathrm{spot \ return} = r-1) $$ Note that $r-1$ is also the **average** price change along AMM curve for an **infinitessimally** small buy quantity. --- # Constant-Product Relations ## Reserves at target price $P_1$ Using $k=X_0Y_0 = X_1Y_1$ and $P_1=\dfrac{Y_1}{X_1}$ $$ X_1=\sqrt{\frac{k}{P_1}}=\frac{X_0}{\sqrt{r}} \qquad Y_1=\sqrt{k\,P_1}=Y_0\sqrt{r} \qquad r = \frac{P_1}{P_0} $$ ## Net token movements **Buy A** (add B, remove A from pool): $$ \Delta A = X_0 - X_1 = X_0\!\left(1-\frac{1}{\sqrt{r}}\right),\qquad \Delta B = Y_1 - Y_0 = Y_0(\sqrt{r}-1). $$ With **input fee** $\tau_{in}$, if trader **spends** $B_{\text{spent}}$: $$ \Delta B \;=\; (1-\tau_{in})\, B_{\text{spent}}\,. $$ Input fee is a fee taken from what the trader sends into the pool before it touches the reserves. Therefore, $\tau_{in}$ is the fraction of the input that does not reach the reserves (goes to protocol, LP, creator). On the other hand, the output fee, $\tau_{out}$ is a fee taken from what the trader receives from the pool after the on-curve output has been computed (going to creator, protocol). It does not affect the reserve, since $\Delta A$ is fixed by the invariant once $\Delta B$ is realized. ## Inverse Relations (see appendix at the end for derivations) The following formulas recover $r = P_1/P_0$ from a fill. For **net quote** added (**buy** token **A** with token **B**, adds **B** to the pool): $$r=\left(1+\frac{\Delta B} {Y_0}\right)^{\!2}$$ From **net base** removed (**buy** token **A** with token **B**, removes **A** from the pool): $$r=\left(1-\frac{\Delta A}{X_0}\right)^{\!-2}$$ Note: when selling token **A**, $\Delta B < 0$ and $\Delta A > 0$ (since $\Delta A = X_0 - X_1$), and the same formulas hold with appropriate sign changes. In the presense of fees, we need to adjust the formula for $r$. Specifically, the input amount (quote) that enters the pool, $\Delta B = Y_1 - Y_0$, **includes** LP fee. However, the output amount (base) should exclude the LP fee to walk the AMM curve: $$\Delta B_{eff} = (1 - f_{LP})\Delta B$$ since LP fee is added to the input reserve, but does not entitle extra output. Therefore, $X_1 = k/(Y_0 + \Delta B_{eff})$, and taking $P_1 = Y_1/X_1$ and $P_0 = Y_0/X_0$ we get: $$ r = \frac{P_1}{P_0} = \left(1+\frac{\Delta B}{Y_0}\right)\cdot \left(1 + \frac{\Delta B_{eff}}{Y_0}\right) $$ --- # Slippage on the AMM Curve Note that the final spot price differs from the average execution price paid along the AMM curve. ## Average execution slippage (no fees) Average paid price for a buy over the path is the **geometric mean** of starting and ending prices (see exact derivation in the appendix at the end): $$ P_{\text{avg}}=\sqrt{P_0 P_1}=P_0\sqrt{r}. $$ Price-relative average execution slippage for **buy**: $$ \Sigma= \frac{P_{\text{avg}}}{P_0}-1=\sqrt{r}-1 = \frac{\Delta B}{Y_0} $$ For sell, simply reverse the sign $\Sigma \rightarrow -\Sigma$ ## Average execution slippage (with input fees) If fees are charged on input before hitting the curve, the **price-plus-fee uplift** (buy) is: $$ \frac{P_{\text{avg}}}{P_0}\cdot\frac{1}{\,1-\tau\,}-1 \;=\; \frac{\sqrt{r}}{\,1-\tau\,}-1 $$ Rule of thumb for **cost per \$ traded** for **buy** (expanding above in Taylor series): $$ \Gamma \approx\; (\sqrt{r}-1) \;+\; \tau $$ With LP fee $f$ retained in the pool we have (for small trades): $$ \sqrt{r} - 1 \approx \frac{\Delta B + \Delta B_{eff}}{2Y_0} \approx \left(1-\frac{f}{2}\right)\frac{\Delta B}{Y_0} $$ Note that LP fee increases the constant $k$ in the constant product formula: $$ k' = k\cdot \left(1+\frac{\Delta B}{Y_0}\right)\left(1+\frac{\Delta B_{eff}}{Y_0}\right) > k $$ For small trades: $$ \frac{k'}{k} \approx 1 + f\cdot \frac{\Delta B}{Y_0} $$ --- # Impermanent Loss (see derivation in appendix) For a 50/50 constant-product pool (ignoring fees) and price ratio $r=P_1/P_0$: $$ \boxed{\;\text{IL}(r)\;=\;\frac{2\sqrt{r}}{1+r}-1\;} $$ Note: negative for $r\neq 1$. Fees earned can offset IL if volume is sufficient. --- # Appendix — AMM mathematics ### Recovering $r$ from **net quote added** $\Delta B$ (buy $A$ with $B$) - Net change in B reserves: $Y_1 = Y_0 + \Delta B$ - Constant product: $X_1 = \dfrac{k}{Y_1} = \dfrac{X_0Y_0}{Y_0 + \Delta B}$ Substitution yields: $$ \begin{aligned} r \;=\; \frac{P_1}{P_0} &= \frac{Y_1/X_1}{Y_0/X_0} = \frac{Y_1 X_0}{X_1 Y_0} = \frac{Y_1 X_0}{\big(\frac{X_0Y_0}{Y_1}\big) Y_0} = \frac{Y_1^2}{Y_0^2} = \left(\frac{Y_0+\Delta B}{Y_0}\right)^2 = \left(1+\frac{\Delta B}{Y_0}\right)^{\!2} \end{aligned} $$ - When **selling** $A$ for $B$, $\Delta B<0$, implying $r<1$. --- ### Recovering $r$ from **net base removed** $\Delta A$ (buy $A$ with $B$ ) Use the **“removed from pool”** sign convention: $$ \Delta A \;\equiv\; X_0 - X_1 \quad\Rightarrow\quad \begin{cases} \Delta A > 0 & \text{buy (A leaves pool)}\\ \Delta A < 0 & \text{sell (A enters pool)} \end{cases} $$ - $X_1 = X_0 - \Delta A$ - $Y_1 = \dfrac{k}{X_1} = \dfrac{X_0Y_0}{X_0-\Delta A}$ Substitution yields: $$ \begin{aligned} r \;=\; \frac{Y_1/X_1}{Y_0/X_0} &= \frac{Y_1 X_0}{X_1 Y_0} = \frac{\big(\frac{X_0Y_0}{X_0-\Delta A}\big) X_0}{(X_0-\Delta A) Y_0} = \frac{X_0^2}{(X_0-\Delta A)^2} = \left(1-\frac{\Delta A}{X_0}\right)^{-2} \end{aligned} $$ - When **selling**, $\Delta A<0$ so the denominator is greater than 1 and $r<1$. ### Average execution price over path derivation **Spot price along the path** Since $k=XY$ is constant, $$ Y=\frac{k}{X}\;\;\Rightarrow\;\;P(X)=\frac{Y}{X}=\frac{k}{X^2}. $$ **Infinitesimal trade relation** Removing an infinitesimal $dA>0$ from the pool corresponds to $dX=-dA$. Keeping $k$ constant: $$ (X-dX)(Y+dB)=XY \;\Rightarrow\; -\,Y\,dX + X\,dB = 0 \;\Rightarrow\; dB=\frac{Y}{X}\,dA=P(X)\,dA. $$ **Integrate totals** $$ \Delta A=\int dA = X_0 - X_1 $$ $$ \Delta B=\int dB=\int_{X_0}^{X_1}\frac{k}{X^2}(-dX) = -k\!\int_{X_0}^{X_1}\frac{dX}{X^2} = k\!\left(\frac{1}{X_1}-\frac{1}{X_0}\right). $$ **Average execution price** $$ P_{\text{avg}}=\frac{\Delta B}{\Delta A} = \frac{k\left(\frac{1}{X_1}-\frac{1}{X_0}\right)}{X_0 - X_1}. $$ Using $k=X_0Y_0=X_0^2P_0$: $$ P_{\text{avg}} = \frac{X_0^2P_0\left(\frac{X_0-X_1}{X_0X_1}\right)}{X_0-X_1} = P_0\,\frac{X_0}{X_1}. $$ Also, from $P=\dfrac{k}{X^2}$: $$ P_1=\frac{k}{X_1^2}=\frac{X_0^2P_0}{X_1^2} \rightarrow\; \frac{X_0}{X_1}=\sqrt{\frac{P_1}{P_0}}. $$ Therefore, $$ \boxed{\,P_{\text{avg}}=P_0\sqrt{\frac{P_1}{P_0}}=\sqrt{P_0P_1}\, } $$ **Interpretation** The average paid price over a constant-product path equals the **geometric mean** of the start and end spot prices. Hence the (fee-excluded) average execution slippage for a buy is $$ \frac{P_{\text{avg}}}{P_0}-1=\sqrt{\frac{P_1}{P_0}}-1=\sqrt{r}-1,\quad r=\frac{P_1}{P_0}. $$ ### Impermanent loss formula derivation **Goal.** Show that for a price move factor $r = \dfrac{P_1}{P_0}$ (price of A in units of B), an LP’s impermanent loss relative to just holding is: $$ \boxed{\;\text{IL}(r)\;=\;\frac{2\sqrt{r}}{1+r}\;-\;1\;} $$ Assume that LP owns $0 < s \le 1$ fraction of the pool, and consistent with V2, 50/50 deposit at entry is mandated. Using $r = P_1/P_0$, $k=X_0Y_0$ and $P_1 = Y_1/X_1$ we get: $$ X_1 = \sqrt{\frac{k}{P_1}} = \frac{X_0}{\sqrt{r}}, \qquad Y_1 \;=\; \sqrt{k\,P_1} \;=\; Y_0\,\sqrt{r}. $$ **LP value vs holding value at $P_1$:** Take **B** as the numeraire (values in units of token B). - **LP value at exit** (holding a fraction $s$ of pool): $$ V_{\text{LP}}(P_1) = s\Big(X_1\,P_1 + Y_1\Big). $$ - **Holding value at $P_1$** (if the LP had simply kept their entry amounts $sX_0,\,sY_0$): $$ V_{H}(P_1) = s\Big(X_0\,P_1 + Y_0\Big). $$ Simplify $V_{\text{LP}}$ using $P_1 = rP_0$ and $P_0=Y_0/X_0$, we compute the term $X_1 P_1$: $$ X_1 P_1 = \left(\frac{X_0}{\sqrt{r}}\right)\!\cdot\!P_1 = \left(\frac{X_0}{\sqrt{r}}\right)\!\cdot\!(r P_0) = \left(\frac{X_0}{\sqrt{r}}\right)\!\cdot\!\left(r \frac{Y_0}{X_0}\right) = \sqrt{r}\,Y_0. $$ Hence $$ V_{\text{LP}}(P_1) = s\Big(\underbrace{\sqrt{r}\,Y_0}_{X_1P_1} + \underbrace{Y_0\sqrt{r}}_{Y_1}\Big) = s\cdot 2Y_0\sqrt{r}. $$ Similarly, the holding value: $$ V_{H}(P_1) = s\Big(X_0\,P_1 + Y_0\Big) = s\Big(X_0(rP_0) + Y_0\Big) = s\Big(rY_0 + Y_0\Big) = s\cdot Y_0(1+r). $$ **Impermanent Loss as a relative shortfall:** By definition (relative performance of LP vs holding): $$ \text{IL}(r) = \frac{V_{\text{LP}}(P_1)}{V_{H}(P_1)} - 1 = \frac{s\cdot 2Y_0\sqrt{r}}{s\cdot Y_0(1+r)} - 1 = \frac{2\sqrt{r}}{1+r} - 1. $$ This completes the derivation. **Properties of IL:** - $\text{IL}(1)=0$ (no price change → no divergence). - Symmetry: $\text{IL}(r)=\text{IL}(1/r)$. - Always non-positive for $r\neq 1$ (LP underperforms holding on pure price divergence). - Fees accrued from trading can offset IL in practice, but are **not** part of the formula above.