0

I am working with Uniswap v3 and need to calculate the sqrtPriceX96 value that is exactly 5% (or any other percentage) above or below the current pool price. I understand that each tick in Uniswap v3 represents a price movement of approximately 1 basis point. Could I use this understanding to calculate the sqrtPriceX96 for a price that is a certain percentage above or below the current price by simply moving a corresponding number of ticks? What are the potential accuracy or resolution trade-offs with this approach?

1 Answer 1

0

To calculate a sqrtPriceX96 that is a specific percentage (e.g., 5%) above or below the current pool price in Uniswap v3, you can indeed use the tick-based approach. Each tick in Uniswap v3 roughly corresponds to a 1 basis point change in price. To adjust the price by, say, 5%, which is 500 basis points, you can add or subtract 500 ticks from the current tick to get the approximate sqrtPriceX96 that is 5% from the current price.

Here's a general approach in Solidity:

import { TickMath } from '@uniswap/v3-core/contracts/libraries/TickMath.sol';

function calculateAdjustedSqrtPriceX96(uint160 currentSqrtPriceX96, int24 basisPointsDelta)
    external
    pure
    returns (uint160 adjustedSqrtPriceX96)
{
    int24 currentTick = TickMath.getTickAtSqrtRatio(currentSqrtPriceX96);
    int24 adjustedTick = currentTick + basisPointsDelta;
    adjustedSqrtPriceX96 = TickMath.getSqrtRatioAtTick(adjustedTick);
}

Accuracy and Resolution Trade-offs:

  1. Precise Tick Calculation with Logarithms: The use of logarithmic calculations for determining the number of ticks corresponding to a specific percentage change is more precise. Since price movements in ticks follow a geometric progression, logarithms accurately capture the necessary tick adjustments. So in the 5% price delta example, the actual number of ticks is 488 and not 500. See this answer for more detail.

  2. Challenges with Solidity Computations: Implementing logarithmic calculations in Solidity can be complex and gas-intensive. Solidity’s lack of native support for floating-point arithmetic and advanced math functions like logarithms makes on-chain calculations of this nature less straightforward.

  3. Quantization in Tick Spacing: While the logarithmic approach is mathematically precise, the fixed tick intervals in Uniswap v3 still introduce a quantization effect. This effect can result in small deviations, especially when dealing with very fine price movements for e.g. within 1 bsp(0.01%).

  4. Precomputed Lookup Tables for Efficiency: To balance precision and computational efficiency, using precomputed lookup tables for common percentage changes is a practical solution. This approach avoids complex on-chain calculations by referencing off-chain computed values.

Your Answer

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

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