DeFi Security Lecture 9 — Impermanent Loss

Beaver Finance
beaver-smartcontract-security
12 min readJan 24, 2022

Do you know what keeps LPs awake at night? Impermanent loss

What is Impermanent Loss?

When you provide liquidity to a liquidity pool, transient losses occur, and the price at which you deposit the asset changes compared to when you deposit it. The greater this variation, the more volatile losses you face. In this case, the loss means that the dollar value at the time of withdrawal is lower than the dollar value at the time of deposit. This is called impermanent loss because the loss is realized only after you withdraw tokens from the liquidity pool.

However, the losses become permanent, and the fees you earn may make up for them.

This kind of loss occurs when you do liquidity mining on Uniswap. This loss is due to the price divergence of the token, and when prices return, that loss disappears.

In essence, impermanent loss occurs when depositing assets into an automated market maker (AMM) and withdrawing them later results in a loss compared to if you’d have just held your assets in your wallet.

Impermanent loss is impermanent because losses are registered only after money is withdrawn from the liquidity pool. Until then, any losses are merely potential and could be reduced or wiped out entirely, depending on market movement.

To properly understand how impermanent losses occur, you first need to understand how the liquidity pools are used by AMM-based decentralized exchanges such as Uniswap, SushiSwap, or PancakeSwap work.

So why provide liquidity if liquidity providers face potential losses? Well, transaction fees can still offset impermanent losses. In fact, even pools on Uniswap that are very vulnerable to impermanent losses can be profitable due to transaction fees. Uniswap charges a 0.3% fee for each trade that goes directly to liquidity providers. Providing liquidity can be profitable if large volumes occur in a given pool, even if that pool is heavily exposed to impermanent losses. However, this depends on the protocol, the specific pool, the depositing asset, and even broader market conditions.

Use extreme caution when depositing funds into AMM. As we discussed, some liquidity pools are more susceptible to impermanent losses than others. As a simple rule, the more volatile the assets in the pool, the more likely you are to face impermanent losses. It’s best to start by depositing a small amount of money. This way, you can get a rough estimate of your expected return before investing a larger amount.

The last point is to look for more tried and tested AMMs. DeFi makes it easy for anyone to fork an existing AMM and add some small changes. However, this can expose you to bugs that could keep your funds stuck in AMM forever. If a liquidity pool promises unusually high returns, there may be a trade-off somewhere, and the associated risk may also be higher.

Basic Concept Pertinent To Impermanent Loss

- Liquidity Pool

Liquidity pools usually consist of a token pair (eg ETH:DAI).

The weights of both tokens are equal in order to create a market for users to trade in and out.

For example, an ETH:DAI pool consists of 50% ETH and 50% DAI.

By pre-funding the pool like this, AMMs avoid the need to pair buyers with sellers. Instead, traders have access to a pool of liquidity that is permanently available without having to wait for someone to trade on the other side, which is how traditional exchanges using spot markets work.

- Automated market maker

AMM calculates transaction prices for standard liquidity pools. They raise and lower the value of crypto assets based on what traders buy or sell. The formula for each DEX may vary, but the most popular form is: x*y = k

Here, x is the amount of one cryptocurrency in the pool. y is the number of another and k is the total liquidity in the pool. When the total liquidity k changes, the ratio of x and y must be adjusted to maintain balance.

The total liquidity in the pool may change when transaction fees are increased or when liquidity providers add or remove their liquidity.

It can be displayed through dynamic graphs: inject/destroy liquidity, swap transactions, and adjust fees; in addition, some auxiliary lines are displayed, such as expected price tangent, slippage of transaction price, etc.

Why does Impermanent Loss occur?

Let’s use an example to illustrate what impermanent losses for liquidity providers might look like:

Alice deposits 1 ETH and 100 DAI in the liquidity pool. In this Automated Market Maker (AMM), the deposited token pair needs equal value. This means that the price of ETH at the time of deposit is 100 DAI. This also means that Alice’s deposit has a dollar value of $200 at the time of deposit.

Additionally, there is a total of 10 ETH and 1,000 DAI in the pool — funded by other LPs like Alice. Therefore, Alice owns 10% of the pool with total liquidity of 10,000.

Suppose the price of ETH rises to 400 DAI. In this case, arbitrage traders will add DAI to the pool and remove ETH from it until the ratio reflects the current price. Remember, AMM does not have an order book. What determines the price of assets in the pool is their proportion in the pool. While the liquidity in the pool remains the same (10,000), the ratio of assets in it changes.

If ETH is now 400 DAI, then the ratio of ETH to DAI in the pool has changed. There are now 5 ETH and 2,000 DAI in the pool due to the work of arbitrage traders.

Therefore, Alice decides to withdraw her funds. She is entitled to 10% of the pool, as we know before. As a result, she can withdraw 0.5 ETH and 200 DAI for a total of $400. Since she deposited $200 worth of tokens, she made some decent profits, right?

But wait, what would happen if she just held her 1 ETH and 100 DAI? The total dollar value of these assets will now be $500.

We can see that Alice is better off by HODLing instead of depositing into a liquidity pool. This is what we call impermanent loss. In this case, Alice’s loss is not large because the initial deposit is relatively small. But keep in mind that impermanent losses can result in huge losses (including a significant portion of your initial deposit).

Having said that, Alice’s example completely ignores the transaction fees she earns for providing liquidity. In many cases, the fees earned will offset losses and make providing liquidity profitable. Even so, it is critical to understand impermanent losses before providing liquidity to DeFi protocols.

Estimating Impermanent Loss

When the price of the assets in the pool changes, impermanent loss occurs, and we can take the value to draw a curve of this loss rate.

When the gain rate is -1 (that is, the price becomes 0), the loss rate is 1 — — -100%

When the rate of gain is 0 (that is, the price remains unchanged), the rate of loss is 0 — — 0.00%

When the gain rate is 0.25, the loss rate is 0.006 — — 0.6%

When the gain rate is 0.5, the loss rate is 0.02 — — 2.0%

When the gain rate is 1 (that is, the price doubles), the loss rate is 0.057 — — 5.7%

When the gain rate is 4 (that is, the price increases by 4 times), the loss rate is 0.255 — — 25.5%

You also need to know some important things. Impermanent losses occur no matter which direction the price moves. The only concern with impermanent losses is the price ratio relative to the time of deposit.

Drawing the course of Impermanent Loss

When you provide liquidity to the pool, you are depositing the equivalent value of each asset (e.g. $100 in ETH and $100 in DAI). You then receive Liquidity Provider Tokens (LP Tokens), a receipt that entitles you to a percentage of the funding pool, which is dynamic and corresponds to the amount of liquidity and funding you provide, the ratio of the total number in the pool.

So, if you contribute $200 of assets to the pool, bringing the total value to $1,000, your LP tokens will be entitled to 20% of the pool (which now includes transaction fees) or other when you use them again to withdraw assets in the future award). However, if others add assets to the pool over time and increase the total to $2,000, you will only get 10% of the pool now.

Unfortunately, however, there are unique risks involved when supplying 2 assets into a pool that requires the asset value to remain balanced.

For example, an ETH/LINK pool with a total value of $2 million would require $1 million in ETH and $1 million in LINK to balance, regardless of how many tokens are actually equivalent.

As the value of a token (or two) starts to fluctuate, the balance in the pool will change. People are also transacting in transaction pools, which can also cause one side of the pool to grow or shrink, eventually reaching a 60/40 balance.

When that happens, it presents an opportunity for carry traders to buy one of these assets at a discount, essentially, compared to the rest of the market. By taking advantage of this, arbitrage traders will naturally end up rebalancing in the pool. This is an important part of how AMMs keep operating but creates problems for liquidity providers.

As a result of the rebalancing, the number of tokens on both sides of the pool changes, although the value remains the same. Remember, LPs are entitled to a percentage of the pool, not a certain amount of tokens or the equivalent in USD. This means that when you withdraw from the pool, you may receive more of one token and less of the other.

Depending on how the prices of these assets change, you may end up “losing” compared to if you just left those tokens in your wallet.

Illustrating Impermanent Loss Using A Mathematical Calculation

To understand the possibility of impermanent loss, it is best to go using a more analytical approach:

Investor A wants to deposit liquidity into the ETH:DAI liquidity pool on SushiSwap. To account for impermanent loss, let us assume that the total liquidity in the pool remains constant at all times. No transaction fees are added, and no liquidity is removed or added.

The current price of 1 ETH is $100. Since DAI is a USD stablecoin, 1 DAI is 1 USD. The ratio of the liquidity pool must be balanced (50:50), so Investor A deposits 1 ETH and 100 DAI into the liquidity pool. The total investment is $200.

There are a total of 10 ETH and 1,000 DAI in the liquidity pool. According to the AMM formula above, the total liquidity in the pool is $10,000 (10 x 1,000). Investor A owns 10% of the shares.

In a week, the real-world market price has changed significantly, so the price of 1 ETH is now $200 (or 200 DAI). There is now an imbalance between the real world market price and the trading price of the liquidity pool. This is an arbitrage opportunity.

Arbitrage traders buy ETH from liquidity pools that are 50% cheaper than real-world external market prices. This reduces the amount of ETH and increases the amount of DAI. This process continues until 1 ETH = 200 DAI.

There is now a new allocation of ETH and DAI in the liquidity pool. a computable one. This involves defining some variables taken from the automated market maker formula and adding a new variable “r”. r is the new ratio for crypto assets.

For this example, x = ETH, y = DAI, k = $10,000 (total liquidity), and r is 200 (1 ETH = 200 DAI). The new distribution for each asset can then be calculated using the following formula:

After the arbitrage process, the liquidity pool has just over 7 ETH and just over 1,400 DAI.

Remember that Investor A is entitled to 10% of the liquidity pool. If they wish to withdraw their share at the end of the week, they can withdraw 0.707 ETH and 141.42 DAI. At the new market price, this equals $282.82, and investor A received $82.82 compared to the initial investment.

However, if Investor A had left the initial 1 ETH and 100 DAI in a crypto wallet, their assets would be worth $300 at the new market price.

The impermanent loss in this example can be calculated by subtracting $282.82 from $300. The impermanent loss is $17.17.

How to avoid Impermanent Loss?

In volatile markets, immortality is almost guaranteed when staking crypto assets in standard liquidity pools. Exchange prices always change. However, there are ways to mitigate the effects of impermanent loss:

- Transaction fees

In the mathematical example above, no transaction fees have been added to the liquidity pool. Use liquidity pools to collect transaction fees from traders, then reward liquidity providers with a portion of those fees. These fees are sometimes sufficient to mitigate and offset any impermanent losses. The more transaction fees charged, the less impermanent loss. After a certain point, if a pool charges enough fees, investors will earn more for staking assets in a liquidity pool than for holding them.

- Low volatility pair

Impermanent losses can occur in most volatile cryptocurrency pairs. However, impermanent losses can be mitigated by choosing cryptocurrency pairings that trade-in price that does not fluctuate. Examples of low volatility currency pairs include stablecoin pairings, such as DAI:USDT, or different variants of the same token, such as wETH(wrapped Ether):ETH. These examples include cryptocurrency pairings with very similar prices. Therefore, major price movements between the pair are unlikely. If there are no price fluctuations, impermanent losses can be avoided.

- Complex liquidity pools

One of the main reasons for impermanent losses is the 50:50 split most liquidity pools require. Some decentralized exchanges such as Balancer provide users with multiple liquidity pool ratios to overcome this problem. They also offer pools with 2+ digital assets. Price changes in a pool with a higher ratio (e.g. 80:20 or 98:2) do not result in as much impermanent loss compared to a pool with a 50:50 split.

- Unilateral liquidity pool

Impermanent losses occur in standard liquidity pools where 2 different crypto assets must be deposited. However, some exchanges like Bancor have developed liquidity pools that offer users the opportunity to stake on only one side of the pool. The other end of each liquidity pool on Bancor consists of the native Bancor token, BNT. For example, for all ETH provided to the ETH:BNT liquidity pool, the system adds an equivalent amount of BNT. Since users only need to provide one side of the liquidity pool, there is no risk of permanent loss.

Bancor also recently integrated price feeds through the decentralized oracle Chainlink. By linking liquidity pools to real-time market prices, they can automatically adjust when prices change significantly, which is impossible in standard liquidity pools.

Additional Facts about impermanent Loss

While the basics of impermanent loss have been covered, there are a few additional details worth knowing before betting liquidity on DeFi protocols.

  • Permanent losses can occur regardless of price direction. In the mathematical example above, we increased the price of ETH and explained that impermanent losses mean less gains compared to digital assets in wallets. However, a permanent loss will occur irrespective of which asset in a cryptocurrency pair is moving. Investors can only withdraw digital assets that have not suffered impermanent losses when the exchange price is exactly the same at the time of withdrawal.
  • Second, impermanent losses are only realized when funds are withdrawn. It is “impermanent” because the price can revert to the original trading price at any time. If prices revert, impermanent losses will cease to exist. This loss is permanent only when investors withdraw funds from the liquidity pool. How to avoid impermanent losses.

Impermanent Loss Calculator/Tool

To help investors navigate the complexities of impermanent losses, several online calculators are now available to help investors determine the potential risks of depositing assets in specific liquidity pools.

Here are a few options:

  1. Daily DeFi: Daily DeFi is an educational website covering most aspects of the decentralized finance space. The site includes a built-in impermanence loss calculator. The calculator applies Uniswap’s AMM formula and allows users to enter current token prices and future token prices. Any future impermanent losses can then be calculated.
  2. Decent yield: DecentYields is a website focused on providing users with the best insights into the yield returns of lending and liquidity pools in DeFi. The site also provides a detailed impermanence loss calculator. The calculator allows users to enter the dollar value of the digital asset they are considering staking, the amount of each token, and the percentage change in price.

Summing up

Beaver is DeFi’s first single-asset yield-enhancing protocol that combines liquidity mining with a next-gen (European)options-based Impermanent loss hedging solution. Beaver provides a far more robust and efficient solution to drive decentralized liquidity and unlock maximum capital efficiency in DeFi.

Beaver Finance team up with experienced security experts on building our system and would continuously share our findings on DeFi security topics.

Stay Alert, Stay Safe!

Reference

https://everipedia.org/wiki/lang_en/impermanent-loss

--

--

Beaver Finance
beaver-smartcontract-security

Beaver Finance is a Single-Asset Intelligent Yield Enhancing platform with the Option-based cutting-edge hedging solution for Impermanent Loss.