Uniswap (V2+V3) Impermanent loss

In this article, I will explain what is Uniswap impermanent Loss, and why this is such a hot topic for liquidity providers

More specifically, what awaits you in this article:

  • A dive into how Uniswap V2 works

  • Impermanent Loss for Uniswap V2

  • A dive into how Uniswap V3 works

  • Impermanent Loss for Uniswap V3

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

A 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 the formula

$$x \cdot y = k$$

x — the amount of token1

y — the amount of token2

k — a constant that is determined by initial conditions

Let at some point in time there were x, y amounts of tokens in the pool, and consider a person that wants to buy some tokens2 (y) for tokens1 (x). Then the following happens:

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

Hence we can find \Delta y — the amount of tokens2 that he receives for his tokens1.

Where does the liquidity in these pools come from?

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

The exchange fee is distributed among liquidity providers in accordance with the contribution of tokens to this pool.

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

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

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

$$x \cdot y = L^2$$

$$x = \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}$$

One differential is usually considered positive, second — negative, we get

$$P = \frac{dy}{dx} = \frac{y}{x}$$

P is the price of tokens2 in terms of tokens1

Hence we get

$$y = L \cdot \sqrt{P}$$

$$x = \frac{L}{\sqrt{P}}$$

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

The total value of assets depending on the price:

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

Let at the initial moment of time the user plans to put tokens in Uniswap V2 in amounts of x and y. At the initial moment of time, the total cost 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 a Uniswap, but simply “holds” them, his total portfolio, depending on the price, is determined by:

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

For simplicity consider:

$$P = P_0 \cdot k, :::k > 0$$

Then we get

$$V_{held} = L \cdot \sqrt{P_0} \cdot (1 + k)$$

$$IL = \frac{V - V{held}}{V{held}}$$

$$IL = \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$$

In this way, you can see the user's PnL compared to how he did not put the tokens in Uniswap, but simply passively kept them in the wallet.

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

A dive into how Uniswap V3 works

Important: Here I deliberately omit some details related to the Solidity implementation of Uniswap V3 (e.g. not all the price ticks are activated, different tokens have different decimals, etc), because in this case they do not greatly affect the result, but complicate the article.

In the case of Uniswap V3, liquidity providers can choose in which price range their tokens will be used. Thus, the provided liquidity is not evenly distributed over the entire range [0, +\inf], it is evenly distributed over the specifier price range [P_a, P_b].

This is beneficial because now, by setting a narrower price range, the user’s share of liquidity at a specific price (on a specific ‘price tick’) becomes larger, so he will earn more commissions. However, Impermanent Loss also increases.

Uniswap V3 hyperbola is defined as follows:

$$(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 hyperbole to the “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 the case when inequalities are satisfied, the Uniswap position is active and generates commissions.

$$y_{virtual} = L \cdot \sqrt{P}$$

$$x_{virtual} = \frac{L}{\sqrt{P}}$$

Now we 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 to P_b, the user’s liquidity no longer participates in exchanges and receives no commission. He also now has 0% of tokens1 (whose price went up) and 100% of tokens2. Similarly when the price becomes equal to P_a or even lower. Then the user has 100% of tokens2, and 0% of tokens1.

Amount of tokens when the price rose above the upper price:

$$P = P_b => x = \frac{L}{\sqrt{P}} - \frac{L}{\sqrt{P_b}} = 0$$

$$P = P_b => y^* = L\sqrt{P} - L\sqrt{P_a} = L\sqrt{P_b} - L\sqrt{P_a}$$

$$y^* = L\sqrt{P_b} - L\sqrt{P_a}$$

Amount of tokens when the price fell below the lower price:

$$P = P_a => y = 0$$

$$P = P_a => x^* = \frac{L}{\sqrt{P}} - \frac{L}{\sqrt{P_b}}$$

$$x^* = \frac{L}{\sqrt{P_a}} - \frac{L}{\sqrt{P_b}}$$

Impermanent Loss for Uniswap V3

Similarly to the derivation of Uniswap V2 Impermanent Loss, let’s write down the total value of the position depending on the price:

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

As before, P = P_0 * 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 of 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 \cdot k}{\sqrt{P_b}})$$

Impermanent loss:

$$IL = \frac{V(P) - V{held}}{V{held}}$$

$$IL = \frac{2L\sqrt{P \cdot k} - L\sqrt{P}(1+k)}{L\sqrt{P}(1+k) - L(\sqrt{P_a} + \frac{P \cdot k}{\sqrt{P_b}})}$$

Let's simplify a bit:

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

This formula is valid when the inequalities that I wrote just above are met. Just in case, I’ll write 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 put liquidity into Uniswap V3. Risk profile — in this case, means the dependence of the total value of the position on the price.

With strong price drops, firstly, the user will have cheaper and cheaper tokens, respectively, he will lose more and more money — so we can say that he has an unprotected risk associated with the price of the underlying asset.

User would likely want to be less exposed to the underlying price changes, and still earn Uniswap fees as a liquidity provider.

What can liquidity providers do?

They may reduce risks by hedging their positions.

How hedging works I will explain in the next articles.

Thanks for your time.

Hope you enjoyed my article.

Please subscribe, I will post more interesting articles soon.

You can find other useful stuff on my Github:

https://github.com/kirrya95