---
# System prepended metadata

title: Uniswap V2 - Arbitrage (hyperbolic)
tags: [uniswap2, arbitrage, crypto]

---

# Uniswap V2 - Arbitrage (hyperbolic)

###### tags: `crypto`, `uniswap2`, `arbitrage`

In Uniswap V2 the reserves (amount of tokens secured by the protocol) has the following functional dependency:

$$XY=k\qquad k\text{ - const}$$

Let's say that $qx$ and $y$ are differences of the protocol's reserves is scope of a swap, so that the resulting reserves after the swap are:

$$X_1=X_0+qx\qquad Y_1=Y_0-y$$

Where $q$ is the expression of the protocol fee. For Uniswap V2 the fee is constant and equals to 0.3%, so it's $q$ is:

$$q=(1-fee)=0.997$$

The signs of $+qx$ and $-y$ indicates that asset $X$ is exchanged for the asset $Y$, and multiplier $q$ is applied to the swap's **input** (the amount of asset sold to the protocol). The swap's **output** (the amount of asset released in exchange for the **input**) is calculated using the following equation:

$$X_1Y_1=X_0Y_0\qquad\Rightarrow\qquad (X_0+qx)(Y_0-y)=X_0Y_0$$

<br>
<br>

The hyperbolic dependency between reserves is encoded within the protocol's internals. In such case the convinient choice is to use the [hyperbolic coordinates](https://en.wikipedia.org/wiki/Hyperbolic_coordinates), where coordinates are expressed as:

$$X=ve^u\qquad Y=ve^{-u}\\
v^2=XY\qquad 2u=\ln{\frac{X}{Y}}$$

In hyperbolic space, the coordinate $v$ is the [geometric mean](https://en.wikipedia.org/wiki/Geometric_mean), showed on a graph with red hyperbolas. It represents the ratio of protcol's reserves and is constant for each particular protocol. The $u$ coordinate (blue lines on the graph) is the [hyperbolic angle](https://en.wikipedia.org/wiki/Hyperbolic_angle), shows at which current ratio of the protocol reserves.

<iframe src="https://www.desmos.com/calculator/1pw0synsrv?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*Protocol state in hyperbolic coordinates, red - is a constant per each protocol, and blue shows the current exchange rate.*

<br>
<br>

The swap procedure is determined by it's initial position (asset reserves before the exchange) and the resulting position (asset reserves after the exchange).

<iframe src="https://www.desmos.com/calculator/lgfzii7wcr?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*A swap procedure in hyperbolic coordinates*

Such transition expressed in hyperbolic coordinates looks as:

$$U_0 \rightarrow U_1\qquad V_0 = V_1 - \text{const}$$

And the dependency between the **input** and the possition offest can be expressed by translating the reserves changes into $u, v$ space:

$$qx=X_1-X_0=ve^{U_1}-ve^{U_0}\\
y=Y_0-Y_1=ve^{-U_0}-ve^{-U_1}$$

Here the value of $U_1$ depends on the **input**, let's show this explicitly by resolving the top equation for x:

$$ve^{U_1}-ve^{U_0}=qx\\
e^{U_0}(e^{U_1-U_0}-1)=\frac{q}{v}x\\
e^{U_1-U_0}=\frac{q}{v}e^{-U_0}x+1\\
U_1-U_0=\ln(\frac{q}{v}e^{-U_0}x+1)\\
U_1=\ln(\frac{qe^{-U_0}}{v}x+1)+U_0$$

In general this dependency has the following form:

$$u(x)=\ln(\frac{q}{v}e^{-U_0}x+1)+U_0\\
v=\sqrt{X_0Y_0}=\sqrt{k}\qquad U_0=\frac{1}{2}\ln{\frac{X_0}{Y_0}}$$

<iframe src="https://www.desmos.com/calculator/us5ytu9mw0?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*The $u(x)$ dependency in $u, x$ space is a logarithmic infinitely growing function, with $u(0)=U_0$*

<br>
<br>

The dependency of the **output** from **input** (the **exchange equation**) in hyperbolic coordinates has the following form:

$$y(u,v)=Y_0-Y_1=ve^{-U_0}-ve^{-u(x)}$$

<iframe src="https://www.desmos.com/calculator/hyj2xlut8i?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*The **output** function $y(x)$ expressed with hyperbolic transition. The **output** value asymptotically approaches the $Y_0$ with $x\rightarrow \infty$*

<details>
<summary>Check</summary>

Considering that **output** in regular coordinates is:

$$y=qY_0x/(X_0+qx)$$

What is a direct consequence of $(X_0+qx)(Y_0-y)=X_0Y_0$. Let's make sure that one form can be derived from another:

$$u(x)=ln(\frac{q}{ve^{U_0}}x+1)+U_0=ln(\frac{qx}{X_0}+1)+U_0$$

$$e^{u(x)}=\frac{qx+X_0}{X_0}e^{U_0}=\frac{qx+X_0}{X_0}\sqrt{\frac{X_0}{Y_0}}=\frac{qx+X_0}{\sqrt{X_0Y_0}}$$

$$y=ve^{-U_0}-ve^{-u(x)}=Y_0-\sqrt{X_0Y_0}\frac{\sqrt{X_0Y_0}}{qx+X_0}=\\
=Y_0-\frac{X_0Y_0}{qx+X_0}=\frac{qY_0x}{X_0+qx}$$
</details>

<br>
<br>

## Arbitrage definition

Having two protocols with the following parameters:

$$u=\ln{\sqrt{\frac{X_0}{Y_0}}}\qquad v=\sqrt{X_0Y_0}\\
\phi=\ln{\sqrt{\frac{X_0'}{Y_0'}}}\qquad w=\sqrt{X_0'Y_0'}$$

The arbitrage procedure can be described as two subsequential swaps, described with the following **exchange equations**:

$$(X_0+qx)(Y_0-y)=X_0Y_0\\
(X_0'-x')(Y_0'+qy)=X_0'Y_0'$$

Let's express the **exchange equation** of the second protocol in $\phi, w$ space:

$$x'=X_0'-X_1'=we^{\phi_0}-we^{\phi_1}\\
qy=Y_1'-Y_0'=we^{-\phi_1}-we^{-\phi_0}$$

Now, considering the $y(u,v)$ function from the first swap:

$$we^{-\phi_1}=qy+we^{-\phi_0}=qve^{-U_0}-qve^{-u}+we^{-\phi_0}\\
e^{-\phi_1}=\frac{1}{w}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})\\
e^{\phi_1}=w(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}$$

$$x'=we^{\phi_0}-w^2(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}$$

<iframe src="https://www.desmos.com/calculator/bof3elllxs?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*The $x'(u)$ dependency. Approaches $we^{\phi_0}$ when $u\rightarrow \infty$. Undefined in the point with coordinate $u=\ln(\frac{q\sqrt{X_{0}Y_{0}}}{qY_{0}+Y_0'})$ as suffers the gap.*

<details>
<summary>Check</summary>

Let's check if the $x'(u)$ function can be converted into $x'(y)$:

$$e^{u(x)}=\frac{qx+X_0}{X_0}e^{U_0}=\frac{qx+X_0}{X_0}\sqrt{\frac{X_0}{Y_0}}=\frac{qx+X_0}{\sqrt{X_0Y_0}}$$

$$x'=we^{\phi_0}-w^2(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}=\\
=X_0'-X_0'Y_0'(qY_0+Y_0'-q\sqrt{X_0Y_0}e^{-u(x)})^{-1}=\\
=X_0'-X_0'Y_0'(qY_0+Y_0'-q\sqrt{X_0Y_0}\frac{\sqrt{X_0Y_0}}{qx+X_0})^{-1}=\\
=X_0'-\frac{X_0'Y_0'(X_0+qx)}{(qY_0'+Y_0')(X_0+qx)-qX_0Y_0}=\\
=X_0'-\frac{X_0'Y_0'(X_0+qx)}{Y_0'(X_0+qx)+q^2Y_0x}=\\
=X_0'-\frac{X_0'Y_0'}{Y_0'+q(qY_0x/(X_0+qx))}=\\
=X_0'-\frac{X_0'Y_0'}{Y_0'+qy}\\
(X_0'-x')=\frac{X_0'Y_0'}{Y_0'+qy}$$
</details>

<br>
<br>

By definition the **margin** is a difference between amount of asset received in result of the sell operation (the second swap **output**) and the amount of asset utilized to perform the buy operation (the first swap **input**), considering that:

$$M(x)=x'-x$$

Considering that $x$ is:

$$ve^u-ve^{U_0}=qx\qquad\Rightarrow\qquad x=\frac{v}{q}e^u-\frac{v}{q}e^{U_0}$$

$$M(u,v)=we^{\phi_0}-w^2(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}-\frac{v}{q}e^u+\frac{v}{q}e^{U_0}$$

<iframe src="https://www.desmos.com/calculator/clyn3eg71j?embed" width="720" height="360" style="border: 1px solid #ccc" frameborder=0></iframe>

*Margin as a function of **input** $M(u(x),v,\phi,w)$ has a distinguishable range of positive values and the maximum.*

<details>
<summary>Expressing the margin maximum and the optimal input</summary>

The maximum value of the margin $\hat{M}$ as well as the optimal **input** $\hat{x}$ can be found by finding the solutions of the following equation:

$$\frac{dM}{dx}=0$$

Let's express the derivative of margin function in $u,v$ space:

$$\frac{dM}{du}=-w^2\frac{d}{du}((qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1})-\frac{v}{q}e^u=\\
=-w^2(-1)(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-2}\frac{d}{du}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})-\frac{v}{q}e^u=\\
=w^2(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-2}(-(-1)qve^{-u})-\frac{v}{q}e^u=\\
=qw^2ve^{-u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-2}-\frac{v}{q}e^{u}=\\
=\frac{v}{q}e^{u}(q^2w^2e^{-2u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-2}-1)=\\
=\frac{v}{q}e^{u}(qwe^{-u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}-1)(qwe^{-u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}+1)$$

The equation $\frac{dM}{du}=0$ has three solutions in general:

$$u_0=-\infty$$

<br>

$$qwe^{-u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}-1=0\\
qwe^{-u}-qve^{-U_0}+qve^{-u}-we^{-\phi_0}=0\\
q(w+v)e^{-u}=qve^{-U_0}+we^{-\phi_0}\\
e^{-u}=\frac{qve^{-U_0}+we^{-\phi_0}}{q(w+v)}\\
u_1=\ln{\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}}$$

<br>

$$qwe^{-u}(qve^{-U_0}-qve^{-u}+we^{-\phi_0})^{-1}+1=0\\
qwe^{-u}+qve^{-U_0}-qve^{-u}+we^{-\phi_0}=0\\
q(w-v)e^{-u}=-qve^{-U_0}-we^{-\phi_0}\\
e^{-u}=\frac{qve^{-U_0}+we^{-\phi_0}}{q(v-w)}\\
u_2=\ln{\frac{q(v-w)}{qve^{-U_0}+we^{-\phi_0}}}$$

Considering that $u(x)$ is:

$$u(x)=\ln(\frac{q}{v}e^{-U_0}x+1)+U_0$$

The first solution is:

$$u_0=\ln(\frac{q}{v}e^{-U_0}x_0+1)+U_0=-\infty\\
\ln(\frac{q}{v}e^{-U_0}x_0+1)=-\infty\\
\frac{q}{v}e^{-U_0}x_0+1=0\\
x_0=-\frac{ve^{U_0}}{q}=-\frac{X}{q}$$

Taking into account that operating with negative **input** is impossible ($x > 0$), this solution is not applicable and will be considered no longer.

<br>

The second solution:

$$u_1=\ln{\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}}=\ln(\frac{q}{v}e^{-U_0}x_1+1)+U_0\\
e^{\ln{\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}}}=e^{\ln(\frac{q}{v}e^{-U_0}x_1+1)+U_0}\\
\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}=e^{U_0}(\frac{q}{v}e^{-U_0}x_1+1)\\
q(w+v)=(\frac{q}{v}x_1+e^{U_0})(qve^{-U_0}+we^{-\phi_0})\\
q(w+v)=\frac{q}{v}x_1(qve^{-U_0}+we^{-\phi_0})+e^{U_0}(qve^{-U_0}+we^{-\phi_0})\\
\frac{q}{v}x_1(qve^{-U_0}+we^{-\phi_0})=q(w+v)-(qv+we^{U_0-\phi_0})\\
\frac{q}{v}x_1(qve^{-U_0}+we^{-\phi_0})=qw-we^{U_0-\phi_0}\\
x_1=\frac{qvw-ve^{U_0}we^{-\phi_0}}{q^2ve^{-U_0}+qwe^{-\phi_0}}$$

$$x_1=\frac{q\sqrt{X_0'Y_0'X_0Y_0}-Y_0'X_0}{q^2Y_0+qY_0'}$$

Is only applicable if:

$$q\sqrt{X_0'Y_0'X_0Y_0}-Y_0'X_0\ge0\\
q\sqrt{X_0'Y_0}\ge\sqrt{Y_0'X_0}$$

<br>

And the third solution is:

$$u_2=\ln{\frac{q(v-w)}{qve^{-U_0}+we^{-\phi_0}}}=\ln(\frac{q}{v}e^{-U_0}x_2+1)+U_0\\
\frac{q(v-w)}{qve^{-U_0}+we^{-\phi_0}}=e^{U_0}(\frac{q}{v}e^{-U_0}x_2+1)\\
\frac{q(v-w)}{qve^{-U_0}+we^{-\phi_0}}=\frac{q}{v}x_2+e^{U_0}\\
x_2=\frac{qv(v-w)-ve^{U_0}(qve^{-U_0}+we^{-\phi_0})}{q^2ve^{-U_0}+qwe^{-\phi_0}}\\
x_2=\frac{qv^2-qvw-qv^2-ve^{U_0}we^{-\phi_0}}{q^2ve^{-U_0}+qwe^{-\phi_0}}\\
x_2=\frac{-q\sqrt{X_0'Y_0'X_0Y_0}-Y_0'X_0}{q^2Y_0+qY_0'}$$

This solution is not applicable as well. And the final expression for the optimal **input** is:

$$\hat{x}=\frac{q\sqrt{X_0'Y_0'X_0Y_0}-Y_0'X_0}{q^2Y_0+qY_0'}\\
q\sqrt{X_0'Y_0}\ge\sqrt{Y_0'X_0}$$

Let's express the maximum margin:

$$\hat{M}=we^{\phi_0}-w^2(qve^{-U_0}-qve^{-\hat{u}}+we^{-\phi_0})^{-1}-\frac{v}{q}e^\hat{u}+\frac{v}{q}e^{U_0}$$

$$\hat{u}=\ln{\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}}$$

$$qve^{-\hat{u}}=\frac{qv^2e^{-U_0}+wve^{-\phi_0}}{w+v}$$

$$-w^2(qve^{-U_0}-qve^{-\hat{u}}+we^{-\phi_0})^{-1}=\\
=w^2(qve^{-U_0}+we^{-\phi_0}-\frac{qv^2e^{-U_0}+wve^{-\phi_0}}{w+v})^{-1}=\\
=w^2(\frac{qwve^{-U_0}+qv^2e^{-U_0}+w^2e^{-\phi_0}+wve^{-\phi_0}-qv^2e^{-U_0}-wve^{-\phi_0}}{w+v})^{-1}=\\
=w^2\frac{w+v}{qwve^{-U_0}+w^2e^{-\phi_0}}=\frac{w^2+wv}{qve^{-U_0}+we^{-\phi_0}}$$

$$\frac{v}{q}e^\hat{u}=\frac{v}{q}\frac{q(w+v)}{qve^{-U_0}+we^{-\phi_0}}=\frac{wv+v^2}{qve^{-U_0}+we^{-\phi_0}}$$

$$\hat{M}=we^{\phi_0}-\frac{w^2+wv}{qve^{-U_0}+we^{-\phi_0}}-\frac{wv+v^2}{qve^{-U_0}+we^{-\phi_0}}+\frac{ve^{U_0}}{q}=\\
=we^{\phi_0}+\frac{ve^{U_0}}{q}-\frac{(w+v)^2}{qve^{-U_0}+we^{-\phi_0}}=\\
=X_0'+X_0/q-\frac{(\sqrt{X_0'Y_0'}+\sqrt{X_0Y_0})^2}{qY_0+Y_0'}=\\
=\frac{qX_0'Y_0+X_0'Y_0'+X_0Y_0+Y_0'X_0/q-X_0'Y_0'-X_0Y_0-2\sqrt{X_0'Y_0'X_0Y_0}}{qY_0+Y_0'}=\\
=\frac{qX_0'Y_0+Y_0'X_0/q-2\sqrt{X_0'Y_0'X_0Y_0}}{qY_0+Y_0'}=\\
=\frac{q^2X_0'Y_0-2q\sqrt{X_0'Y_0'X_0Y_0}+Y_0'X_0}{q^2Y_0+qY_0'}=\\
=\frac{(q\sqrt{X_0'Y_0}-\sqrt{Y_0'X_0})^2}{q^2Y_0+qY_0'}$$
</details>

<br>

The maximum **margin** and the optimal **input** are:

$$q\sqrt{X_0'Y_0}\ge\sqrt{Y_0'X_0}$$

$$\hat{M}=\frac{(q\sqrt{X_0'Y_0}-\sqrt{Y_0'X_0})^2}{q^2Y_0+qY_0'}\qquad
\hat{x}=\frac{\sqrt{Y_0'X_0}(q\sqrt{X_0'Y_0}-\sqrt{Y_0'X_0})}{q^2Y_0+qY_0'}$$

<br>

In conclusion let's consolidate all from above. It is quite easy to calculate the possible outcome of the arbitrage operation having data of current balances of two Uniswap V2 protocols. In case if $q\sqrt{X_0'Y_0}\lt\sqrt{Y_0'X_0}$ the margin is completely unavailable and the calculation may be skipped completely.

The value $\hat{M}$ is the maximum margin available for an arbitrage between the two protocols with reserves $X_0$, $Y_0$ and $Y_0'$, $X_0'$, when the exchange is performed as from $X\rightarrow Y$ assets in the first protocol and $Y'\rightarrow X'$ assets in the second protocol. The maximum margin can only be achieve with the **input** value equal to $\hat{x}$.

<br>
<br>
