Liquidity Pools 101: What is Price Impact?

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?

Jul 23, 2022    m. Sep 14, 2024    #defi   #blockchain  

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?

swap

What is this mysterious -76.42% number on my Uniswap 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:

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:

$$ 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 ):

chart

Example shown in chart: Token X reserve is 10, Token Y reserve 2.5, hence the price of 1 Y token is 10 / 2.5 or 4 X token.

With that, we can now understand how price impact is calculated via examining Uniswap:

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

swap2

The actual price impact indicated for this swap on Uniswap is much lower than what we theoretically calculated before, due to auto-routing feature.

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:

depth

Bitcoin markets liquidity depth & spread is shown on CoinGecko — very handy!

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”!