# Deriving impermanent loss formula in Balancer

• Rúben do Reis • 14 min read## Introduction

Decentralized exchanges, specifically automated market makers (AMMs), play a crucial role in the DeFi ecosystem. Due to their significance, it is essential to examine the risks associated with these products. In an effort to compare the expected impermanent loss among major DEXes, we found there was not yet an explicit proof of the impermanent loss formula for Balancer. As a result, this blog post aims to fill that gap by deriving the formula outlined in Fernando Martinelli's page, which offers a well-presented intuition.

As a quick overview, Balancer is a decentralized exchange that allows users to trade Ethereum-based tokens in a trustless environment. It uses smart contracts to enable users to trade ERC-20 tokens, creating liquidity pools for any sequence of tokens. The platform uses a unique algorithm to automatically adjust the token price based on the executed trades.

## Impermanent Loss

The term "impermanent loss" (IL) is used in the context of liquidity pools. When liquidity providers (LPs) contribute their tokens to a pool to facilitate transactions, they might face an opportunity cost. The term IL refers to the fraction of the pool's worth that is worth less than what it would have been if the LPs had simply retained their tokens outside of the pool. In other words, IL is a measure of the opportunity cost of owning tokens indirectly rather than directly. This cost is paid because the relative values of the tokens in the pool fluctuate over time, resulting in a reduction in the aggregate value of the LPs' investments.

For simplicity’s sake, assume there are no deposits or withdrawals in order for us to have a constant value function, . Notice that, according to the Balancer Whitepaper, the spot price or marginal rate of substitution (MRS) of an input token with respect to the output token was proven to be:

Where represents the reserve of the token that is being sold at time , is the correspondent weight in the pool, is the reserve of the token that is being bought at time , and denotes the respective weight.

It is important to emphasize that the weight of each token in any Balancer pool is strictly between zero and one, and when summed, the weights of all tokens add up to unity.

For the sake of simplicity, let us express the price of any token as its current value in dollars per token, which gives us the flexibility to define the price of any -th token whose reserve is as a function of time such as:

When a trader wants to buy token , they can do so directly using an acceptable currency through an exchange, or they can first buy token and then use it to acquire token using the pool. To this end, inline with our definition of the spot price and as stated in the Balancer Whitepaper, the price of token may be expressed as: .

Due to the previous price relation, the following property holds true for any two tokens and in any Balancer liquidity pool: .

As an example, consider that an LP adds BTC, ETH, and BAL as liquidity into a Balancer pool, with weights of , , and , respectively. The initial prices of BTC, ETH, and BAL are , and respectively at time , in units of . At time admit there was a price change such that the current price of BTC, ETH, and BAL in dollars terms are now given by , , and . If we consider the following price ratios:

By using these inputs into the formula provided by none other than the co-founder of the Balancer protocol Fernando Martinelli himself on his Medium page, we can say that the IL this LP will be subjected to at time if they choose to provide liquidity in this hypothetical Balancer pool will be

So in particular, this liquidity provider would have suffered an impermanent loss or opportunity cost of about of his initial capital.

## The impermanent loss formula

In this framework, we want to show that if we consider a liquidity pool containing multiple tokens. Considering the token indexing set , the impermanent loss, , can be given by

where and represent respectively the pool weight of token and the associated future and initial price ratio.

## Proof for the two token case

It is important for the reader to know this proof can be done directly considering the general case, i.e., a liquidity pool composed of multiple ERC-tokens. However, due to the complexity that formulation would entail, it is truly advantageous to build a solid intuition about the reason for each step that will appear in the proof of the general case, and that is precisely the reason behind the proof provided in the case where the liquidity pool is made up of only two tokens.

In this setting, consider the simplest case where there are two tokens, and . The LP invested in the pool by providing assets of both tokens for the pool reserves, which are represented by and at the instant , for and , respectively. By defining as the fractions of the pool reserves provided by the LP, the total amounts of and in the pool can now given by and , correspondingly. Note that, in practice, the LP does not need to provide tokens in the the exact ratio present in the pool.

Let us operate in the case of no deposits or withdrawals, so we can assume that the Balancer pool function, , is constant, yielding

where , and represent the weight, i.e. the value-proportion of tokens and , respectively, in this particular pool. Recall also, that because this is a Balancer weighted pool as previously stated we know that and .

Furthermore, by defining as the pool-invariant associated to the LP, one has

Because both denote token quantities in the pool, we naturally have for all . As we previously mentioned, the condition holds true, where represent respectively, the token and prices (in dollars per token) at any time .

This property allows us to have

From the equation above, if we substitute, for example, the by the expression we obtain isolating it from , the resulting expression is:

Analogously, we can derive the expression for which yields

Hence, the value (in dollars) at time the LP invested, i.e., for , will be given by

Had the LP held their tokens instead, the asset quantities would have remained constant, and we can derive their value at time - which will be denoted by . Notice that we may substitute the initial (and constant) quantities of the LP's reserves by the pool's relation, because in the initial instant there is, by definition, no price variation - this is will be useful later for direct comparison between the expressions. It follows:

If we consider the following notations for sake of simplicity:

then the previous result becomes:

Conjugating the expressions for and , and taking into account the definition of impermanent loss , we may conclude that the impermanent loss, , that the LP will be subjected to, at the maturity time , is given by:

Now we have built our intuition by proving the result for the simplest case when we have a liquidity pool composed of two ERC-tokens, let us prove that this result holds true when considering a pool made of tokens.

## Proof for the multiple token case

The current reserve of token in the multiple-tokens Balancer weighted pool is denoted as , for any , where is the indexing set for the tokens in the pool.

For example, one could have: .

Notice that even for a liquidity pool with multiple tokens, we can still assert the base premise we have been using thus far still holds true, i.e. for each ,

similarly to the case of -tokens, we can isolate from the invariant associated with the LP amount of tokens, which yields

It is important to emphasize that the weights sum up to one, which leads to

By replacing the reserve of token in equation with its correspondent expression from , we obtain

Therefore the fraction of the Pool value that belongs to the LP at time in dollar terms is

Again, had the LP held his or her tokens instead of providing the liquidity with them to this pool, the quantities of each would have remained the same from time , expressly , for . Hence, the hold value in dollars is given by

Having the definition of impermanent loss in mind, we may conclude that the LP will face impermanent loss at time , which will be given by

where we are considering for each .