I need to calculate the price impact on Uniswap V3 to further compute the bid-ask spreads. What would be the mathematical formula and which data do I need?
Thank you in advance for the help I'll keep the post updated with my research
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityIt is not possible to have a single mathematical formula as a swap Uniswap v3 may cross tick range boundaries. Each tick range may have different liquidity and needs to be considered separately. The multi-step swapping algorithm is given in the whitepaper, Figure 4.
For a single step, the computeSwapStep
logic may be used. Alternatively, look at formulas 6.9 to 6.16 in the whitepaper and implement the logic yourself.
Edit: If access to on-chain data is an option, the best solution may be to call the QuoterV2 contract using a static call, as it returns not only the amount, but also the sqrtPriceX96After
, which can be compared with the pool's previous sqrtPriceX96
to directly get the price impact.