MultiversX Tracker is Live!

How to calculate swap slippage of uniswap V3?

Etherum Reddit

More / Etherum Reddit 249 Views

How to calculate swap slippage of uniswap V3?

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:

https://preview.redd.it/z1ey5ufvyw491.png?750&format=png&auto=webp&s=d8121be452de813763cdc78a3037f9c5cac753ea

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

https://preview.redd.it/l5kquxjwyw491.png?619&format=png&auto=webp&s=1a115d86201e07f776e7afe3d9c65ff629ae8804

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

https://preview.redd.it/ajy281g0zw491.png?636&format=png&auto=webp&s=eb7ecc1fa316d8b2a74e6550c7f10bff360319aa

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

https://preview.redd.it/rkthlfj1zw491.png?610&format=png&auto=webp&s=f1fe6ad2c2075fc37de3f673cf6c4f63553d6238

Wherein px is the final price of pool after swap,

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

https://preview.redd.it/6xh9ucl3zw491.png?449&format=png&auto=webp&s=d235b114c2e7fa430a19666248ef897da7521795

Then the swap price Pswap

https://preview.redd.it/tz6pqop4zw491.png?655&format=png&auto=webp&s=4ced89007aeee96637f41efaecbc397be992f693

Then we can get the slipgage (%)

https://preview.redd.it/alpg5gz5zw491.png?865&format=png&auto=webp&s=d10c0a0d5430acb442f5fb60d9178441584239a0

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.

https://x3finance.medium.com/how-to-calculate-swap-slippage-of-uniswap-v3-d433ed6d74b0

submitted by /u/x3finance
[link] [comments]

Get BONUS $200 for FREE!

You can get bonuses upto $100 FREE BONUS when you:
πŸ’° Install these recommended apps:
πŸ’² SocialGood - 100% Crypto Back on Everyday Shopping
πŸ’² xPortal - The DeFi For The Next Billion
πŸ’² CryptoTab Browser - Lightweight, fast, and ready to mine!
πŸ’° Register on these recommended exchanges:
🟑 Binance🟑 Bitfinex🟑 Bitmart🟑 Bittrex🟑 Bitget
🟑 CoinEx🟑 Crypto.com🟑 Gate.io🟑 Huobi🟑 Kucoin.



Comments