Section 3 of 18

Build
+15 Lynx

SqrtPriceMath: Amounts from Price Ranges

Key takeaway: SqrtPriceMath is the engine room of V3: given liquidity L and two sqrt prices, getAmount0Delta computes Δx = L * (sqrtPb - sqrtPa) / (sqrtPb * sqrtPa) and getAmount1Delta computes Δy = L * (sqrtPb - sqrtPa); given an amount, the getNextSqrtPrice* functions invert those formulas to find where the price lands. Every function takes an explicit rounding direction, and the direction is a security property: amounts the user owes the pool round up, amounts the pool pays out round down, and price movement rounds so the swap never overshoots its target. Get one direction wrong and the pool leaks a wei per call — which an attacker will happily loop.

What You Are Building

The full SqrtPriceMath library: both amount-delta functions (with explicit rounding), their signed wrappers, and the four next-price functions the swap loop will call in Part 2. Provided at the top of the starter: FullMath (512-bit mulDiv, so L * price products can't overflow), UnsafeMath.divRoundingUp, FixedPoint96, and SafeCast. Everything below the "Your code" marker is yours.

Your Code

Solution.sol
Solidity
Loading editor...

Requirements

Write your implementation, then click Run Tests. Tests execute on the server.

Sign up free — keep reading + earn 15 Lynx

Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.

Sign up free to continue

Already have an account? Log in