
In my previous article, I shared a high-level breakdown of how I built my own arbitrage bot on Solana — leveraging liquidity from decentralized exchanges (DEXs) and executing atomic multihop swaps.
After publishing it, I got a bunch of comments and DMs with the same core question:
Where does the price on a DEX actually come from?
Why does it move? Who sets it? And what’s up with slippage?
So in this article, I want to go deeper and explain how pricing works inside decentralized liquidity pools. No fluff — just clear examples and simple explanations.
If you’re curious to dig into the math and mechanics under the hood, let me know in the comments — happy to explore that in follow-ups.
Let’s start by quickly revisiting how price formation works on centralized exchanges (CEXs), and then contrast it with DEXs.
If you want to see more of my posts, insights, and real experiences from trading, building bots, and experimenting with Solana DeFi — join my Telegram channel. I share shorter, more frequent updates there than on the blog.
On CEXs like Binance, Bybit, or OKX, the price is determined through orders. One person wants to buy, another wants to sell — and the exchange simply matches their orders.
Example:
Imagine a vertical Y-axis, where the top represents high prices and the bottom — low prices. That’s how we visualize the order book.
Sellers always want to sell at higher prices, so in the sell side of the book:
Buyers, on the other hand, want to buy cheaper, so in the buy side:
Below is an example of how it looks in practice.

For example:
Each row in both sides of the order book represents orders like this — just like the one we broke down.

To make it clearer, let’s imagine flipping the BUY order book upside down (like a mirror reflection)
and placing it under the bottom of the SELL order book.
This gives us the classic picture with a spread in the middle — showing how the buy and sell prices converge.

All of this makes up the order book — a table where the sell orders are on top, and the buy orders are at the bottom. They don’t align perfectly, and the gap between them is called the spread.
You go to Binance and want to buy 1 SOL.
Prices on a CEX change constantly — not because someone is “setting” the price, but because people place orders: someone buys, someone sells, and that creates the new price.
It’s like an auction — the price is whatever people are willing to pay.
The exchange doesn’t set the price itself — it simply matches orders between participants.
Unlike centralized exchanges where prices are set by matching buy and sell orders, decentralized exchanges (DEXs) work differently — through liquidity pools.
A liquidity pool is a smart contract holding two tokens, like SOL and USDC.
Anyone can come and swap one for the other — at a rate determined by the current ratio of tokens inside the pool.
If we simplify things to the most fundamental level, almost every DEX uses one of two core pricing models under the hood:
Example: OpenBook (formerly Serum)
And this is where things get more interesting.
Instead of using orders, the price is based purely on the token balances in the pool — and a formula that calculates the exchange rate.
This includes several variations:
CPMM (Constant Product Market Maker)
CLMM (Concentrated Liquidity Market Maker)
DLMM (Dynamic Liquidity Market Maker)
Instead of matching buyers and sellers like in an order book, AMMs rely on token balances in a liquidity pool and a formula to determine price. There are no limit orders — just math and assets locked in a smart contract.
At the core of this model lies a simple yet powerful formula:
X * Y = K
Where:
This formula ensures that as one token is bought, its price increases relative to the other, maintaining balance in the pool.
Let’s say you want to swap SOL → USDC.
That’s why larger swaps suffer from higher slippage — it’s not a fee, it’s just how AMMs naturally work.
Let’s say the pool contains:
That means:
K = 1000 × 100,000 = 100,000,000
You want to swap 1 SOL for USDC.
After the swap, the new SOL balance becomes X = 1001.
To keep K constant, we calculate the new USDC balance:
Y = K / X = 100,000,000 / 1001 ≈ 99,900.1
So:
Effective rate: 1 SOL = 99.9 USDC
(instead of exactly 100 — you get a tiny bit less due to slippage)
Let’s take the same pool:
Now you want to swap 100 SOL for USDC.
The new SOL balance becomes X = 1100
Calculate the new USDC balance:
Y = K / X = 100,000,000 / 1100 ≈ 90,909.1
So in the pool:
Effective rate:
100 SOL = 9,090.9 USDC → 1 SOL ≈ 90.91 USDC
That’s a much worse rate than before — you’ve shifted the balance heavily, so the price dropped hard. That’s slippage in action.
To demonstrate how everything works in practice, I created a test pool on devnet and will walk through how the calculation is done — and how closely it matches reality.
I minted two test tokens for myself, which I’ll use as paired assets in the pool.
For this experiment, I set up a pool with 100 tokens of each type — Token X and Token Y.
In the screenshot you can see:
Now let’s see what happens when we try to swap.

Recalling the classic AMM formula:
X × Y = K — must remain constant.
Without fees, here’s the basic math:
Initial state of the pool:
You add 10 tokens of X:
Now calculate new Y:
Amount you receive:
🔸 So, without any fee, you’d get ~9.09 Y tokens in return for 10 X.
Next up: we’ll factor in the swap fee.
Most AMMs apply the fee on the input token, so here’s how it works:
You swap 10 X, but 10% goes to fees:
New pool state:
Final output:
🔺 So you receive 8.26 Y instead of 9.09 Y.
That difference — 0.83 Y — is the effective cost of the 10% swap fee.

Important:
This is not universal behavior — different DEXs and liquidity pools handle this differently:
That’s why you should always check:
What happened to the pool after the swap?
From the screenshot:
💡 This price shift reflects how AMMs work — swaps change the ratio, and therefore the price.

📌 Makes sense:
That’s how AMM pricing dynamically adjusts after each swap — the pool always seeks to restore balance through the price.
After the first swap, the token balances in the pool have changed. Now let’s run the same swap again — another 10 tokens of X — and see what happens.
Starting state after the first swap:
Let’s factor in the 10% fee right away:
Just like before:
New X balance:
Recalculate Y:
Output:
🔻 You only get 7 Y tokens this time — less than the previous swap.

In total:
20 X → 15.26 Y
Because the ratio of tokens in the pool changed — and the price of token X relative to Y dropped.
In other words, your X is now “worth less” than before.
You can now see:

The current price shown in the UI is just the token ratio in the pool — not the actual rate you’ll get on your next swap.
The real swap price is calculated at the moment of the transaction, based on:
The formula X * Y = K is simple — but it’s not about fixed exchange rates.
Every swap changes the price.
That’s exactly why arbitrage exists between pools:
If one pool’s price shifts significantly and another hasn’t caught up — that’s an opportunity.
Follow if you’re curious 🗿
DEX from the Inside: Who Moves the Price and How It Actually Works was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.