# Math

## This page has been deprecated. V1 documentation is partially maintained here

## Math

The Balancer whitepaper describes a set of formulas derived from the value function for interacting with the protocol. The formulas in the Theory section are sufficient to describe the functional specification, but they are not straightforward to implement for the EVM, in part due to a lack of mature fixed-point math libraries.

Our implementation uses a combination of a few algebraic transformations, approximation functions, and numerical hacks to compute these formulas with bounded maximum error and reasonable gas cost.

### Exponentiation Approximation

#### Spot Price

Where:

*Bi*is the balance of token*i*, the token being sold by the trader which is going*into*the pool.*Bo*is the balance of token*o*, the token being bought by the trader which is coming*out*of the pool.*Wi*is the weight of token*i**Wo*is the weight of token*o*

When we consider swap fees, we do exactly the same calculations as without fees, but using $A_i \cdot (1-swapFee)$ instead of $A_i$. This strategy is referred to as charging fees "on the way in." With the swap fee, the spot price increases. It then becomes:

#### Out-Given-In

In the Whitepaper, we derive the following formula to calculate the amount of tokens out –$A_o$– a trader gets in return for a given amount of tokens in –$A_i$, considering a Balancer pool without any swap fees:

To take into account the swap fees charged by the Balancer pool, we replace$A_i$with$A_i \cdot (1-swapFee)$. This is known as charging the fees "on the way in"

#### In-Given-Out

In the Whitepaper, we derive the following formula for the amount of tokens in –$A_i$– a trader needs to swap to get a desired amount$A_o$of tokens out in return, considering a Balancer pool without any swap fees:

Since $A_i$ is the amount the user has to swap to get a desired amount out $A_o$, all we have to do to include swap fees is divide the formula above by $(1-swapFee)$. This is because we know the fee charged on the way in will multiply that amount$A_i$ by $(1-swapFee)$. This will cross out both terms $(1-swapFee)$ and the amount out will be $A_o$ as desired:

#### All-Asset Deposit/Withdrawal

Anyone can be issued Balancer pool tokens (provided the pool is finalized) by depositing proportional amounts of each of the assets contained in the pool. So, for each token *k* in the pool, the amounts of token *k* –$D_k$– that need to be deposited for someone to get $P_{issued}$pool tokens are:

Conversely, if a user wants to redeem their pool tokens to get their proportional share of each of the underlying tokens in the pool, the amounts of token k –$A_k$– a user gets for redeeming $P_{redeemed}$pool tokens will be:

All Balancer Protocol smart contracts were coded supporting a protocol-level exit fee to be charged that goes to Balancer Labs for supporting the development of the protocol. However, after careful consideration the Balancer Labs team decided to launch the first version of Balancer without any protocol fees whatsoever. (For technical reasons, this is unlikely to change.)

#### Single-Asset Deposit / Withdrawal

**Single-Asset Deposit**

In the Whitepaper, we derive the following formula for the amount of pool tokens –$P_{issued}$– a liquidity provider gets in return for depositing an amount $A_t$of a single token *t* present in the pool:

Since Balancer allows for depositing and withdrawing liquidity to Balancer pools using only one of the tokens present in the pool, this could be used to do the equivalent of a swap: provide liquidity depositing token A, and immediately withdraw that liquidity in token B. Therefore a swap fee has to be charged, proportional to the tokens that would need to be swapped for an all-asset deposit.

Another justification for charging a swap fee when a liquidity provider does a single-asset deposit is that they are getting a share of a pool that contains a basket of different assets. So what they are really doing is trading one of the pool assets (the token *t* being deposited) for proportional shares of all the pool assets.

Since the pool already has a share of its value in token *t*, represented by the weight$W_t$, it only makes sense to charge a swap fee for the remaining portion of the deposit $A_t \cdot(1 - W_t)$

The formula then becomes:

The formula above calculates the amount of pool tokens one receives in return for a deposit of a given amount of a single asset. We also allow for users to define a given amount of pool tokens they desire to get – $P_{issued}$– and calculate what amount of tokens *t* is needed – $A_t$:

Taking into account the swap fees, we have:

**Single-Asset Withdrawal**

Without considering swap fees, each withdrawal formula is simply the inverse of the corresponding deposit formula. In other words, if you deposit a given amount of token *t* for pool tokens and then immediately redeem these pool tokens for token *t*, you should receive exactly what you started off with.

The formula without considering swap fees is then:

Where $A_t$ is the amount of token *t* one receives when redeeming $P_{redeemed}$pool tokens.

Considering swap fees, we have the following:

If there were an exit fee, it would be taken from the amount of tokens redeemed $P_{redeemed}$but as mentioned above this fee is zero in the first version of Balancer.

Balancer also allows for a liquidity provider to choose a desired amount of token t, $A_t$, they would like to withdraw from the pool, and calculates the necessary amount of pool tokens required for that,$P_{redeemed}$. The formula without considering swap fees is:

Where $A_t$ is the amount of token *t* one receives when redeeming $P_{redeemed}$pool tokens.

Considering swap fees, we have the following:

Last updated