Index of every page on this site: llms.txt
All skills
whetstoneresearch avatar

/uniswap-fundamentals

@4f654fe
Run it yourself

Reference for Uniswap V3/V4 concepts used in Doppler development, including tick math, sqrtPriceX96, concentrated liquidity formulas, and V4 hooks/singleton architecture.

Use this Skill: https://skilld.dev/gh/whetstoneresearch/doppler-skills/uniswap-fundamentals

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesTICK-MATH.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Tick Math

Mathematical foundations for price representation in Uniswap V3 and V4.


Q Notation (Fixed-Point Arithmetic) [V3+V4]

Variables ending in X96 or X128 use Q notation for fixed-point math.

Conversion:

actualValue = qValue ÷ 2^k

Where k is the number after "X" (e.g., X96 means divide by 2^96).

Example:

sqrtPrice = sqrtPriceX96 ÷ 2^96

Purpose: Allows integers to function like floating-point numbers while preserving precision across 256-bit representations.

Source: Uniswap V3 Math Primer


sqrtPriceX96 [V3+V4]

The primary price representation in both V3 and V4.

What it stores: The square root of the price, scaled by 2^96.

Why square root: Gas efficiency - many AMM calculations involve square roots, so storing it directly avoids repeated computation.

Conversion to price:

sqrtPrice = sqrtPriceX96 ÷ 2^96
price = sqrtPrice²

Combined formula:

price = (sqrtPriceX96 ÷ 2^96)²

Price meaning: price represents how many token1 you get for 1 token0.

Source: Uniswap V3 Math Primer


Tick System [V3+V4]

Ticks discretize the price space for gas-efficient storage.

Tick-to-Price Formula

price = 1.0001^tick

Each tick represents a 0.01% (1 basis point) price change.

Price-to-Tick Formula

tick = floor(log(price) / log(1.0001))

Note: This produces a fractional result; the protocol stores the floor.

From sqrtPriceX96 to Tick

tick = floor(log((sqrtPriceX96 ÷ 2^96)²) / log(1.0001))

Precision warning: sqrtPriceX96 retains more precision than integer tick values. Converting tick→price→tick may not round-trip exactly.

Source: Uniswap V3 Math Primer


Tick Spacing [V3] vs [V4]

V3: Fixed Tick Spacing by Fee Tier

In V3, tick spacing is determined by the pool's fee tier:

Fee Tier Tick Spacing Price Step per Tick
0.01% 1 ~0.01%
0.05% 10 ~0.10%
0.30% 60 ~0.60%
1.00% 200 ~2.00%

V3 constraint: Position boundaries must align to tick spacing multiples.

Source: Uniswap V3 Math Primer

V4: Dynamic Tick Spacing

In V4, tick spacing is:

  • Set per pool during initialization
  • Independent of fee tier (fees can be dynamic)
  • Maximum of 32767 (int16 max, though practical limits are lower)

V4 allows: Same tick spacing with different fee configurations.

Source: Uniswap V4 Docs


Tick Alignment [V3+V4]

Positions must have tick boundaries aligned to tick spacing.

Basic Alignment Formula

alignedTick = floor(tick / tickSpacing) * tickSpacing

Direction-Aware Alignment (Doppler Pattern)

The rounding direction depends on which token is being sold:

When selling token0 (isToken0 = true): Round DOWN

if (tick < 0) {
    return (tick - tickSpacing + 1) / tickSpacing * tickSpacing;
} else {
    return tick / tickSpacing * tickSpacing;
}

When selling token1 (isToken0 = false): Round UP

if (tick < 0) {
    return tick / tickSpacing * tickSpacing;
} else {
    return (tick + tickSpacing - 1) / tickSpacing * tickSpacing;
}

Why: Ensures positions are aligned in the direction that favors the protocol (conservative rounding).


Decimal Adjustment [V3+V4]

Raw prices must be adjusted for token decimals.

Formula:

adjustedPrice = rawPrice ÷ 10^(token1Decimals - token0Decimals)

Example: USDC (6 decimals) / WETH (18 decimals)

adjustedPrice = rawPrice ÷ 10^(18 - 6) = rawPrice ÷ 10^12

Inverse price:

inversePrice = 1 ÷ adjustedPrice

Source: Uniswap V3 Math Primer


Token Ordering [V2+V3+V4]

Universal rule: token0 < token1 (compared as addresses).

Impact on Calculations

If your asset is... isToken0 Tick increases mean...
token0 true Asset price DECREASES (in token1 terms)
token1 false Asset price INCREASES (in token0 terms)

Impact on Doppler

All Doppler auction types must track isToken0 and flip logic accordingly:

  • Tick direction for price movement
  • Rounding direction for alignment
  • Far tick comparison direction

Common Library Functions [V3+V4]

TickMath (from Uniswap)

// Get sqrt price from tick
uint160 sqrtPriceX96 = TickMath.getSqrtPriceAtTick(tick);

// Get tick from sqrt price
int24 tick = TickMath.getTickAtSqrtPrice(sqrtPriceX96);

Bounds:

  • MIN_TICK = -887272
  • MAX_TICK = 887272
  • MIN_SQRT_PRICE = 4295128739
  • MAX_SQRT_PRICE = 1461446703485210103287273052203988822378723970342

Tick Validity

// Valid tick range
require(tick >= TickMath.MIN_TICK && tick <= TickMath.MAX_TICK);

// Aligned to spacing
require(tick % tickSpacing == 0);

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill serves as a mathematical and architectural reference for Uniswap V3 and V4 protocols, specifically tailored for the Doppler project. It contains documentation on liquidity formulas, tick math, and V4 hook lifecycles. All external links point to official documentation or the author's own code repository.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer8mo

    5 files scanned · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4f654fe. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 9 months ago
metadata
{
  "author": "uniswap",
  "version": "2.0",
  "source": "docs.uniswap.org"
}

README badge

README badge for whetstoneresearch/doppler-skills/uniswap-fundamentals