The problem: trading without an order book
Centralized exchanges work with order books. Buyers post bids, sellers post asks, the exchange matches them. On chain this is hard. Every post and every cancel is a transaction that costs compute and rent. Order books do exist on Solana (Serum was the main one, and OpenBook is its community fork), but they still depend on market makers who keep sending those transactions.
Uniswap V2 took a different approach, and Solana AMMs (Automated Market Maker) inherited it. Instead of matching orders, the AMM holds a pool of both tokens and uses a formula to set the price. Anyone can swap one token for the other against the pool. Anyone can add tokens to the pool to make it deeper. The price comes out of one piece of math.
A pool is a set of accounts, not a contract
A pool on Solana is not a contract that holds tokens, the way Uniswap V2's pool is a smart contract on Ethereum. Solana programs are stateless code. A pool is a set of accounts that one AMM program operates on. This is the biggest architectural difference between Uniswap V2 on Ethereum and any constant-product AMM on Solana.
One program, many pools. On Solana, no new program gets deployed for each pool. The same AMM program serves every market on the network. USDC/SOL, USDC/JUP, BONK/SOL, and every other pair run on the same deployed code.
What makes each pool distinct is a set of accounts. A pool consists of four accounts, all of them PDAs derived from the two underlying token mints.
- A pool config account that holds the pool's metadata. The two token mints, the current reserves, the fee rate, the LP mint address, the bumps. This is where the program reads the pool's reserves from.
- A vault for token A, an SPL token account custodying the actual token A. Its authority is a PDA of the AMM program, so only the AMM program can move tokens out of it.
- A vault for token B, the same shape, custodying token B.
- An LP mint, the SPL mint for this pool's LP tokens, which the pool gives to liquidity providers (LPs) for their deposits. Its mint authority is a PDA of the AMM program.
All four addresses are derived from the two token mints, typically taken in canonical order, with a different seed for each account. Given the two mints, anyone can find every account that belongs to that pool, without needing a registry. The risk isolation between markets that Ethereum gets from separate contracts, Solana gets from separate account sets. The code path is shared, but a bug in pool USDC/SOL cannot directly drain the vaults of pool BONK/USDC, because each pool's vaults have their own PDA authority and the program checks them.
Pools hold SPL tokens only. A pool's vault accounts are SPL token accounts, and they can only hold SPL tokens. To trade native SOL against another token, the user wraps SOL into wrapped SOL, an SPL token, before depositing. Wrapped SOL accounts can be created and closed within the same transaction, so the user sees a normal "swap SOL for token" experience. Because the vaults accept only SPL tokens, the program has one code path for token movement.
The pool enforces one rule
For the math, treat the pool as a logical thing that holds two tokens, token A and token B, and enforces one rule:
x * y = kx is how much of token B the pool holds. y is how much of token A the pool holds. k is the product, and the pool keeps k constant across every swap.
Suppose the pool currently holds 100 of token A and 100 of token B. Then x = 100, y = 100, and k = 10,000. After any swap, the product of the two reserves must still equal 10,000.
Walking through one swap
Let us see what happens when someone trades against this pool. A trader wants to swap token B for token A. They put 10 of token B into the pool. What does the program give them back?
Step one. After the trader's deposit, the pool's reserve of token B has grown by 10.
x_new = 100 + 10 = 110Step two. To keep k = 10,000, the pool's reserve of token A must shrink to whatever value satisfies 110 * y_new = 10,000.
y_new = 10,000 / 110 = 90.91Step three. The pool used to have 100 of token A. Now it must have 90.91. The difference goes to the trader.
trader receives = 100 - 90.91 = 9.09 of token AThe trader put in 10 of token B and got 9.09 of token A. Nobody quoted a price and no orders were matched. The output was whatever amount keeps the product equal to 10,000.
Written as a formula in general terms, with Δx for the input and Δy for the output:
Δy = y - k / (x + Δx)The new reserve of token A must equal k divided by the new reserve of token B, and the trader gets whatever the old reserve was minus that. After simplification:
Δy = (y * Δx) / (x + Δx)This swap formula is the invariant x * y = k rearranged to solve for the output.
The curve x * y = k is a hyperbola. The pool's current state is always one specific point on this curve. A swap is a movement from one point on the curve to another point on the same curve. The trader's input pushes the pool to the right along the curve, and the program gives back enough token A to make the pool's new position satisfy the invariant.
The price changes after every swap
The pool's reserves change after every swap, which means the next trader doing the same swap gets a different result.
After the first trader's swap, the pool is at (110, 90.91). A second trader wants to do the same thing, putting in another 10 of token B.
x_new = 110 + 10 = 120
y_new = 10,000 / 120 = 83.33
second trader receives = 90.91 - 83.33 = 7.58 of token AThe first trader got 9.09 of token A for their 10. The second trader gets only 7.58 for the same input.
A third trader comes along with the same 10 of token B.
x_new = 120 + 10 = 130
y_new = 10,000 / 130 = 76.92
third trader receives = 83.33 - 76.92 = 6.41 of token A9.09, then 7.58, then 6.41. Each trader gets less than the one before, even though they all put in the same amount. The pool has been giving away token A and receiving token B, so token A is becoming scarcer in the pool, and the pool charges more for it.
This is the price discovery mechanism. The pool does not ask anyone what the price should be. It keeps the invariant, and the invariant produces prices that respond to supply and demand. If token A is in high demand, with lots of traders putting B in to get A out, the reserve of A shrinks fast, and the price of A in B rises fast. If demand fades, no swaps happen, and the price stays put.
Why bigger trades face worse rates
What if a single trader had put in 30 of token B in one swap instead of three separate trades of 10? The pool ends at the same point, x = 130 and y = 76.92, so the trader receives 100 - 76.92 = 23.08 of token A. That is the sum of 9.09 + 7.58 + 6.41. The total output is identical whether you arrive in one big swap or three small ones.
What the size of a trade changes is its average rate. For a very small trade the swap formula gives Δy / Δx ≈ y / x, so the ratio of the reserves is the pool's spot price: 1 token A per token B in the starting pool. The first 10-token trade got 9.09 / 10 = 0.91 token A per token B, and the 30-token trade got 23.08 / 30 = 0.77. The bigger your trade relative to the pool, the worse your average rate, because you push deeper into the unfavorable part of the curve in one move. This effect is called price impact. There is nothing the program can do about this, because the rate is what the invariant produces.
The diagram continues with trades of 30. The first one is the trade just computed, and the next two get 14.42 and 9.87. A single trade of 90 would get the sum of the three, 47.37, at an average rate of 47.37 / 90 = 0.53.
Where the reserves come from: liquidity providers
The pool needs reserves before anyone can swap against it. Liquidity providers, or LPs, supply them.
An LP deposits both tokens. They must deposit them in the same ratio the pool currently holds. If the pool is at 100 of token A and 200 of token B, an LP depositing 1 of token A must also deposit 2 of token B. Depositing out of ratio would change the pool's spot price away from the market price, which would invite arbitrage and effectively let the LP donate value to whoever shows up next.
In exchange for the deposit, the LP receives LP tokens from the pool's LP mint. The mint authority on that mint is a PDA of the AMM program, so only the AMM program can create new LP tokens. The LP tokens themselves are normal SPL tokens, transferable between accounts like any other token, and they represent a share of the pool. If an LP owns 10 percent of the LP token supply, they own a claim on 10 percent of the pool's reserves. To take their money out, they hand back their LP tokens to be burned, and the program sends them their share of the underlying.
The first deposit into a fresh pool is special, because there is no existing ratio. The first LP sets it. The LP tokens minted to them are computed as sqrt(x_deposit * y_deposit). For 200 USDC and 1 SOL that is sqrt(200 * 1) = 14.14. The square root makes the supply grow in proportion to the deposit: twice as much of both tokens gives sqrt(400 * 2) = 28.28, twice as many LP tokens, where the plain product would give four times as many.
For every later deposit, LP tokens are minted in proportion. If an LP adds 5 percent to the pool's reserves in the current ratio, the program mints 5 percent of the current LP supply to them.
How LPs get paid: the fee
LPs take a risk by providing liquidity. The composition of the pool changes as traders swap, which means an LP can end up holding more of whichever token has fallen in value and less of whichever has risen. To compensate for that risk, the pool charges traders a fee on every swap.
The fee is typically 0.3 percent of the input amount on a standard constant-product pool. The fee is not sent anywhere. It stays in the pool. The trader puts in Δx, the program treats only 99.7 percent of Δx as effective input when computing the output, and the remaining 0.3 percent stays in the reserves.
The swap formula with the fee becomes:
Δy = (y * Δx * 997) / (x * 1000 + Δx * 997)That is the same formula as before, with Δx replaced by Δx * 997 / 1000, the 99.7 percent that counts. The * 1000 factor in the denominator is the standard way to handle the 0.3 percent in integer arithmetic without using floating point.
The fee adds to the reserves but no new LP tokens are minted to cover it. So the total LP supply stays the same while the reserves grow, and each LP token now claims a slightly bigger slice of the pool. LPs do not have to claim anything or call any instruction. The program has no per-LP fee tracker and no claim-fees instruction.
Impermanent loss: the cost LPs pay
The constant-product invariant rebalances the pool every time price changes. If SOL appreciates, swappers buy SOL from the pool, so the pool loses SOL and gains USDC, and an LP who owns a fixed share of the pool misses part of the SOL appreciation. If SOL falls, the pool buys SOL from sellers and the LP now holds more of the depreciated asset. Either direction, holding the original tokens would have done better. The difference is called impermanent loss.
Take Alice from the diagram above. She deposits 1 SOL and 200 USDC when SOL costs $200, so k = 200 and her deposit is worth $400. SOL then doubles to $400. Traders buy SOL from the pool until the ratio of its reserves is 400 USDC per SOL, and the reserves that multiply to 200 at that ratio are 0.707 SOL and 283 USDC. Alice's position is now worth 0.707 * $400 + $283 = $566. Had she kept 1 SOL and 200 USDC in her wallet, she would have $600. She is $34 short, which is 5.7 percent of what holding would have given her.
For a price ratio r between exit and entry, the same calculation gives:
IL = 2 * sqrt(r) / (1 + r) - 1With r = 2 this is 2 * 1.414 / 3 - 1 = -0.057, the same 5.7 percent. The loss is symmetric: a 2x move and a 0.5x move both produce 5.7 percent loss.
The loss is only impermanent in one specific sense. If price returns to entry before you withdraw, the loss disappears. If you withdraw at any other price, the loss is realized and permanent.
Fees are the LP's income against this cost. A pool with high trading volume relative to its reserves earns the loss back, and a quiet pool does not. The CLMM lesson shows the next design, where the LP chooses the price range their capital serves.
The first deposit locks a minimum of liquidity
The first LP into a fresh pool gets sqrt(x_deposit * y_deposit) LP tokens. The naive implementation that mints all of them to the depositor lets an attacker manipulate the share price for the next depositor. The attack works against a program that takes the pool's reserves from the vault balances. An attacker deposits a tiny amount as the first LP and mints a tiny number of LP tokens, say 1 base unit. They then transfer a large donation of tokens directly into the pool's vaults, using a plain SPL transfer to the vault account address, bypassing the add-liquidity instruction entirely. The vault balances balloon, but the LP supply stays at the tiny number from the first deposit.
The next user who tries to deposit normally finds that their proportional share rounds down to zero LP tokens, because each LP token now represents a hugely inflated amount of underlying. They lose their deposit to the attacker, who can then withdraw and walk away with the donation plus the victim's funds.
A correct implementation mints the first LP slightly less than sqrt(x_deposit * y_deposit), and locks a small amount, typically 1000 base units of the LP mint, by sending it to a non-spendable PDA or burning it outright. This is the minimum liquidity lock. Locking 1000 base units of LP at the first deposit means there is always a baseline supply that nobody owns and nobody can manipulate around. This caps how much an attacker can inflate the per-share value.