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 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.
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 whitepaper)
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 whitepaper)
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: