In a previous article, I shared how I started building an arbitrage bot on Solana, using liquidity from decentralized exchanges (DEX) and executing atomic multi-hop swaps.
Many readers asked one simple but critical question: Where does the price on a DEX actually come from? Who sets it, why does it change, and what exactly is slippage?
This article breaks down the core pricing mechanics in DEX liquidity pools, using clear language and real examples—no screenshots or fluff.
On centralized exchanges (CEX) like Binance or OKX, prices are derived from order books. Buyers and sellers submit orders, and the exchange matches them. The last matched trade sets the current market price.
But on a DEX, there's no centralized order book (unless it's a DEX like OpenBook). Instead, trades happen via liquidity pools, and prices are determined algorithmically.
A liquidity pool is a smart contract that holds two tokens—say, SOL and USDC. Anyone can trade one for the other, and the exchange rate is calculated from the token balances in the pool.
There are different types of pricing models used in these pools:
Within AMMs, we see three main models:
CPMM (Constant Product Market Maker)
Used in Raydium AMM v4, Uniswap v2
Formula: X * Y = K
CLMM (Concentrated Liquidity)
Used in Raydium CLMM
Liquidity is allocated to specific price ranges (ticks)
DLMM (Dynamic Liquidity)
Used in Meteora
Liquidity dynamically follows price trends
Let's explore CPMM in depth.
At the heart of CPMM is this elegant formula:
X * Y = K
Where:
X
= amount of token A in the pool (e.g. SOL)Y
= amount of token B (e.g. USDC)K
= a constant value (product of the two token amounts)This relationship must remain constant after each swap. So when you swap tokens, you change the balance of X and Y—but the product K
stays the same.
Let's say the pool holds:
K = 1,000 * 100,000 = 100,000,000
You want to swap 1 SOL for USDC. After adding your SOL:
X = 1,001
Y = K / X = 100,000,000 / 1,001 ≈ 99,900.1
So you receive:
100,000 - 99,900.1 = 99.9 USDC
Your effective price is slightly less than 1 SOL = 100 USDC — this is slippage.
Same pool, but now you swap 100 SOL:
X = 1,100
Y = 100,000,000 / 1,100 ≈ 90,909.1
You receive:
100,000 - 90,909.1 = 9,090.9 USDC
Now your effective price is 1 SOL ≈ 90.91 USDC — about 9% slippage due to swap size.
Most AMMs take a swap fee (e.g. 0.3%). Usually, this is deducted from the input amount before the swap calculation.
Let's say the fee is 10% (just for illustration):
Recalculating:
X = 1,009
Y = 100,000,000 / 1,009 ≈ 99,108.0
AmountOut = 100,000 - 99,108.0 = 892.0 USDC
You receive less because of the fee and slippage combined.
Swapping again after the first trade gives worse results. For example:
Why? The pool becomes unbalanced: more SOL, less USDC. The formula keeps adjusting the price downward for SOL.
This formula is the foundation for arbitrage strategies: if one pool is mispriced compared to another, bots can profit by correcting the difference.
Understanding the core formula X * Y = K
is essential for anyone building or trading on Solana DEXs. Raydium, Orca, and Meteora all use variations of this model, each with different liquidity behaviors and pricing precision.
If you're writing bots or designing trading strategies, always simulate the swap math before execution — it can make or break your edge.
Want a breakdown of CLMM and DLMM pricing logic? Let me know and I'll dive into those next.