We’ve accomplished an in depth calculation of the swap slippage of uniswap V2 within the final publish. It’s comparatively sophisticated to calculate the the swap slippage of uniswap V3 because of its liquidity is a piecewise perform of worth. In fact, we will additionally acquire it by the entrance finish of the uniswap net web page:
However how do you get the swap slippage by calculating it your self? Let’s analyze it intimately beneath. Taking ETH-USDT for example, we all know that the value on uniswap V3 is represented by tick, that’s, ?(?) = 1.0001^?, ? is the tick order, firstly we set all liquidity of the ETH-USDT pool as segments. Suppose it’s divided into n segments, then the liquidity in every tick area included within the n section is identical. We mark it as L1, L2…Ln, and the value vary of every section is [Pan, Pbn], the present worth is P0, we have to calculate how a lot you possibly can swap u0 usdt for ETH. As everyone knows, the quantity of USDT and Ep within the present section as
Clearly on the endpoints of the present section Pan and Pbn may have Epn ETH and Upn USDT
When u0+up<=up, the swap could be accomplished within the present section, and at the moment:
Whereby px is the ultimate worth of pool after swap,
Based on (1) to (6), it may be calculated as adopted:
Then the swap worth Pswap
Then we will get the slipgage (%)
When u0+up>up, that’s, when tick-cross happens, it is just essential to repeat the calculation for the remaining half after the swap within the present section within the subsequent section, and the ultimate slippage will be calculated.
https://x3finance.medium.com/how-to-calculate-swap-slippage-of-uniswap-v3-d433ed6d74b0