Automated market makers (AMMs) have been established as a key primitive and building block in DeFi, allowing anyone to trade tokens in a permissionless manner. Users can become market makers and provide liquidity to a pool for a proportionate share of a pool’s total swap fees. However, this comes with risks. When providing liquidity to a pool, you may be subject to impermanent loss. This occurs when the value of holding two tokens over a period of time outweighs the value of depositing them into a liquidity pool over the same time period. The loss remains “impermanent” until a user withdraws their LP position, actualizing their losses.

In a recent upgrade to the Astroport UI, users now have access to charts for every liquidity pool which compare fees vs impermanent loss:

In this article, we cover how impermanent loss works in more detail and how it is calculated for liquidity providers on Astroport.

# Impermanent Loss

Currently, the Astroport protocol supports two types of pools: constant product and stableswap. The former allows for volatile pairs like ASTRO-axlUSDC or ASTRO-LUNA. The latter applies to pairs that closely trade at a 1:1 ratio, such as axlUSDC-axlUSDT. When discussing impermanent loss, we mainly refer to risks associated with constant product pools.

The x * y = k constant product formula preserves an indefinite range for price discovery between a pair. This means the ratio and amount of tokens you initially deposit to a constant product pool can change depending upon price action. For example, if ASTRO increases in value and becomes more scarce in the pool, you may end up with less ASTRO and more axlUSDC than you initially started with. In contrast, stableswap pools optimize for trading at a stable rate and have concentrated liquidity around a significantly smaller range. The ratio and amount of tokens you initially deposit to a stableswap pool will largely stay the same plus swap fees and incentives.

Let’s dive into an example:

Alice deposits 100 axlUSDC and 2000 ASTRO at a rate of 0.05 axlUSDC per ASTRO for a total value of 200 axlUSDC in liquidity. For this example, the pool will also have a total value of 1,000 axlUSDC (500 axlUSDC and 10,000 ASTRO), making Alice’s share 20% of the total liquidity in the pool.

Using the constant product formula (x * y = k), we can solve for the constant (k) where x is the total amount of ASTRO and y is the total amount of axlUSDC in the pool. This will help us calculate the rebalancing of tokens later on.

`k = 10,000 * 500 = 5,000,000`

The value of k could increase or decrease with additional deposits or withdrawals in the liquidity pool. For our example, we assume k remains constant at *5,000,000*, and we simply recalculate the balance of tokens under a new price ratio.

If ASTRO suddenly appreciated by 100% from 0.05 to 0.1 axlUSDC per ASTRO, we would need to calculate the new distribution of tokens in the liquidity pool. To do this, we first get the amount of each asset in the pool at any given ratio, where the current value of r equals the price ratio of the asset in question.

x = sqrt(k / r)y = sqrt(k * r)

For Alice’s initial starting position, k equals *5,000,000*, and the ratio between tokens is 0.05 (0.05 axlUSDC per ASTRO). We can test if our formulas work by returning the expected initial values for the liquidity pool.

x = sqrt(5,000,000 / 0.05) = 10,000 ASTROy = sqrt(5,000,000 * 0.05) = 500 axlUSDC

To calculate the new distribution of tokens in our pool, we simply change the ratio value. In this case, we change *r *from 0.05 to 0.1 (0.1 axlUSDC per ASTRO).

x = sqrt(5,000,000 / 0.1) = 7071.067811 ASTROy = sqrt(5,000,000 * 0.1) = 707.106781 axlUSDC

We see that with the increase in price for ASTRO, the token becomes more scarce in the liquidity pool (from 10,000 to 7071 ASTRO) and the amount of axlUSDC increases (from 500 to 707 axlUSDC). At 0.1 axlUSDC per ASTRO, the pool’s 7071 ASTRO tokens are worth around 707 axlUSDC, equal to the value of axlUSDC in the pair. The distribution and amount of tokens in the pool has changed, but the ratio between ASTRO and axlUSDC remains 50/50.

Additionally, recall that the initial amount of liquidity in the pool was 1,000 axlUSDC (500 axlUSDC and 10,000 ASTRO at a rate of 0.05 axlUSDC per ASTRO). With the new distributions of 707 axlUSDC and 7071 ASTRO, the pool’s total liquidity has increased to roughly 1,414 axlUSDC.

Now that we have our new ratio of tokens, we can check for our value of k using the constant product formula again:

`k = 7071.067811 * 707.106781 = 4,999,999.99807`

We roughly arrive at a similar k value of *5,000,000* with a new balance and distribution of tokens. This means our constant product formula is working, allowing for price discovery over a range of token balances with a similar k value.

Since Alice has a 20% stake in the pool’s total liquidity, the new ratio for her deposited liquidity looks more like this:

7071.067811 ASTRO / 5 = 1414.2135622 ASTRO707.106781 axlUSDC / 5 = 141.4213562 axlUSDC

Recall that Alice started off with 100 axlUSDC and 2000 ASTRO. After the appreciation of ASTRO in price and the pool rebalancing, Alice is left with roughly 141 axlUSDC and 1414 ASTRO. Notice that she ends up with more axlUSDC and less ASTRO than she initially deposited. However, the value of her liquidity is still higher than her initial deposit: 282 axlUSDC vs 200 axlUSDC.

So, where does the impermanent loss come into play? Impermanent loss results from the loss in value one could have actualized if they simply held on to the asset instead. For example, if Alice held on to her initial 2,000 ASTRO and 100 axlUSDC, her axlUSDC would still be worth the same amount. However, at a rate of 0.1 axlUSDC per ASTRO, her 2,000 ASTRO would now be worth 200 axlUSDC. In total, her tokens would now be worth 300 axlUSDC. Compared to the 282 axlUSDC we get when providing liquidity over the same period of time, holding both assets is actually a more profitable strategy (by 18 axlUSDC).

Finally, our above calculations do not account for trading fees. Liquidity providers accrue trading fees from swaps, usually 0.3% for each swap in constant product pools. Since Alice has a 20% share in the pool, she is entitled to 1/5th of all of the pool’s fees. With fees in mind, impermanent loss occurs when the potential gains from holding two assets outweigh the trading fees accrued over a period of time. As more trading volume and fees accumulate for a pool, this could overturn impermanent loss and result in profits from fees alone.

# Conclusion

There are legitimate risks when it comes to impermanent loss that users should consider when providing liquidity. Having an understanding of the risks and rewards can help liquidity providers navigate their positions. Now that you have a better understanding of what impermanent loss is and how it is calculated, you can play with the charts to compare impermanent loss vs trading fees, as well as account for incentive rewards.

✦

Follow Astroport on Twitter and subscribe to the Astroport email newsletter to get the latest alerts from the mothership.

**DISCLAIMER**

Remember, Terra 2.0 and Astroport are experimental technologies. This article does not constitute investment advice and is subject to and limited by the Astroport disclaimers, which you should review before interacting with the protocol.