Introduction
When making swap trades on DEXs, you may have come across a negative number marked in red, labelled as ‘price impact’ on the user interface - and wondered what this all means and why it matters to your trade?
In this article, we will be covering succinctly all you need to know fundamentally about price impact.
What is Price Impact?
We can define price impact as :
Price impact is the difference between the market price of an asset and the expected fill price for an incoming trade order.
In other words, price impact measures the influence of the user’s individual trade over the market price of an underlying asset pair.
It correlates directly with:
- the trade size compared to the amount of liquidity in the Automated Market Maker (AMM) (a.k.a. “pool”) — the more illiquid the markets/pairs, the greater the price impact.
- the trading rule used in the AMM (more details below).
It is an important factor when making trades, as it can result in the trader losing value by having to pay a premium for their subsequent buy trades.
To illustrate, if a trade involves swapping a large amount from a highly liquid token like ETH, to an illiquid token like BANANA — because there are not enough sellers of BANANA at the current market price to fulfill the order, the value of BANANA would increase against the value of ETH in this particular trade, resulting in the trader paying a premium, in what is known as negative price impact.
For example, a [negative] price impact of -42.09%, means that the trader will be paying 42.09% more than current market price (or ie. losing 42.09% of trade value) for their swap.
The opposite, positive price impact, is known as arbitrage opportunity, when a pool is imbalanced towards a trader’s favour, where he/she holds the more illiquid (or ‘rarer’) token and is able to “arbitrage” for better deals from the many trade offers at hand.
The math behind price impact
To understand how price impact is calculated, we must first understand fundamentally how an AMM determines its token prices:
- Liquidity pools are smart contracts that act as AMMs — they hold reserves of 2 or more tokens & allow anyone to deposit or withdraw funds from them, according to specific trading rules.
- The standard trading rule found across established DEXs like Uniswap V2, Sushiswap & Balancer, is the
constant product formula
:
$$ x \cdot y = k \\ \text{where } x \text{ is Token A, } y \text{ is Token B, } k \text{ is constant product} $$
or in pseudo-code:
token_a_pool_size * token_b_pool_size = constant_product
This constant product formula enforces that for some amount of token A to be withdrawn, a proportional amount of token B must be deposited to maintain the constant k
before fees (fees increase the k
slightly every trade).
From this constant product formula, the token price formula can be derived as:
price_token_a = reserve_token_b / reserve_token_a
The following pricing curve graph illustrates this (Source: Curve Whitepaper ):
With that, we can now understand how price impact is calculated via examining Uniswap:
- Uniswap V2 defines price impact as “the difference between the mid-price & the execution price of a trade”, with mid-price being the price ratio of the two token reserves shown in the previous chart.
- When inspecting Uniswap’s frontend codebase, we find a script computeFiatValuePriceImpact.tsx that computes the price impact displayed on the app, with the key line:
const pct = ONE_HUNDRED_PERCENT.subtract(
fiatValueOutput.divide(fiatValueInput),
);
In essence, Uniswap calculates an estimate price impact using the following formula:
$$ P_{\text{impact}} = \frac{\text{Output Token} \cdot Pcurrent_{\text{output token value}}}{\text{Input Token} \cdot Pcurrent_{\text{input token value}}} - 1 $$
As an example, if we wanted to swap 10,000 WBTC to ETH, we calculate as such:
1 — Calculate expected output token to receive based on the constant product:
We have the pool information :
$$ \text{WBTC Reserve} = 319.3029 \\ \text{ETH Reserve} = 5549 \\ $$
$$ \text{constant product } k = \text{ Token A Reserve } \cdot \text{ Token B Reserve } = 319.3029 \cdot 5549 = 1755164.792 $$
Swapping 10,000 WBTC should give us:
$$ \text{New WBTC Reserve } = 319.3029 + 10000 = 10319.3029 \\ \text{constant product k} = \text{unchanged} = 1755164.792 \\ $$
$$ \text{New ETH Reserve} = \frac{\text{constant product } k}{\text{New Token A Reserve}} = \frac{1755164.792}{10319.3029} = 170.0855968 $$
This means we receive:
$$ \text{Expected ETH received} = \text{Previous Reserve} - \text{New Reserve} = 5549 - 170.0855968 = 5378.914403 $$
2 — With that, we can now calculate the estimated price impact:
Let’s measure the value of the tokens in BTC terms:
$$ Pcurrent_\text{output token value} = Pcurrent_\text{ETH value in BTC} = 0.057006502 \\ $$
$$ Pcurrent_\text{input token value} = Pcurrent_\text{BTC value in BTC} = 1 $$
Substituting the variable, we get:
$$ P_\text{price impact} = \frac{5378.914403 \cdot 0.057006502}{1000 \cdot 1} - 1 = -96.934 \text{ percent to 5 s.f.} $$
If we were to only make the swap fully within this single WBTC-ETH pool, then the price impact is a very high -96.934%.
How to minimise negative price impact?
If we take the previous 10k WBTC swap to ETH as an example, we find that on Uniswap, the actual Price Impact is much lower (-59.23% instead):
This is because most DEXs have a built-in pathfinder algorithm (in this example, called Auto Router
) that automatically splits a trade amount between multiple liquidity sources, which helps reduce negative price impact. On Uniswap, this can be found in the smart contract Router02
.
Other than that, here are some simple ways to reduce negative price impact manually:
- Find the market with the deepest liquidity, in the price range we want. We can use CoinGecko as a starting point to view the depth of markets, e.g. for BTC:
-
Trade off-chain, for example on CEXs. As there are arbitrage opportunities for “backrunning” trades due to rate differences between CEXs & DEXs, many times DEXs may not offer the best prices.
-
Spread out trades in smaller trades over time. Routing through different “bridge” currencies may also help reduce negative price impact - in this regard, aggregators like 1inch are particularly useful.
Conclusion
Price impact is a very important metric to watch out for whenever making trades on DEXs / liquidity pools - a negative price impact means that your trade is making you lose value. DEXs try to reduce this effect automatically through auto-routing feature written in their smart contracts, but traders can further mitigate negative price impact by always looking out for markets with the deepest liquidity and spreading out their trades over time into smaller chunks.
So, the next time you are making a trade on a DEX, be sure to pay attention to that sneaky little red figure that is “price impact”!