Math
Last updated
Last updated
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.
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 instead of . This strategy is referred to as charging fees "on the way in." With the swap fee, the spot price increases. It then becomes:
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.)
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.
The formula then becomes:
Taking into account the swap fees, we have:
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:
Considering swap fees, we have the following:
Considering swap fees, we have the following:
In the Whitepaper, we derive the following formula to calculate the amount of tokens out –– a trader gets in return for a given amount of tokens in –, considering a Balancer pool without any swap fees:
To take into account the swap fees charged by the Balancer pool, we replacewith. This is known as charging the fees "on the way in"
In the Whitepaper, we derive the following formula for the amount of tokens in –– a trader needs to swap to get a desired amountof tokens out in return, considering a Balancer pool without any swap fees:
Since is the amount the user has to swap to get a desired amount out , all we have to do to include swap fees is divide the formula above by . This is because we know the fee charged on the way in will multiply that amount by . This will cross out both terms and the amount out will be as desired:
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 –– that need to be deposited for someone to get 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 user gets for redeeming pool tokens will be:
In the Whitepaper, we derive the following formula for the amount of pool tokens –– a liquidity provider gets in return for depositing an amount of a single token t present in the pool:
Since the pool already has a share of its value in token t, represented by the weight, it only makes sense to charge a swap fee for the remaining portion of the deposit
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 – – and calculate what amount of tokens t is needed – :
Where is the amount of token t one receives when redeeming pool tokens.
If there were an exit fee, it would be taken from the amount of tokens 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, , they would like to withdraw from the pool, and calculates the necessary amount of pool tokens required for that,. The formula without considering swap fees is:
Where is the amount of token t one receives when redeeming pool tokens.