Education

How DEX Prices Work: AMM Formulas and Liquidity Pools

How Raydium, Orca and Meteora actually price a swap: the constant product formula, concentrated liquidity, and why your quote moves as you trade.

How DEX Prices Work: AMM Formulas and Liquidity Pools article cover
In this article
  1. 🎯 What You'll Learn
  2. 📊 Traditional Exchanges vs DEXs
  3. 🏊 What is a Liquidity Pool?
  4. ⚡ The Constant Product Formula (x * y = k)
  5. 📉 Understanding Slippage and Price Impact
  6. 🎯 Concentrated Liquidity (CLMM)
  7. 🔢 Different AMM Formulas
  8. 🌊 How Solana DEXs Calculate Prices
  9. 📐 Calculating Price from Pool Reserves
  10. 💱 How Aggregators Find Best Prices
  11. 📊 Price vs OHLCV Data
  12. 🎓 Key Takeaways
  13. 🔗 Learn More
  14. 📖 Glossary

Have you ever wondered how decentralized exchanges determine the price when you swap tokens? Unlike traditional exchanges with order books, DEXs use mathematical formulas to calculate prices automatically.

This guide explains how these Automated Market Makers (AMMs) work, from basic concepts to the math behind them, all in terms anyone can understand.


🎯 What You'll Learn

  • What liquidity pools are and how they work
  • The famous x*y=k formula explained simply
  • How different AMM types calculate prices
  • Why slippage happens on DEXs
  • How Solana's major DEXs (Raydium, Orca, Meteora) differ

📊 Traditional Exchanges vs DEXs

How Traditional Exchanges Work

On a traditional exchange (like the stock market or centralized crypto exchanges):

  1. Buyers place orders: "I want to buy 1 SOL at $100"
  2. Sellers place orders: "I want to sell 1 SOL at $101"
  3. When prices match, a trade happens
  4. This is called an order book

The price is determined by what buyers and sellers are willing to pay.

How DEXs Work

Decentralized exchanges don't have order books. Instead:

  1. Liquidity providers deposit tokens into a pool
  2. An algorithm calculates the price based on pool balances
  3. Traders swap directly with the pool
  4. The algorithm adjusts prices after each trade

This algorithm is called an Automated Market Maker (AMM).


🏊 What is a Liquidity Pool?

A liquidity pool is a smart contract that holds two (or more) tokens.

Example: SOL/USDC Pool

Imagine a pool containing:

  • 1,000 SOL
  • 200,000 USDC

This pool allows anyone to:

  • Swap SOL for USDC
  • Swap USDC for SOL

The ratio of tokens in the pool determines the price.

In this case:

  • 200,000 USDC ÷ 1,000 SOL = 200 USDC per SOL

Who Provides Liquidity?

Liquidity Providers (LPs) are people who:

  1. Deposit pairs of tokens into pools
  2. Earn fees from trades
  3. Take on the risk of impermanent loss

Without LPs, there would be no liquidity to trade with.


⚡ The Constant Product Formula (x * y = k)

The most famous AMM formula is the constant product formula, invented by Uniswap.

The Formula

x * y = k

Where:

  • x = amount of Token A in the pool
  • y = amount of Token B in the pool
  • k = a constant number that never changes

How It Works

Let's use our SOL/USDC pool:

  • x = 1,000 SOL
  • y = 200,000 USDC
  • k = 1,000 × 200,000 = 200,000,000

This "k" value must stay constant before and after every trade.

Calculating a Swap

You want to buy 10 SOL from the pool. Here's how the math works:

Before the trade:

  • SOL: 1,000
  • USDC: 200,000
  • k: 200,000,000

After you take 10 SOL:

  • SOL: 990 (1,000 - 10)
  • The pool needs enough USDC to maintain k = 200,000,000
  • USDC needed: 200,000,000 ÷ 990 = 202,020.20

You pay:

  • 202,020.20 - 200,000 = 2,020.20 USDC for 10 SOL
  • Effective price: 202.02 USDC per SOL

Notice the price (202.02) is higher than the initial price (200.00). This is called price impact.


📉 Understanding Slippage and Price Impact

What is Price Impact?

Price impact is how much your trade moves the market price.

  • Small trades = small price impact
  • Large trades = large price impact

The bigger your trade relative to the pool size, the worse your price.

Example: Different Trade Sizes

Using our 1,000 SOL / 200,000 USDC pool:

Trade SizeYou GetEffective PricePrice Impact
1 SOL199.80 USDC$199.800.1%
10 SOL1,980.20 USDC$198.021.0%
50 SOL9,523.81 USDC$190.484.8%
100 SOL18,181.82 USDC$181.829.1%

The more you buy, the worse your average price.

What is Slippage?

Slippage is the difference between:

  • The price you expect
  • The price you actually get

Slippage can occur because:

  1. Price impact from your trade size
  2. Other trades happening before yours
  3. Network delays changing pool state

Slippage Tolerance

When you set slippage tolerance to 1%, you're saying:

  • "I'm okay if the price is up to 1% worse than quoted"
  • If it's worse than that, cancel my transaction

🎯 Concentrated Liquidity (CLMM)

The Problem with x*y=k

The constant product formula spreads liquidity across all prices (from $0 to $∞).

But most trading happens in a narrow range:

  • SOL trades between $150-$250 most of the time
  • Liquidity at $0.01 or $10,000 is wasted

The Solution: Concentrated Liquidity

Concentrated Liquidity Market Makers (CLMMs) allow LPs to provide liquidity in specific price ranges.

How It Works

Instead of providing liquidity for all prices:

  1. You choose a minimum price (e.g., $150)
  2. You choose a maximum price (e.g., $250)
  3. Your liquidity is concentrated in this range
  4. You earn more fees when trades happen in your range

Benefits

For traders:

  • More liquidity at common prices
  • Better prices and less slippage
  • More efficient markets

For LPs:

  • Higher fee earnings per dollar deposited
  • Capital efficiency up to 4,000x better
  • Active management opportunities
  • Orca Whirlpools - The original Solana CLMM
  • Raydium CLMM - High-volume concentrated pools
  • Meteora DLMM - Dynamic liquidity with price bins

🔢 Different AMM Formulas

1. Constant Product (x * y = k)

Used by: Raydium Legacy, early Uniswap

Best for: General-purpose trading

Formula: x * y = k

Characteristics:

  • Simple and reliable
  • Liquidity spread across all prices
  • Higher slippage for large trades

2. Constant Sum (x + y = k)

Used by: Some stablecoin pools

Best for: Assets that should be 1:1

Formula: x + y = k

Characteristics:

  • Zero slippage within range
  • Breaks if prices diverge
  • Not used alone in practice

3. StableSwap (Curve Formula)

Used by: Saber, Mercurial

Best for: Stablecoin-to-stablecoin swaps

Formula: Hybrid of constant product and constant sum

Characteristics:

  • Very low slippage for stable pairs
  • More complex math
  • Optimal for correlated assets

4. Concentrated Liquidity

Used by: Orca, Raydium CLMM, Meteora DLMM

Best for: Most trading pairs

Formula: Modified x*y=k within price ranges

Characteristics:

  • Capital efficient
  • Better prices for traders
  • Requires active LP management

🌊 How Solana DEXs Calculate Prices

Raydium

Raydium offers two pool types:

Legacy AMM Pools (CPMM):

  • Uses constant product formula
  • Liquidity spread across all prices
  • Simple for LPs

Concentrated Liquidity (CLMM):

  • LPs choose price ranges
  • Higher capital efficiency
  • Better prices for traders

Price Calculation Example (Legacy):

Price = Token B in pool / Token A in pool
Price of SOL = USDC in pool / SOL in pool

Orca

Orca uses Whirlpools (CLMM):

Key Features:

  • Price ticks define liquidity ranges
  • Liquidity concentrated where it matters
  • NFT represents LP positions

How Price Works:

  • Each tick represents a discrete price point
  • Liquidity is assigned to tick ranges
  • Swaps cross ticks as price moves

Example:

  • Current price: $200 per SOL
  • Your liquidity: $180 - $220 range
  • You earn fees only when price is in your range

Meteora

Meteora offers multiple AMM types:

DLMM (Dynamic Liquidity Market Maker):

  • Uses "bins" instead of ticks
  • Price changes in discrete steps
  • Dynamic fees based on volatility

DAMM (Dynamic AMM):

  • Traditional constant product pools
  • Optimized for stable pairs
  • Built-in fee management

DBC (Dynamic Bonding Curve):

  • For token launches
  • Price increases as supply decreases
  • Fair launch mechanism

Pumpfun AMM

Pumpfun uses a bonding curve:

How It Works:

  1. Token starts at a low price
  2. As people buy, price increases algorithmically
  3. Fixed supply released over the curve
  4. Price is determined by total amount bought

Formula (simplified):

Price = Base Price × (1 + Tokens Sold / Curve Constant)

📐 Calculating Price from Pool Reserves

For Constant Product AMMs

To calculate the current price of Token A in terms of Token B:

Price of A = Reserve of B / Reserve of A

Example

Pool contains:

  • 500 SOL
  • 125,000 USDC

Price of SOL:

125,000 USDC / 500 SOL = 250 USDC per SOL

For Concentrated Liquidity

The calculation is more complex:

  1. Find the current active tick
  2. Calculate virtual reserves within the range
  3. Apply the formula within that range

Most traders use DEX APIs rather than calculating manually.


💱 How Aggregators Find Best Prices

What is a DEX Aggregator?

A DEX aggregator (like Jupiter) finds the best price across multiple DEXs.

How It Works

  1. You want to swap 100 SOL for USDC
  2. Jupiter checks prices on:
    • Raydium pools
    • Orca pools
    • Meteora pools
    • Many others
  3. It might split your trade:
    • 60 SOL through Raydium
    • 30 SOL through Orca
    • 10 SOL through Meteora
  4. You get the best overall price

Why Aggregators Help

  • Better prices - finds the best rates
  • Lower slippage - splits large orders
  • One transaction - handles complexity

📊 Price vs OHLCV Data

What is OHLCV?

OHLCV stands for:

  • Open - Price at start of period
  • High - Highest price in period
  • Low - Lowest price in period
  • Close - Price at end of period
  • Volume - Total traded in period

Where Does OHLCV Come From?

OHLCV data is aggregated from individual trades:

  1. Every swap has a price
  2. Data providers collect all swaps
  3. They organize into time periods (1m, 5m, 1h, etc.)
  4. Charts display this data

Data Sources

  • On-chain: Reading pool reserves directly
  • Aggregators: DexScreener, GeckoTerminal, Birdeye
  • APIs: Jupiter, Raydium, Orca APIs

🎓 Key Takeaways

  1. AMMs use formulas - Not order books, but math
  2. x*y=k is fundamental - Most AMMs derive from this
  3. Bigger trades = worse prices - Price impact matters
  4. Concentrated liquidity is more efficient - CLMM pools offer better prices
  5. Aggregators find best prices - Use Jupiter for optimal rates
  6. Pool reserves determine price - Simple ratio calculation

🔗 Learn More

Solana DEXs

DEX Aggregators

Price Data


📖 Glossary

TermDefinition
AMMAutomated Market Maker - algorithm determining prices
Liquidity PoolSmart contract holding tokens for trading
Constant Productx*y=k formula for price calculation
CLMMConcentrated Liquidity Market Maker
SlippageDifference between expected and actual price
Price ImpactHow your trade moves the market price
LPLiquidity Provider - deposits tokens to pools
OHLCVOpen/High/Low/Close/Volume price data
DEX AggregatorService finding best prices across DEXs

Understanding how DEX prices work gives you an edge in trading. Now you know why big trades get worse prices and how to minimize slippage! 🚀

View all research