Price impact and how to calculate it

What is Price Impact?

Price impact is the difference between the current market price and the price you will actually pay when performing a swap on a decentralized exchange.

Price Impact Example

In the screenshot above, the price impact is 4.64%, meaning the price being paid for YFI is 4.64% higher than the current market price.

Price impact occurs because of the pricing model of the decentralized exchange. As the ratio of the assets in the pool changes, one asset becomes more expensive and one becomes cheaper. Supply of one asset increases and the supply of another asset decreases. This makes the decreasing asset more expensive.

The more that the ratio of the assets is shifted, the more that the price changes. Price impact is a bigger issue in lower liquidity pools where it's easier to change this ratio compared to a pool with very large liquidity.

As an example, let's say there is a liquidity pool with 1,000 ETH and 2,000,000 USDC. Given the ratio of each pool we can say that 1 ETH = 2,000 USDC. I'll refer to this as the market rate.

Swaping 10,000 USDC for ETH in this example pool would put the price of ETH in this transaction at 1 ETH = 2009.64 USDC (price impact of 0.48%). If we were to swap even more, say 100,000, this would put the price of ETH in the transaction at 1 ETH = 2099.95 (price impact of 4.99%).

The reason that we can't swap at the market rate (1 ETH = 2,000 USDC) is because this would mean that you could swap 2,000,000 USDC for 1,000 ETH in this example pool, and then subsequently be left with a pool with 0 ETH and 4,000,000 USDC. And that wouldn't make much sense.

To understand the mechanics of this and to see how I got the price impact numbers above, see the calculation section below. But otherwise, it's just a useful concept to understand, especially in lower liquidity pools.

Price Impact Calculation

This calculation uses the constant product formula used on Uniswap to determine how much of one asset should be swapped for another asset.

Some exchanges may have a slighty more complex formula to combat price impact or impermanent loss. But this is the most common and easiest to calculate.

Constant product formula: token_a_pool_size * token_b_pool_size = constant_product

constant_product will be the same number before and after a trade occurs.

A couple of examples to show price impact calculation:

Starting pool for both examples

Pool info

  • USDC = 2,000,000
  • ETH = 1,000
  • Constant Product = 2,000,000,000
  • Market Price = 2,000

First example, 10,000 USDC for ETH

After swap

  • USDC = 2,010,000 (because we added 10,000 to the pool)
  • Constant Product = 2,000,000,000 (stays the same)
  • ETH = 995.024 (constant product / new usdc amount)

ETH recieved = 4.976 (old eth amount - new eth amount)

Price paid per ETH = 2009.64 USDC

Price impact = 0.48%

Second example, 100,000 USDC for ETH

After swap

  • USDC = 2,100,000 (because we added 100,000 to the pool)
  • Constant Product = 2,000,000,000 (stays the same)
  • ETH = 952.38 (constant product / new usdc amount)

ETH recieved = 47.62 (old eth amount - new eth amount)

Price paid per ETH = 2099.95 USDC

Price impact = 4.99%