How DEX Prices Work: AMM Formulas, Liquidity Pools, and Price Calculations Explained
How DEX Prices Work: AMM Formulas, Liquidity Pools, and Price Calculations Explained
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):
- Buyers place orders: "I want to buy 1 SOL at $100"
- Sellers place orders: "I want to sell 1 SOL at $101"
- When prices match, a trade happens
- This is called an order book
- Liquidity providers deposit tokens into a pool
- An algorithm calculates the price based on pool balances
- Traders swap directly with the pool
- The algorithm adjusts prices after each trade
- 1,000 SOL
- 200,000 USDC
- Swap SOL for USDC
- Swap USDC for SOL
- 200,000 USDC ÷ 1,000 SOL = 200 USDC per SOL
- Deposit pairs of tokens into pools
- Earn fees from trades
- Take on the risk of impermanent loss
- x = amount of Token A in the pool
- y = amount of Token B in the pool
- k = a constant number that never changes
- x = 1,000 SOL
- y = 200,000 USDC
- k = 1,000 × 200,000 = 200,000,000
- SOL: 1,000
- USDC: 200,000
- k: 200,000,000
- 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
- 202,020.20 - 200,000 = 2,020.20 USDC for 10 SOL
- Effective price: 202.02 USDC per SOL
- Small trades = small price impact
- Large trades = large price impact
- The price you expect
- The price you actually get
- Price impact from your trade size
- Other trades happening before yours
- Network delays changing pool state
- "I'm okay if the price is up to 1% worse than quoted"
- If it's worse than that, cancel my transaction
- SOL trades between $150-$250 most of the time
- Liquidity at $0.01 or $10,000 is wasted
- You choose a minimum price (e.g., $150)
- You choose a maximum price (e.g., $250)
- Your liquidity is concentrated in this range
- You earn more fees when trades happen in your range
- More liquidity at common prices
- Better prices and less slippage
- More efficient markets
- 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
- Simple and reliable
- Liquidity spread across all prices
- Higher slippage for large trades
- Zero slippage within range
- Breaks if prices diverge
- Not used alone in practice
- Very low slippage for stable pairs
- More complex math
- Optimal for correlated assets
- Capital efficient
- Better prices for traders
- Requires active LP management
- Uses constant product formula
- Liquidity spread across all prices
- Simple for LPs
- LPs choose price ranges
- Higher capital efficiency
- Better prices for traders
- Price ticks define liquidity ranges
- Liquidity concentrated where it matters
- NFT represents LP positions
- Each tick represents a discrete price point
- Liquidity is assigned to tick ranges
- Swaps cross ticks as price moves
- Current price: $200 per SOL
- Your liquidity: $180 - $220 range
- You earn fees only when price is in your range
- Uses "bins" instead of ticks
- Price changes in discrete steps
- Dynamic fees based on volatility
- Traditional constant product pools
- Optimized for stable pairs
- Built-in fee management
- For token launches
- Price increases as supply decreases
- Fair launch mechanism
- Token starts at a low price
- As people buy, price increases algorithmically
- Fixed supply released over the curve
- Price is determined by total amount bought
- 500 SOL
- 125,000 USDC
- Find the current active tick
- Calculate virtual reserves within the range
- Apply the formula within that range
- You want to swap 100 SOL for USDC
- Jupiter checks prices on:
- Raydium pools
- Orca pools
- Meteora pools
- Many others
- It might split your trade:
- 60 SOL through Raydium
- 30 SOL through Orca
- 10 SOL through Meteora
- You get the best overall price
- Better prices - finds the best rates
- Lower slippage - splits large orders
- One transaction - handles complexity
- 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
- Every swap has a price
- Data providers collect all swaps
- They organize into time periods (1m, 5m, 1h, etc.)
- Charts display this data
- On-chain: Reading pool reserves directly
- Aggregators: DexScreener, GeckoTerminal, Birdeye
- APIs: Jupiter, Raydium, Orca APIs
- AMMs use formulas - Not order books, but math
- x*y=k is fundamental - Most AMMs derive from this
- Bigger trades = worse prices - Price impact matters
- Concentrated liquidity is more efficient - CLMM pools offer better prices
- Aggregators find best prices - Use Jupiter for optimal rates
- Pool reserves determine price - Simple ratio calculation
- Jupiter - Best price aggregation
- DexScreener - Real-time DEX data
- GeckoTerminal - On-chain analytics
- Birdeye - Solana token data
The price is determined by what buyers and sellers are willing to pay.
How DEXs Work
Decentralized exchanges don't have order books. Instead:
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:
This pool allows anyone to:
The ratio of tokens in the pool determines the price.
In this case:
Who Provides Liquidity?
Liquidity Providers (LPs) are people who:
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:
How It Works
Let's use our SOL/USDC pool:
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:
After you take 10 SOL:
You pay:
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.
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 Size | You Get | Effective Price | Price Impact |
|---|---|---|---|
| 1 SOL | 199.80 USDC | $199.80 | 0.1% |
| 10 SOL | 1,980.20 USDC | $198.02 | 1.0% |
| 50 SOL | 9,523.81 USDC | $190.48 | 4.8% |
| 100 SOL | 18,181.82 USDC | $181.82 | 9.1% |
The more you buy, the worse your average price.
What is Slippage?
Slippage is the difference between:
Slippage can occur because:
Slippage Tolerance
When you set slippage tolerance to 1%, you're saying:
🎯 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:
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:
Benefits
For traders:
For LPs:
Popular CLMMs on Solana
🔢 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:
2. Constant Sum (x + y = k)
Used by: Some stablecoin pools
Best for: Assets that should be 1:1
Formula: x + y = k
Characteristics:
3. StableSwap (Curve Formula)
Used by: Saber, Mercurial
Best for: Stablecoin-to-stablecoin swaps
Formula: Hybrid of constant product and constant sum
Characteristics:
4. Concentrated Liquidity
Used by: Orca, Raydium CLMM, Meteora DLMM
Best for: Most trading pairs
Formula: Modified x*y=k within price ranges
Characteristics:
🌊 How Solana DEXs Calculate Prices
Raydium
Raydium offers two pool types:
Legacy AMM Pools (CPMM):
Concentrated Liquidity (CLMM):
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:
How Price Works:
Example:
Meteora
Meteora offers multiple AMM types:
DLMM (Dynamic Liquidity Market Maker):
DAMM (Dynamic AMM):
DBC (Dynamic Bonding Curve):
Pumpfun AMM
Pumpfun uses a bonding curve:
How It Works:
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:
Price of SOL:
125,000 USDC / 500 SOL = 250 USDC per SOL
For Concentrated Liquidity
The calculation is more complex:
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
Why Aggregators Help
📊 Price vs OHLCV Data
What is OHLCV?
OHLCV stands for:
Where Does OHLCV Come From?
OHLCV data is aggregated from individual trades:
Data Sources
🎓 Key Takeaways
🔗 Learn More
Solana DEXs
DEX Aggregators
Price Data
📖 Glossary
| Term | Definition |
|---|---|
| AMM | Automated Market Maker - algorithm determining prices |
| Liquidity Pool | Smart contract holding tokens for trading |
| Constant Product | x*y=k formula for price calculation |
| CLMM | Concentrated Liquidity Market Maker |
| Slippage | Difference between expected and actual price |
| Price Impact | How your trade moves the market price |
| LP | Liquidity Provider - deposits tokens to pools |
| OHLCV | Open/High/Low/Close/Volume price data |
| DEX Aggregator | Service 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! 🚀