How to Build a Concentrated Liquidity AMM: A Protocol Engineer's Guide
Learn how to design and implement a concentrated liquidity AMM with tick-based pricing, range orders, and capital efficiency optimizations for production DeFi.
Most teams trying to ship a DEX underestimate how much the gap between a basic x*y=k pool and a production-grade concentrated liquidity AMM actually costs them. A naive constant-product pool wastes 80–95% of deposited capital in ranges that never trade. If you are building a DEX in 2026 and capital efficiency matters, this guide walks through what a concentrated liquidity AMM actually requires at the engineering level.
What Concentrated Liquidity AMMs Change About Protocol Design
In a standard constant-product AMM, liquidity is spread uniformly across all possible prices from zero to infinity. A concentrated liquidity AMM lets LPs deposit capital only within a chosen price range. Capital that sits outside the active tick earns nothing but also does not get depleted, so LPs can target tighter spreads and earn more fees per dollar deposited.
The tradeoff is implementation complexity. Your protocol now needs to track:
- A tick system — a discrete grid of price points
- Per-tick liquidity deltas — how much liquidity enters or exits as the price crosses each tick
- Fee growth accumulators — per-tick and global snapshots to calculate LP earnings across arbitrary ranges
- Position NFTs or bitmap indexes — to identify which ticks are initialized
Each of these introduces surface area for bugs. Getting tick math wrong by even one integer can silently miscalculate swap outputs or drain LP positions.
Core Data Structures You Need
The Tick
Every tick stores:
liquidityGross— total liquidity referencing this tick (used to know when to clear it)liquidityNet— the signed delta applied when price crosses this tickfeeGrowthOutside0X128andfeeGrowthOutside1X128— fee accumulators relative to this tick's outside
The fee growth values are the trickiest part. They are stored as 128-bit fixed-point numbers and require careful handling during cross-tick events to avoid overflow or mis-attribution of fees.
The Position
A position is defined by a specific LP address and a lower/upper tick pair. It stores:
liquidity— the amount of liquidity the LP provided in rangefeeGrowthInside0LastX128— a snapshot taken the last time fees were collectedtokensOwed0andtokensOwed1— accrued but uncollected fees
When an LP collects fees, you calculate the difference between the current inside fee growth and the snapshot, then multiply by their liquidity.
The Global State
At the pool level you maintain:
sqrtPriceX96— the current price as a square root in Q64.96 fixed-pointtick— the current active tick indexliquidity— active liquidity at the current pricefeeGrowthGlobal0X128andfeeGrowthGlobal1X128
The Swap Loop
Swaps step through ticks one segment at a time. For each step:
- Determine the next initialized tick in the direction of the swap.
- Compute how much of the remaining input fills the current segment without crossing that tick.
- If the input is exhausted, update
sqrtPriceX96and stop. - If the tick is crossed, apply the
liquidityNetdelta, flip fee accumulators, and continue.
This loop can cross many ticks in a single transaction, which is why gas costs on concentrated liquidity pools scale with the number of ticks crossed rather than being flat.
Best Practices for Concentrated Liquidity AMM Development
Use Q notation explicitly. All price and fee math should happen in well-documented fixed-point types. Document the precision of every variable and test edge cases around the maximum and minimum tick values.
Write invariant tests. Before deployment, define protocol invariants — for example, the sum of all liquidityNet values at all ticks must equal zero at all times. Run fuzz tests that execute arbitrary sequences of mint, burn, and swap operations and assert these invariants hold.
Test tick boundary conditions. The most common bug class involves behavior exactly at tick boundaries: when sqrtPriceX96 lands exactly on a tick, which side does the tick belong to? Get this wrong and you get a one-wei discrepancy that compounds into significant fund loss at scale.
Implement a fee protocol from the start. Even if you set the protocol fee to zero initially, building in the collection mechanism avoids a redeployment later. The Uniswap V3 fee switch is a good reference implementation.
Handle rounding direction deliberately. When rounding token amounts, always round in the direction that protects the protocol rather than the user. For amounts going out, round down. For amounts coming in, round up. Inconsistent rounding is a frequent source of audit findings.
Liquidity Math: The Core Formula
Given a price range from sqrtPriceLower to sqrtPriceUpper and a liquidity value L, the token amounts are:
amount0 = L * (sqrtPriceUpper - sqrtPriceCurrent) / (sqrtPriceCurrent * sqrtPriceUpper)amount1 = L * (sqrtPriceCurrent - sqrtPriceLower)
These formulas must handle the case where current price is outside the range — in that case, only one token is held by the position, not both.
What to Audit Before Launch
A concentrated liquidity AMM has a larger audit scope than a basic pool. Ensure your audit covers:
- Tick crossing logic under adversarial conditions (flash loans, sandwiching)
- Fee accumulator correctness across position lifecycle (mint, partial burn, collect, full burn)
- Reentrancy in the swap and mint callbacks
- Integer overflow and underflow in all fixed-point arithmetic
- Behavior at maximum and minimum representable prices
Most credible auditors will want 4–6 weeks on a pool of this complexity. Budget accordingly.
When Not to Build Concentrated Liquidity from Scratch
If your use case is stablecoin-to-stablecoin or similarly-priced asset swaps, a StableSwap invariant (as used by Curve) is often a better fit than tick-based concentrated liquidity. The math is simpler, gas costs are lower, and slippage characteristics are superior for pegged assets. Pick the invariant that matches your asset pairs before committing to an implementation.
If you are building a DEX for your protocol's native token alongside a stablecoin pair, a hybrid pool or a single-sided liquidity bootstrapping pool may serve the launch phase better than a full concentrated liquidity implementation.
Building a production AMM requires more than translating whitepaper math into Solidity. It requires careful architecture, adversarial testing, and operational readiness. Start a build with Clixo if you want a team that has shipped DeFi protocol infrastructure and knows where the real complexity lives.