Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
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:
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.
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.
80/20
6,005.3009
5,315,514
50/50
135.9922
30,158
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.
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.
60/20/20
107,244
56,164
25/25/25/25
5,194,894
2,722,125
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:
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) |
---|---|---|
80/20
6,005.3009
5,315,514
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) |
---|---|---|
50/50
135.9922
30,158