Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
This example is based upon the Arbitrum Weighted pool named in the title
At time of writing, this pool holds roughly 10.261 million USD. To be less precise on decimals we will say the pool holds $3.410MM of USDC, $3.422MM of WETH, and $3.429MM of WBTC. Please note in this example we are ignoring price impact and swap fees which are incurred during a trade. To solidify the idea that a pool is its own market and is independent of the external prices, we can see here that the USD price of USDC is slightly under $1. These prices are dictated directly by token balances, and will only be arbitraged back to market price by external users if the extractable amount is greater than the “financial friction” (gas and swap fees) of the system.
We can see our pools will keep the weighting constant. This means the value of the token will dynamically change with the quantity of each token in the pool.
Weight (Wt) will be constant, in this case each token’s weight is 33.33% of the pool. This is set during the creation of the pool
The value V(usd) will be our pool value over the number of tokens in this case is calculated by:
We can now determine our value of each token.
Using the expected value as a benchmark for what the pool would hold in a perfectly balanced state, we can generate the following data:
In both instances the value function holds true yielding essentially the equations below:
This page will discuss both the 80/20 pool mechanisms as well as how to correlate USD value to the balance of tokens with in a pool. The relationship is a function of the pool traits.
Pools with assets which are much more independent of one another such as BAL/WETH are subject to impermanent loss which we can model here or see the simulator linked here. The invariant function does not depend on external market prices, only the balance of tokens and weightings within the pool. However, we can consider a pool’s USD value over time.
At the time of writing the BAL/WETH 80/20 Pool holds
5,532,476 BAL ($112,530,555)
9,726.09 WETH ($28,160,135)
Therefore, the total balance is $140,690,690.
The expected balances:
Therefore, a pool can go through a transition to hold slightly more WETH than anticipated and slightly less BAL. Yielding the following charts.
In this case we now know our weights will be constant based on the pool ratios.
In the prior example an opportunity for profit through arbitrage presents itself. A trader or investor can input their tokens valued higher than the open market does in terms of USD in order to extract value in by exchanging on an external market.
This example is done at a snapshot in time, but please understand there is much more working internally. Slippage, price impact, and gas fees must be considered when an arbitrager is acting to rebalance a pool and extract profit for doing so.
The center building block for all Balancer Protocol pools and swaps stems from the value function. Learn more in this section.
The invariant, or Value Function, of Balancer Weighted Pools is critical for maintaining pool value and fair swap prices. . This weighted constant product equation facilitates pools of assets with fluctuating prices with respect to one another. Fluctuations inherently lead to price impact, which creates r arbitrage opportunities for rebalancing the pool to market price. Another consequence of this mechanism is impermanent loss, or divergence loss. What does this mean to a user and what is different between two token and multiple token pools? We will look at several examples to explain the differences.
Value Function:
Where
t ranges over the tokens in the pool
Bt is the balance of the token in the pool
Wt is the normalized weight of the token, such that the sum of all normalized weights is 1.
t
Essentially t represents how many tokens there are in a pool. We will do even splits to start (33/33/33) and look at when we have uneven weights (80/20) afterwards.
Bt
Bt is the balance of the token but this is not to be confused with the universal cost of the token. The pool is only as aware of prices as its contents dictate. The market of traders are responsible for (and financially incentivised to) making the value match the rest of the market.
For general use:
The USD value of the balance of a given token within the pool is dependent upon an external definition of price, often supplied by CoinGecko:
Wt
Wt is the weight of the token in the pool. If a pool is well balanced with market prices, the weight of a token corresponds to how much of the pool’s value is denominated in that token.
This section will outline spot price and effective price with the end goal of explaining how price impact, also known as slippage in some cases, is calculated.
In order to understand how swaps are going to alter the price of tokens in a pool we must understand the relationship between the spot and effective prices tokens assign to one another as pools go on and off balance. In theory trades in any market are working with a mechanism of price impact. Supply and demand is the root logic, where as you take more of something out of circulation, it becomes more valuable. For example people consume more of a certain product, the price will go up because there is less of it. This happens in an extreme way when swapping a large amount of tokens. The value of what you want goes up, the value of what you are selling in turn will go down.
Dissecting the spot price and effective price functions followed by some small scale and real scale examples will bring us to understand price impact.
The Spot Price as defined in the Whitepaper:
Where the variables represent the following:
SP(i^o) is the spot price of the input and output tokens in the pool.
B(i) is the balance of the token coming into the pool (input)
W(i) is the weight of the token coming into the pool (input)
B(o) is the balance on the token going out of the pool (output)
W(o) is the weight of the token going out of the pool (output)
Important to note that these variables are from a pool in a stagnant state. Do not incorporate the swap you are planning in to do by adding or subtracting the balance changes you will create in your swap at this point.
Balancer Labs Documentation:
This section highlights and dives into the equations and calculations done under the hood of Balancer Protocol. Expanding on the whitepaper and solving examples to solidify understanding.
Reference the Balancer white paper here.
Balancer Labs Documentation:
Balancer protocol math:
Pool Interfacing: Weighted, Stable, MetaStable, Liquidity Bootstrapping, Managed, and Oracle Pools
This is a proof for the net USD value of a pool based upon the invariant. Please note this can be done for any denomination of currency.
The change in value of a Balancer pool given a price reference (in this case a base of USD) as seen on the medium article linked here can be defined by the following equation :
Note per the whitepaper the invariant (V) is a constant value based upon a pool’s traits. This would mean that
This understanding of the invariant as a constant surface is crucial when proving the initial equation stated to be true.
Next, we can calculate the Value function in relation to the USD reference point using the following equations:
Assuming Pi is equal to the price of a token in reference to USD in this case
2. Once a change in price occurs the Balance of each token will change due to the pool being rebalanced by arbitrageurs yielding (B’) at a new price (P’) in relation to the reference.
3. To determine the change in dollar value of our entire pool we will need the ratio of our new USD value divided by our initial USD dollar value. This is shown as the following:
4.This is to be simplified to the following by isolating our balances from our prices.
5. Therefore the fraction of these two values is equal to 1 leaving:
An example of a calculation of spot price. Please see the intro for a reference to the equation used for spot price.
See introduction here
Let’s look at an entry level example to understand Spot Price before moving on.
Assume a new pool is created. It is an 80/20 AAVE/WETH pool. The fair market price per CoinMarketCap is $284.00 per AAVE and $3500.00 per WETH. The pool creator started the pool off with 100k perfectly split in liquidity therefore we have 80k in AAVE and 20k in WETH.
The relationship between the two can be described as:
Note: These values are precisely equal when using exact numbers. Small margins of error occur in above calculations due to rounding off to 4 or less decimal places.
That is how spot price works. The USD value is to be incorporated after the token relationship is determined in order to not overcomplicate the process the last step should be this conversion.
Here a guide and example of how effective price is calculated will be shown below. Please reference prior portions of this section, trading formulas, or the whitepaper for references.
Next, we will look at the Effective Price which in short will consider the impact on the price you are making on the tokens as you make a trade. Note investing in a pool if it is not of perfect proportion is using the same principle in calculating price impact. The white paper shows the following formula:
EP(i)^(o) represents the effective price of the token input in terms of the token output
A(i) represents the number of tokens one will input (sell)
A(o) represents the number of tokens one will output (buy)
This equation is not much use to us as we want to predict the effective price, and with the formula above we can only do this knowing the number of tokens we will get out of a trade. With the effective price increasing as we buy more of a token the above equation needs to be manipulated.
This brings us to the the Out-Given-In Function (see proof on )
Through substituting this equation into our Effective Price equation, we get the following relationship based strictly on variables which are known prior to a trade.
Example Calculations for (BAL WETH 60/40 on Arbitrum)
Known pool trait values:
We are willing to trade 10 WETH in. We will determine how much BAL we will receive and what the price impact of our trade is. We well repeat this for 100 WETH to compare the results.
With these known values we can solve our effective price equation.
The same equation for 100 WETH yields the following:\
Clearly much more WETH is needed to buy a single BAL token, this adds up and grows as the amount going into a trade does. The market is being moved by the trade (within the confines of the pool).
Using this information, we can now calculate the price impact based on ratio between the spot price and the effective price. In theory the spot price equals the effective price only for extremely small trades. (See Balancer )
Example 4 – Including Trading Fees
To go one step further we can incorporate the trade fees into our trade. Ultimately our effective price will be altered due to the token we are selling being used to meet the swap fee. The incorporation of the pool swap fees can be done in the effective price function as follows:
This will account for less tokens being put into the pool because they were absorbed by the fee. Therefore lower price impact is expected due to a slightly smaller trade occurring,
Price impact now yields:
Out-Given-In
One may raise the point that Balancer pools have fixed or dynamic swap fees and can be controlled by the pool owner(s). Therefore, it is possible that the best pool will change if the swap fee is considered, and a trader receives less BAL by paying the swap fee. To take the swap fee into account we will plug in an alternative value (Ai’) for the amount in (Ai) variable in the
At the time of writing the swap fee for the 80/20 pool is 0.05% and the 50/50 pool’s is 0.1%. Clearly with a lower fee and already higher yield, the 80/20 pool will remain the best route for traders yielding the following:
However, an optimization for pool holders can be made to increase the swap fee to the point where their pool is just slightly more favorable for traders. In this case the trader gets the best deal still while the liquidity provider is optimizing the market in their favor. This is outside the scope of the Out-Given-In Equation as the amount in and out will be known values while the swap fee will be an unknown value. Here is how our equation would look if we were to solve for Swap Fee.
We can now solve for the maximum swap fee the BAL/WETH 80/20 pool could have had set. This maximum fee correlates to the value at which the Amount Out for the Amount In would be competitive with the market and maximize swap fees collected by liquidity providers. To do this we will back calculate the swap fee at which the 80/20 could have had by using the 50/50 pools benchmark for amount out. All values are the pool traits of the 80/20, only the amount of (Ao) will be based on the prior 50/50 pool calculations.
Please note this a dynamic circumstance and the swap fees should be optimized over a longer period. Once a swap is executed the optimal swap fee will change due to the price impact a pool undergoes caused by a swap. Furthermore, pools on every decentralized exchange are competing for rates with lower swap fees and varying liquidity depths. This example is meant to be an introduction to how liquidity depth, relative prices, and fees are related.
Balancer’s trade interface is based on several formulas which give a user the permissions to exchange tokens based on the relationships of the underlying pools. Balancer is host to many pools which contain the same tokens, creating the obstacle of which pool is best to route trades through. The solution is that we as users do not need to concern ourselves with deciding which pool to exchange with. While many tokens are found in several pools, Balancer has developed a Smart Order Router also known as the SOR. The SOR will guide a trade through all the options towards the one which benefits our traders the most.
Why would understanding Balancer Protocol’s trading formulas be important to a user? Every interaction with the Balancer Protocol will be impacted by these equations to some degree. For traders, understanding how the price of assets is calculated can vary across Automated Market Making Platforms. These equations are the foundation of market making on Balancer Protocol and create a massive potential for profitable use cases through arbitrage and liquidity provision. For liquidity providers these same equations will provide a gateway to earning swap fees and making long term positions earn yield by harvesting the surrounding market’s volatility.
Balancer’s formulas for trades as defined by the whitepaper are the Out-Given-In, In-Given-Out, and the In-Given-Price which will be described below.
The following variables will be used in these equations:
Balancer Labs Documentation:
This page is meant to encompass the spot price and effective price calculations discussed prior. The end goal being to understand how price impact affects a liquidity pool and traders.
Using our previous example from Arbitrum we can calculate the spot price using the equation from example 1 and from there determine the price impact using the following equation.
Spot price:
Finally we can calculate price impact for both instances of 10 WETH or 100 WETH trades. (Spot price is technically constant as long as the pool remains in its observed state.) Based on calculations
This example was tested on the actual site in order to confirm this. However depending on the trade you may interact with different pools to receive the best price. Please note this value will always act against the trader, regardless of being presented as a positive or negative value.
Out-Given-In
The first function we will examine is the Out-Given-In which is used to calculate what you will receive based on what you are willing to sell. For example, if you know you will sell or spend a given amount of a token, we can calculate what tokens you will receive in return. Hence what amount you will get out, given what amount you put in; based upon the traits of the pool the trade is routed through.
Let’s look at a very common trade, we will consider trading WETH for BAL. We have intentions of trading 2.5 WETH for the maximum amount of BAL possible. We will look at two possible pools we can route through to mimic what the Smart Order Router is doing under the hood of the Balancer Protocol to make sure users receive the best possible deal.
The two pools we will consider are the 80/20 (0.05%) and 50/50 (0.1%) BAL/WETH pools. At the time of writing, they hold the following balances.
Pool (Wo / Wi) | WETH Balance (Bi) | BAL Balance (Bo) |
---|---|---|
(Please note these are excluding swap fees)
First, we will calculate the amount out for the potential 80/20 pool interaction
Next, we will determine the amount of BAL we would receive if interacting with the 50/50 pool
We can see here the SOR would direct us to the 80/20 pool for our swap to be executed, because it benefits us as the user to receive as much BAL as possible. If another pool exists which would give the trader even more BAL, that route would be executed. See the Smart Order Router section for an example of trade optimization routing through multiple pools as opposed to one. Please note the example above would yield the optimal number of BAL for a user if the trade was routed through both pools depending on the gas price.
In-Given-Out
The In-Given-Out equation will be the opposite thought process as the Out-Given-In formula. The idea is a trader, you, or I, wants to receive a specific number of tokens and this equation will solve how many tokens need to trade into a pool to receive that amount out. Note the variables defined in the introduction have not changed.
For example, we will determine which of two pools is best to trade USDC for WMATIC on Balancer’s Polygon network pools. We will examine the two following pools TEL/WMATIC/USDC 60/20/20 (0.2%) and the WMATIC/USDC/WETH/BAL 25/25/25/25 (0.25%) pools.
Pool (Wo / Wi) | USDC Balance (Bi) | WMATIC Balance (Bo) |
---|---|---|
Firstly, let us see what it will cost us to receive 1000 WMATIC as the amount we know we want out of our trade.
TEL/USDC/WMATIC 60/20/20:
WMATIC/USDC/WETH/BAL 25/25/25/25:
In the case we will spend less using our second option to receive the same amount of WMATIC making it the more desirable pool for the SOR to route us through. Now let’s incorporate swap fees.
80/20
6,005.3009
5,315,514
50/50
135.9922
30,158
60/20/20
107,244
56,164
25/25/25/25
5,194,894
2,722,125
In-Given-Out
Our prior equation can now be utilized in place of Ai to determine the actual amount we must spend to receive the 1000 WMATIC we desire. We must now consider Ai to be our initial value as paying our swap fee will increase the Amount-IN required.
TEL/USDC/WMATIC 60/20/20:
WMATIC/USDC/WETH/BAL 25/25/25/25:
We can then determine the break-even swap fee for the WMATIC/USDC/WETH/BAL 25/25/25/25 pool below.
In-Given-Price
Another approach or thought process that can be used between pools as opposed to increasing swap fees to make all pools equal in spot price, is the potential for arbitrage trading. Arbitrage trading is when a trader buys for example the excess BAL from the 80/20 pool and then sells it to the open market. If a large enough gap in price exists, then this will become an efficient transaction, after fees including gas payments are considered. Therefore, an arbitrager can make a profit by balancing out the spot prices of two sources.
The formula for the number of tokens traded to create the intended shift of this type of trading is defined in our . A very important note here is the assumption that market depth is infinite for the new spot price. This will be elaborated on throughout the example.
In-Given-Price formula:
The additional variables are defined as follows:
For a further description on Spot Price and how it is calculated please refer to and sections of our documentation.
From these examples we can see not much WETH can be extracted considering the amount of capital required. However, some people may be interested in making even the smallest amount of profit. If this is done effectively it adds up across multiple markets and tokens. Notably, the margin shrinks after gas costs are considered.
Major factors to an arbitrage opportunity are the following:
The discrepancy in price between the two price sources
A larger gap between SP’ and SP will yield a larger profit
The market depth of both sources
If the pools both hold large balances of tokens, they will be resistant to price impact.
Note that one pool may have a lower spot price, but large trades will make the effective or average price of the swap favor pools with larger depths. This would make a pool with a higher spot price, but more stable swap rate, a better option.
The capital you have available
Larger amounts of capital will maximize your profits; however, the margins will be best on the first token traded and decrease as price impact is occurring.
This means with small amounts of capital you be can successful but need to prioritize large price discrepancies.
The goal of sharing this information is to equip our community of current and future users with the knowledge to leverage Balancer Protocol in their best interest. This ultimately will benefit Balancer as a whole, driving innovation, new ideas, and future efficiencies to build, based upon the building blocks of the underlying protocol. By understanding how trading formulas bridge the trading and liquidity providing members of Balancer is key to point us toward a sustainable future for both parties.
80/20 BAL/ WETH Pool In-Given-Price problem and solution
Example
We will utilize our 80/20 BAL / WETH pool (0.05% Swap fee) as a benchmark and will assume the external market spot price to be 0.00455 WETH / BAL. Please note this market for the purpose of the example is considered infinite in depth. This means as we sell our tokens, the price is not impacted.
If the trade was occurring from one pool to another further optimization would be done to justify our swap. The method would then also consider the impact of the arbitrage opportunity on the second market we interact with.
Pool (Wo / Wi) | WETH Balance (Bi) | BAL Balance (Bo) |
---|---|---|
50/50 BAL/ WETH Pool In-Given-Price problem and solution
Alternate Example:
We will utilize our 50/50 BAL / WETH pool (0.1% Swap fee) as a benchmark and will assume the external market spot price to be 0.0048 WETH / BAL. Please note this market for the purpose of the example is considered infinite in depth. This means as we sell our tokens, the price is not impacted.
In comparison to our previous example, the following pool is much less deep in terms of total liquidity. Due to the lack of depth, we can expect to create a larger price impact using lower amounts of capital.
Pool (Wo / Wi) | WETH Balance (Bi) | BAL Balance (Bo) |
---|---|---|
Overview of asset deposits and withdrawals on Balancer Protocol
What is the act of providing liquidity, and what does it mean for me as an investor? Providing liquidity is when a user or entity places their assets into a liquidity pool. A liquidity pool is its own ecosystem where the assets within it are priced in relation to one another. The relative pricing inside the pool is only altered when outside interactions such as trades or new “off-balance” investments occur. The expectation as a liquidity provider (LP) is that as market prices change, the liquidity pool will be interacted with, and the internal balances of tokens will most likely change over time.
What benefits does providing liquidity give me, why should I not just hold all my assets individually? There are several key benefits providing liquidity for users which holding them does not permit.
Firstly, are the swap fees which are charged to those who interact with your liquidity pool. The more interactions with your pool, the more swap fees you will earn, growing the size of your investment over time. The second benefit are the platform rewards. Multiple decentralized exchange platforms such as Balancer incentivize their liquidity pools with additional rewards for asset pairs which are chosen through the liquidity mining & governance systems. This makes a liquidity pool an easy way to gain access to and maintain exposure in a strategic way to multiple assets while receiving economic incentives.
When providing liquidity to a pool on Balancer users have a few options. Originally in Balancer V1 liquidity providers had one option; to provide liquidity in the portions of tokens designated by the pool. For example, if a pool was weighted at a 50 / 50 ratio the liquidity provider had to provide liquidity of both tokens proportionally. This can become cumbersome in cases of multiple tokens such as eight token index pools.
Balancer V2 now permits liquidity providers the option to invest in pools using only a single asset which will then be added to the pool shifting the ratios of all other assets. This can be done with as many tokens as a liquidity provider has applicable to a pool and in proportion creating an extreme amount of flexibility when user’s want to invest in more exotic asset pools.
When a user invests in a pool in an unproportioned manner a price impact will take place because the internal pool ratios are changing. This is indirectly a cost to a new liquidity provider due to their investment shifting asset prices. This occurrence is called price impact. Like price impact incurred by traders during large swaps, this happens to maintain the supply and demand markets for all tokens involved. In this document we will examine several examples for each of these different investment conditions to display the underlying math which determines price impact on investments.
80/20
6,005.3009
5,315,514
50/50
135.9922
30,158
In-Given-Price-Proof - Please note the prerequisites of the spot price function and In-Given-Out Equations.
In-Given-Price Proof:
Firstly, we must site the spot price function:
2. Secondly, to consider the desired spot price (SP’) we must acknowledge that the Balance-In (Bi) and the Balance-Out (Bo) will be altered based on the Amount-In (Ai) and Amount-Out (Ao) as so:
3. The original spot price can be rearranged to isolate the Balance-Out (Bo):
4. The adjusted spot price equation (SP’) can be arranged to isolate Balance-Out minus the Amount-Out (Bo – Ao) as follows:
5. . Using these equations for substitution in our In-Given-Out equation below we can prove the In-Given-Price Equation is valid:
First, we replace the Balance-Out and Balance-Out minus Amount-Out statements with our equations from steps 3 and 4.
At this point the Weights-In and Weights-Out will neutralize one another reducing our equation size, once this is done the next step is to rationalize our fraction:
Now we need to isolate the spot price ratio to remove the Balance-In (Bi) and Amount-In (Ai) from our inner function:
We will move our exponent to the opposite side of the equation and then adjust our ratio of Balance-In over Balance-In minus Amount-In
Now using this adjusted ratio, we can multiply both sides of the equation by Amount-In, Balance-In portion combining the two terms and have the fraction of SP’ and SP on the right side of our equation as shown below:
By adjusting the exponents and reverting the bulk of our equation to the right side again we can solve for the Amount-In as intended for the In-Given-Price Equation:
This section will breakdown the three core cases of asset deposits on Balancer Protocol.
Balancer Protocol allows users to invest in pools in a variety of ways. Proportional deposits yield no price impact and follow the ratios the pool is already in.
Single Sided Deposits permit users to deposit into a pool with only one of the assets. A price impact due to shifting the internal relationship of the tokens will be accounted in this scenario.
Multi-token Deposits are when a user deposits the tokens they have in an unproportional way. Not a single asset but multiple in a non-proportionate way.
See examples of each deposit type in the following pages:
How does a single sided deposit work?
The pool tokens issued to a depositor of a single asset can be determined by utilizing the change in the value function (invariant) of the pool. As seen in the whitepaper the ratio of the change in the value function will yield the number of tokens issued. This information will become even more important when discussing multi-asset deposits:
This can be simplified for our purpose of a single sided deposit where all other tokens in a pool maintain constant balances. The token which we are investing into a pool will be denoted with the letter “t”. Amount in (At), Balance-In (Bt), Weight-In (Wt) and the pool token supply will be the variables in concern. When simplified the equation is as follows:
Proportional deposits will yield newly minted Balancer Pool Tokens (BPT) for an investor. This function essentially states that the if the proportional amounts of all tokens deposited (Dk) in relation to the Balance of that given token (Bk) will yield that amount of the new total supply of pool tokens. The example in the whitepaper states that depositing 10% of each of the existing tokens in the pool an investor will receive 10% of the current BPT supply.
Equation:
Sample calculation:
Arbitrarily we will assume we are depositing 15% of the total balance meaning Dk divided by Bk will be 0.15
We will assume our total supply to be 10,000 BPTs at this time for the sake of simplicity. From here can make the following calculations:
Substituting 0.15 for our deposit over balance ratio and 10,000 for the supply value we can solve for the issued amount of pool tokens.
This shows that using the formula, based upon the number of tokens we deposit into a pool based upon the balance already we present we will be awarded a proportional number of pool tokens. These pool tokens are a user’s way to redeem their assets when the pool is exited.
Two examples of calculating price impact with the incorporation of swap fees in single asset deposit scenarios.
Now let’s assume we are depositing into the BAL/WETH 80/20 Pool with 10,000 BAL and no WETH. We will be charged swap fees on the portion of our investment which is “off-balance”. In this case 20% of our pool is not in the form of BAL, therefore that is the taxed portion. As referenced from the trading with swap fees section.
The calculation follows as:
The current pool traits are as follows:
Swap fee: 0.05%
BAL: 5,691,640
WETH: 6,194.1921
BPT: 2,891,789.44800306
Using the known amount for A(t) we can calculate the amount of pool tokens we will be issued for this deposit.\
We can see on the user interface completing this transaction will yield a price impact of 0.03% (0.18% for the larger interaction). What exactly does this mean for an investor? Well in short from price impact for trading we can note that the ratio between assets change as supply and demand shifts during an exchange. In this case we are essentially exchanging BAL for Pool tokens and in doing so Pool tokens are becoming more expensive in reference to BAL. This is a bit more complex than comparing prices of individual assets in a pool, but we will focus on the current example to start. The spot price of a pool token and effective price of a pool token are the variables we need to calculate to determine our price impact from an investment.
Pool token spot price (Assume A(t) = 1 BAL):
Next, we must calculate the ratio of the pool’s invariant before and after the exchange of 10,000 BAL.
Using the invariant ratio, we can determine the new total number of pool tokens and in the same stroke, calculate the amount of pool tokens which we received for the exchange.
Knowing the amount of BAL, we needed to invest and the pool tokens we received in exchange we can determine the effective price of a pool token to compare with our spot price:
Multi-token deposits encompass the single sided deposit knowledge base and emphasize how Balancer Protocol's invariant and swap fees are incorporated in complex scenarios.
Balancer Protocol has several features which differentiate it from all other core AMM platforms such as Uniswap and Curve. The one of focus in this section is the multi-token pools and the layer of complexity they add to the calculation of price impact, and ultimately disproportionate asset deposits.
Withdrawals only support proportional or single sided options in the current state; therefore, a single sided example will be solved. The principles defined in the proportional and single sided sections will be building blocks for the examples solved here.
For both examples we will utilize the WMATIC MTA WETH 40/40/20 Pool on Polygon as it highlights uneven weightings of assets as well as the multi-token aspects, we are interested in.
Pool token spot price (Assume A(t) = 1 BAL):
Then, we must calculate the ratio of the pool’s invariant before and after the exchange of 100,000 BAL.
Using the invariant ratio, we can determine the new total number of pool tokens and in the same stroke, calculate the amount of pool tokens which we received for the exchange.
Knowing the amount of BAL, we needed to invest and the pool tokens we received in exchange we can determine the effective price of a pool token to compare with our spot price:
Here we will solve two examples using the pool above, weights are 40%, 40%, and 20% respectively.
Pool traits at the time of writing:
Swap fee: 0.25%
WMATIC: 273,763
MTA: 1,023,625
WETH: 66.0812
Total BPT: 249494.507172
For this example, we will choose to deposit 1,000 WMATIC and 2 WETH without altering the total MTA within the pool. Our alternate example will use a deposit of 10,000 WMATIC and 2 WETH to solidify understanding in a slightly more complex scenario. These instances are referenced below.
Firstly, we will determine our spot prices of each token in terms of 1 BPT. This can be done using the reciprocal of the redemption function most conveniently, making our input 1 BPT. We will do this for WMATIC and for WETH.
WMATIC
WETH
Next, we can determine the amount of BPT our investments would be worth based upon the spot price, this assumes zero price impact is occurring and sets our base case.
From here we must determine what value of our BPT are disproportionate from the ratios of the pool for an evenly distributed deposit this can be done simply by multiplying the Total BPT by the weight of each token as follows.
Using these values, we can determine how much of the total swap fee will be paid for each single token deposited. If our Base Value deposit for any token in BPT exceeds the Proportionate BPT deposit value, a swap fee will be implemented on the excess portion.
For example, because we deposited a Base Value of 364.5419683 BPT in WMATIC and the proportional deposit is 749.9135826 no swap fee will be implemented on the 1,000 WMATIC which were deposited. These will be crucial in determining our invariant ratio after the swap.
In the case of WETH our Base Value is 1510.241988 BPT, and proportional deposit is only 374.9567913 BPT. This yields the following calculation for a taxable amount:
Lastly, we must calculate our initial and post deposit invariant with fees to determine our net BPT and price impact based on the base case.
Our invariant ratio, multiplied by the initial total pool tokens, will yield the total pool tokens and therefore the amount which we will receive during our deposit. This value in comparison with our Base Case will determine our price impact.
This section will breakdown the two core cases of asset withdrawals on Balancer Protocol.
Balancer Protocol allows users to redeem their investments from pools in a two possible ways. Proportional withdrawals yield no price impact and follow the ratios the pool is already in.
Single Sided withdrawals permit users to withdraw from a pool in the form of only one of the assets. A price impact due to shifting the internal relationship of the tokens will be accounted in this scenario.
See examples of each withdrawal type in the following pages:
This example shows an increase in WMATIC only which evens out the ratios of deposit compare to the previous Multi-token example. Notice the decrease in price impact as the ratio is better managed.
Pool traits at the time of writing:
Swap fee: 0.25%
WMATIC: 273,763
MTA: 1,023,625
WETH: 66.0812
Total BPT: 249494.507172\
The changes from the previous example will be notably base value of 10,000 WMATIC in BPT changing the calculations that follow.
The new Base Total BPT will change the proportional values as shown below.
In this case both of our Base Values for WMATIC and WETH are greater than the proportional values for an evenly distributed despot. This means that both deposits will be taxable in terms of the swap fee.
WMATIC
WETH
Finally, we can utilize these values to compare the invariant ratios between the initial and the after-swap fees are paid. This will determine the BPT we receive as opposed to what we would without any change in spot price.
Our invariant ratio, multiplied by the initial total pool tokens, will yield the total pool tokens and therefore the amount which we will receive during our deposit. This value in comparison with our Base Case will determine our price impact.
An example of a proportional Deposit into the BAL/WETH 80/20 liquidity pool
At the time of writing the BAL / WETH 80/20 pools has the following traits:
Swap fee: 0.05%
BAL: 5,682,882
WETH: 6,232.9054
BPT: 2,891,832.103892
Let’s assume we want to invest 500 BAL, and proportionally this would prompt us to invest 0.548393 WETH to maintain the pool balance (see The Value Function section for reference). We know our amount in and balance in of each token. The ratio of Dk over Bk will be the same regardless of which token is chosen in proportional investments and withdrawals:
See the to see that this transaction is reversible.
The opposite operation is needed to calculate the amount of each token remove from a pool upon withdrawing. The value we calculate will correlate to the balances of the tokens within the pool. Given any pool an investor may have funds in, they can determine the tokens they will receive as a % of the token balances.
Equation:
The Balance term (Bk) will vary for every pool and each token within a pool however the portion of each balance an investor is entitled to is related to their share of the pool in reference to pool tokens always.
As the external market prices of assets change the balances of tokens will fluctuate due to trading. The portion of the pool owned by an investor can only be changed as investors enter and exit the pool. For example, you may own 10% of a pool and after a few months as more people invest you now own 4% due to total supply of tokens increasing.
Assuming our previous example where we hold 1,500 pool tokens let’s assume the pool, we invested in has attracted far more liquidity. Now the total supply is at 25,000 pool tokens. Let’s calculate the number of tokens we will receive when we redeem our BPTs.
Using the expression above we know that given the current balances of each asset in the pool (Bk) we will receive 6% of each token in our wallet upon redemption. These examples may be simple in nature but, coupled with swap fees, they are the first building block towards understanding the complexity of single of mixed asset deposits and withdrawals.
Single Sided Withdrawals will follow the same principles laid out in the single sided deposit section prior. The invariant is the core of these transactions and resulting equations are derived from there. As seen in the whitepaper the ratio of the change in the value function will yield the number of tokens redeemed.
This can be simplified for our purpose of a single sided withdrawal where all other tokens in a pool maintain constant balances. The token which we are removing from a pool will be denoted with the letter “t”. Amount Out (At), Balance-Out (Bt) before the transaction, Weight-Out (Wt) and the pool token supply will be the variables in concern. When simplified the equation is as follows:
At the time of writing the BAL / WETH 80/20 pools has the following traits:
Swap fee: 0.05%
BAL: 5,682,882
WETH: 6,232.9054
BPT: 2,891,832.103892
Under the same conditions shown in the deposit example we will determine the amount of each token we would receive if we redeemed the 254.433587 pool tokens we were issued. The amount we receive should be the same as we would invest for the issued amount because the pool traits are constant in this example.
This solidifies the premise of accounting for the pool issuing and redeeming pool tokens Given the pool traits the minting or burning (investment or withdrawal) of pool tokens will respect the ratios of the balances within the pool and the percentage of total pool tokens.
Now let’s assume we are withdrawing from the BAL/WETH 80/20 Pool, we want to remove 5 WETH from the pool not knowing how many pool tokens we will need to redeem. We will be charged swap fees on the portion of our withdrawal which is “off-balance”. In this case 80% of our pool is not in the form of WETH, therefore that is the taxed portion. In this case we know we will receive the total of 5 WETH from the withdrawal, (At), this means our “sent” value will be unknown and slightly higher to compensate the swap fee we are charged.
The current pool traits are as follows:
Swap fee: 0.05%
BAL: 5,598,984
WETH: 5,798.4836
BPT: 2,816,401.77912812
This equation can be rearranged to solve for A (sent) in terms of our BPT (P_redeemed) as follows:
Using this equation, we can solve for A (sent):
For our Spot Price:
From here we can calculate the amount of pool tokens we redeemed to receive our 5 WETH:
Then, we must calculate the ratio of the pool’s invariant before and after the withdrawal.
Using the invariant ratio, we can determine the new total number of pool tokens and in the same stroke, calculate the amount of pool tokens needed to exchange for our WETH.
Knowing the amount of BPT we needed to redeem and the amount of WETH we received in the withdrawal we can determine the effective price of a pool token to compare with our spot price:
Please note the swap fee and pool traits change dynamically over time making calculations such as these valid for only a short period of time. The purpose of these examples is to deepen the understanding of our platform for those interested in are already utilizing Balancer Protocol.