I want to expect how much input tokens should be spent to get exact amount of output token on Uniswap V3. Uniswap V2 has router.getAmountsIn() to expect it. V3 supports quote interface but it is supposed to be called from off-chain only.

If it's impossible, I want rough value too. So I want to calculate A to get B. In some way, we got rough value A. A -> B', B' >= B, B'- B -> 0

1 Answer 1


The formula is laid out in the SDK documentation. You need to read the slot0 value from the UniswapV3Pool contract of the pair you are interested in. The first uint160 value returned in the sqrtPrice96X. Then depending on which token price of the two you want you either use:

price = sqrtRatioX96 ** 2 / 2 ** 192


price = 2 ** 192 / sqrtRatioX96 ** 2


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.