How to calculate swap slippage of uniswap V3?

x3finance
3 min readJun 11, 2022

--

→ Free Crypto Trading Bot with APY as high as 100%↑

We have done a detailed calculation of the swap slippage of uniswap V2 in the last post. It is relatively complicated to calculate the the swap slippage of uniswap V3 due to its liquidity is a piecewise function of price. Of course, we can also obtain it through the front end of the uniswap web page:

But how do you get the swap slippage by calculating it yourself? Let’s analyze it in detail below. Taking ETH-USDT as an example, we know that the price on uniswap V3 is represented by tick, that is, 𝑝(𝑖) = 1.0001^𝑖, 𝑖 is the tick order, firstly we set all liquidity of the ETH-USDT pool as segments. Suppose it is divided into n segments, then the liquidity in each tick space included in the n segment is the same. We mark it as L1, L2…Ln, and the price range of each segment is [Pan, Pbn], the current price is P0, we need to calculate how much you can swap u0 usdt for ETH. As we all know, the amount of USDT and Ep in the current segment as

Obviously at the endpoints of the current segment Pan and Pbn will have Epn ETH and Upn USDT

When u0+up<=up, the swap would be completed in the current segment, and at this time:

Wherein px is the final price of pool after swap,

According to (1) to (6), it can be calculated as followed:

Then the swap price Pswap

Then we can get the slipgage (%)

When u0+up>up, that is, when tick-cross occurs, it is only necessary to repeat the calculation for the remaining part after the swap in the current segment in the next segment, and the final slippage can be calculated.

--

--

x3finance
x3finance

Written by x3finance

Free High Profit Excel AI Crypto Trading Bot including Okx and Binance

No responses yet