Uniswap V2 + V3 Volatile Losses

In this article I will tell you what it is Uniswap Impermanent Lossand why it is such a hot topic for liquidity providers.

More specifically, what awaits you in the article:

  • A quick dive into how Uniswap V2 works

  • Impermanent Loss for Uniswap V2

  • A Deep Dive into How Uniswap V3 Works

  • Impermanent Loss for Uniswap V3

  • Problems of liquidity providers or why this topic is very important for them

A quick dive into how Uniswap V2 works

Uniswap is a decentralized exchange where you can exchange one token for another. There are no buy or sell orders, and the price is determined by a formula:

x \cdot y = k

x – the number of tokens of the first type in the pool

y – the number of tokens of the second type in the pool

k – a constant specified by the initial conditions: the product of the number of tokens at the time before the exchange

Let's say at some point in time there were coins in the pool x And yand let some person want to buy tokens of the second type (\Delta y), for tokens of the first type in the amount of \Delta xThen the following happens.

In the pool now (x+\Delta x) tokens, the hyperbola constant is the same, so the following holds

(x + \Delta x)\cdot (y - \Delta y) = k = x \cdot y

From here we find \Delta y – how many tokens will he receive for the given \Delta x

Tokens of the second type became more expensive. The price at which the exchange took place: P = \frac{\Delta y}{\Delta x} (or flip the fraction if we want to measure in another coin)

Where does the liquidity in these pools come from?

There is another part of users that provides liquidity to such pools – liquidity providers. They put their tokens in these pools and receive commissions for each token exchange in this pool. For example, with the swap described above, the commission is taken first, and only then the token exchange occurs.

The exchange fee is distributed among liquidity providers according to the contribution of tokens to that pool.

After the exchange, the total number of tokens of the first type increased, and the second decreased, and accordingly, the ratio of the number of tokens that liquidity providers have in their pool also changed.

Impermanent Loss (for Uniswap V2)

When another user made a token swap, the price of one token to another changed. The overall ratio of the number of tokens in the pool and the ratio of each liquidity provider also changed.
The number of tokens that became more expensive decreased, and those that became cheaper increased.

As a result, the total value of assets at each liquidity provider changed.

x \cdot y = L^2x = \frac{L^2}{y}y = \frac{L^2}{x}d(x \cdot y) = xdy + ydx= d(L^2) = 0 => xdy = – ydx => \frac{dy}{dx} = – \frac{y}{x}” src=”https://habrastorage.org/getpro/habr/upload_files/1a3/879/aa0/1a3879aa0b1e4c578239d7fb180e071b.svg” width=”557″ height=”43″/></p><p>We usually consider one differential to be positive, the second to be negative, and we get</p><p><img data-lazyloaded=

P – the price of one token expressed in the second token

y = L \cdot \sqrt{P}x = \frac{L}{\sqrt{P}}

Let's derive the formula for Uniswap V2 Impermanent Loss:

Total value of assets depending on price:

V = y \cdot 1 + x \cdot P = 2 \cdot L \cdot \sqrt{P}

Let's say that at the initial moment of time the user plans to put tokens in Uniswap V2 in quantities x_0, y_0At the initial moment of time, the total value of these tokens is equal to:

V_0 = y_0 \cdot 1 + x_0 \cdot P_0 = 2 \cdot L \cdot \sqrt{P_0}

If a person did not put tokens in uniswap, but simply “held” them, his total portfolio depending on the price:

V_{held} = y_0 \cdot 1 + x_0 \cdot P = L \cdot \sqrt{P_0} + \frac{L}{\sqrt{P_0}} \cdot P

To simplify the formulas, we set P = P_0 \cdot k, k > 0″ src=”https://habrastorage.org/getpro/habr/formulas/9/99/99f/99fb54c066b724ea0b7bf0cf745f8232.svg” width=”auto” height=”auto”/></p><p><img data-lazyloaded=IL = \frac{V - V_{held}}{V_{held}} = \frac{L \sqrt{P_0} \cdot (2\sqrt{k} - (1+k))}{L\sqrt{ P_0} \cdot (1+k)} = \frac{2\sqrt{k}}{1+k} - 1

That is, in this way you can see how much the user is in the minus/plus compared to if he did not put tokens in uniswap, but simply passively kept them in the wallet.

Sometimes Impermanent Loss is simply called the dependence of the total value of the position on the price, because it is not always interesting to compare only with the position of holding tokens in the original proportion.

A quick dive into how Uniswap V3 works

Important:

I am deliberately omitting some details related to the Uniswap V3 device (for example, activation of only some price ticks, different number of decimals for different tokens, etc.) since in this case they do not greatly affect the result, but complicate the article.

In the case of Uniswap V3, liquidity providers can choose which price range their tokens will be used in. This means that the liquidity provided is not evenly distributed across the entire range.[0, +\inf)” alt=”[0, +\inf)” src=”https://habrastorage.org/getpro/habr/formulas/b/b9/b96/b966935bac54cf2483677c60f843b3d7.svg” width=”auto” height=”auto”/>, а равномерно в отрезке [p_a, p_b],

p_a – lower, p_b – the upper price displayed.

This is advantageous because now, by setting a narrower price range, the user's share of liquidity at a specific price (at a specific 'price tick') becomes larger, so he will earn more commissions. However, the Impermanent Loss that his position experiences also increases.

The idea is similar to Uniswap V2. When exchanging the first token for the second, the same amount is added \Delta xand decreases \Delta y.

In the case of Uniswap V3, the hyperbola is a little different: having descended, it already crosses the Ox and Oy axes.

(x + \frac{L}{\sqrt{P_b}}) \cdot (y + L \cdot \sqrt{P_a}) = L^2

If we make a change of variables, we can bring this hyperbola to its usual form:

\begin{cases} x_{virtual} \cdot y_{virtual} = L^2, \newline \newline x + \frac{L}{\sqrt{P_b}} \geq 0, \newline \newline y + L \ cdot \sqrt{P_a} \geq 0 \end{cases}

IN in the case when the inequalities are satisfiedthe uniswap position is in an active state and generates commissions.

Accordingly, the formulas derived above, linking the number of tokens, liquidity and the current price (formulas from Uni V2), will work:

y_{virtual} = L \cdot \sqrt{P}x_{virtual} = \frac{L}{\sqrt{P}}

Now let's express x, y:

y = y_{virtual} - L \cdot \sqrt{P_a} = L(\sqrt{P} - \sqrt{P_a})x = x_{virtual} - \frac{L}{\sqrt{P_b}} = L(\frac{1}{\sqrt{P}} - \frac{1}{\sqrt{P_b}})

When the price becomes equal P_bthe user's liquidity no longer participates in exchanges and does not receive commissions. Also, now he has 0% of the first type tokens (the price of which has risen) and 100% of the second type tokens.
Similar to when the price dropped to P_a. Then the user now has 100% of the first (devalued) token and 0% of the second token, and liquidity is again not involved in exchanges.

Zeroing out either x or ywe can find the exact number of tokens of the remaining type.

(x + \frac{L}{\sqrt{P_b}})\cdot(y + L \sqrt{P_a}) = L^2

Number of tokens when price rose above the high price:

P = P_b => x = \frac{L}{\sqrt{P}} – \frac{L}{\sqrt{P_b}} = 0″ src=”https://habrastorage.org/getpro/habr/formulas/8/84/84b/84b8146ee8f215c5f6a9250d45a6cc0e.svg” width=”auto” height=”auto”/></p><p><img class=

Number of tokens when price dropped below bottom price:

P = P_a => y = 0″ src=”https://habrastorage.org/getpro/habr/formulas/6/6d/6d8/6d8e20b6abe691df05ffd16ee480ae55.svg” width=”auto” height=”auto”/></p><p><img class=

Impermanent Loss (for Uniswap V3)

Similar to the IL output for Uniswap V2, we write the full value of a position as a function of price:

V = y\cdot 1 + x \cdot P = L(\sqrt{P} - \sqrt{P_a}) + L(\sqrt{P} - \frac{P}{\sqrt{P_b}})

Let it be as before P = P_0 \cdot k

V(P) = 2L\sqrt{P} - L(\sqrt{P_a} + \frac{P}{\sqrt{P_b}})V(k) = 2L\sqrt{P_0 \cdot k} - L(\sqrt{P_a} + \frac{P_0 \cdot k}{\sqrt{P_b}})

If the user did not open a uniswap position, but simply held it in the same ratio as at the initial moment in time:

V_{held} = y_0 + x_0 \cdot P = L(\sqrt{P_0} - \sqrt{P_a}) + L P_0 k (\frac{1}{\sqrt{P_0}} - \frac{1}{ \sqrt{P_b}})V_{held} = L\sqrt{P_0}(1+k) - L(\sqrt{P_a} + \frac{P_0 \cdot k}{\sqrt{P_b}})

Impermanent loss:

IL = \frac{V(P) - V_{held}}{V_{held}}IL = \frac{2L\sqrt{P_0 \cdot k} - L\sqrt{P_0}(1+k)}{L\sqrt{P_0}(1+k) - L(\sqrt{P_a} + \frac {P_0 \cdot k}{\sqrt{P_b}})}

Let's simplify it a bit

IL(k) = \frac{2\sqrt{k} - 1 - k}{1 + k - \sqrt{\frac{P_a}{P_0}} - k\sqrt{\frac{P_0}{P_b}} }

This formula is valid when the inequalities I wrote above are satisfied. Just in case, I'll duplicate it again.

\begin{cases} x + \frac{L}{\sqrt{P_b}} \geq 0, \newline \newline y + L \cdot \sqrt{P_a} \geq 0 \end{cases}

Problems of liquidity providers

Now let's look at the risk profile of a user who has deposited liquidity into Uniswap V3.
Risk profile – in this case, the dependence of the total value of a position on the price.
With strong price drops, firstly, the user will have more and more cheap tokens, and accordingly, he will lose more and more money – that is, we can say that he unprotected risk associated with the price of the underlying asset.

The user would probably like to be less dependent on the price of the underlying asset (eg the price of ether, if it's an “eth-usdc” pair), while still earning fees as a liquidity provider.

What can he do?

He can hedge (=to protect oneself from risk) one's position.

And how this can be done, I will tell you in the following articles.

More interesting articles from the world of DeFi are in my telegram channel:

https://t.me/kirrya_achieves

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *