AMM // automated market maker
A decentralized exchange (DEX) has no middleman
A DEX such as Uniswap or PulseX runs on an automated market maker (AMM): it trades from a pool, a smart contract holding two tokens. Anyone can trade with it: put one token in, take the other out. No order book, no company holding your money, nobody setting prices. A formula does the work.
$ amm --pool
- design
- constant product, x · y = k
- swap fee
- 0.3% here; each DEX sets its own
- price
- ratio of the two balances
- used by
- Uniswap v2 and forks such as PulseX
- A classic exchange needs a seller on the other side. Here a contract always trades.
- It holds two tokens. In our example: 100,000 of token A and 200,000 of token B.
- Anyone can trade: put one token in, take the other one out.
- No order book and nobody setting prices. One formula does it all.
Liquidity // addLiquidity()
Providers fill the pool and get a receipt
The tokens in a pool come from liquidity providers (LPs). They deposit both tokens in the pool's current ratio and receive LP tokens: a receipt for their share of whatever the pool holds.
LP tokens can be handed back at any time for the same share of both tokens. The fees traders pay are what makes providing liquidity worth it.
- Who put the tokens there? People called liquidity providers, or LPs.
- Each adds both tokens in the pool's ratio: here 1 A for every 2 B.
- One provider adds 10,000 A and 20,000 B, a tenth of the pool.
- In return the pool mints LP tokens: a receipt for 10% of everything in it.
- Traders pay a fee into the pool. That is what LPs are in it for.
Price // getReserves()
The price is just a ratio
The pool knows nothing else. It holds 100,000 A and 200,000 B, so 1 A costs 2 B. There is no oracle and no price feed. Only trades and changes in liquidity move the ratio.
1 A = 200,000 B ÷ 100,000 A = 2 B
Real pools change their balances with every trade. On Uniswap v2-style DEXs a pool's two balances can be read with getReserves in a block explorer.
- The pool holds 100,000 A and 200,000 B.
- There is twice as much B as A…
- …so 1 A trades for 2 B, and 1 B for 0.5 A.
- No price feed and no one setting it: the ratio of the two piles is the price.
- Only trades and changes in liquidity move it.
The rule // x · y = k
One rule: the product must not shrink
Multiply the two balances: 100,000 × 200,000 = 20 billion. Call it k. A swap may change the mix, but the pool pays out only as much as keeps the product at least k.
balance0Adjusted × balance1Adjusted ≥ reserve0 × reserve1 × 1000²
On a chart the rule is a curve: every mix of A and B the pool may hold lies on it. The 0.3% fee leaves the area a hair above k; the next slide shows why.
- Take the pool's two piles: 100,000 A and 200,000 B.
- Lay them down as the sides of a rectangle. Its area is k: 20 billion.
- You sell 10,000 A. The rectangle gets 10% wider…
- …so the pool pays out B until the area is back: 18,132.22 B.
- The corner slid along a curve. Every mix the pool may hold lies on it.
- Now 110,000 A and 181,868 B: 1 A buys just 1.65 B.
Fee // 0.3% per swap
The fee stays in the pool
The pool pays out as if you had sent 99.7% of your tokens, and keeps all of them. The 0.3% left behind makes x · y a little bigger with every trade, and that growth belongs to the LP tokens.
- Fee switch off (how Uniswap v2 started): the whole 0.3% stays with liquidity providers.
- Fee switch on: a sixth of the fee growth is minted to the protocol, so providers keep 0.25%.
- Other DEXs set their own fee and split. PulseX, for example, charges 0.29%.
amountIn × 997 / 1000
- Of your 10,000 A, the pool counts 9,970 when it works out your payout.
- The other 30 A, the 0.3% fee, simply stays in the pool.
- So x · y grows a little with every trade. The growth belongs to the LPs.
- In the Uniswap v2 design the fee switch starts off: all 30 stay with LPs.
- Switched on, the protocol is minted LP tokens worth a sixth of the growth: 5 of the 30.
- Other DEXs set their own fee and split. PulseX, for example, charges 0.29%.
Price impact // size matters
Big trades get worse prices
Each token A you add buys a little less B than the one before, because you walk along the curve as you trade. What counts is your trade compared with the pool, so a thin pool swings hard even on small trades.
That move is the price impact; the figures include the 0.3% fee. Slippage is the extra move from other trades that land before yours, and the slippage tolerance in the swap screen caps it.
- Every extra A you sell buys a little less B than the one before.
- Sell 1% of the pool's A: your price is 1.28% worse than at the start.
- Sell 10%: 9.34% worse, and B gets 20.97% dearer in the pool.
- Sell 40%: 28.72% worse. What counts is the size compared with the pool.
- Your turn: drag the slider to try any trade size.
you get 18,132.22 Bprice 9.34% worseB price +20.97%
Depth // the pool's size
Deep pools hold their price
The same trade is a big splash in a small pool and a ripple in a deep one. Sell 10,000 A into a pool ten times deeper and you add 1% to its token A instead of 10%, so you walk a much shorter way along the curve.
Both pools start at 1 A = 2 B. At that price, with no fee and no price impact, 10,000 A would buy 20,000 B. The figures include the 0.3% fee.
- Like a stone in a bucket and a stone in a lake: two pools, one ten times deeper.
- You sell the same 10,000 A into each of them.
- The small pool pays 18,132 B: a price 9.34% worse than at the start.
- The deep pool pays 19,743 B: only 1.28% worse. Your trade is 1% of it, not 10%.
- Same trade, 1,611 B more. Before a swap, check how much the pool holds.
Arbitrage // the price comes back
Arbitrage pulls the price back
After your trade, token B is dearer in this pool than on other markets. Anyone can buy it cheaper elsewhere, sell it into the pool and keep the difference. Every such trade pushes the pool back towards the outside price.
- That is how a pool with no price feed follows the market, and why pools on different DEXs usually show nearly the same price.
- Each arbitrage trade pays the pool a fee, too.
Illustration: the other markets are assumed to stay at 1 A = 2 B.
- After your trade this pool pays 1.65 B per A. Elsewhere it is still 2.
- A trader buys about 18,187 B elsewhere, paying 9,093 A…
- …and sells them into this pool for 9,973 A.
- The pool is back at 1 A = 2 B. The trader keeps about 879 A.
- The pool ends up a little bigger than it began: both fees stayed in it.
Liquidity providers // impermanent loss
Providers earn fees but carry the price risk
An LP token is a fixed share of whatever the pool holds. When the price moves, the pool keeps more of the token that fell and less of the one that rose. The share is then worth less than simply holding both: impermanent loss.
- Impermanent: the gap closes again if the price returns.
- Fees work against it: every swap adds a little to the share.
Figures before fees.
- Our provider put in 10,000 A and 20,000 B: 10% of the pool.
- Now B doubles in price against A, and arbitrage moves the pool to match.
- The pool sold B on the way up. The share is now 14,142 A + 14,142 B.
- That is worth 28,284 A, 5.72% less than the 30,000 that simply holding would be worth.
- Your turn: drag the slider to move the price of B.
in the pool 28,284 Aheld 30,000 Agap −5.72%
Recap // before you swap
What to check before a swap
- Your size against the pool. A trade that is big for the pool gets a much worse price; a deeper pool gives a better one.
- Slippage tolerance caps how much worse than the quote you accept.
- The fee. Each DEX sets its own, and not all of it has to go to liquidity providers.
- Check it yourself: getReserves on a pair contract, such as the WPLS/PLSX pair on PulseX, shows its two balances.
Other AMMs change the formula or concentrate liquidity in a price range; trading against a pool stays the same idea. A real one: How PulseX works. Not financial advice.
- Liquidity providers fill the pool with two tokens and get LP tokens.
- The price is simply the ratio of the two balances.
- A swap must not shrink x · y, so big trades walk far along the curve.
- The 0.3% fee stays in the pool and makes it grow.
- Arbitrage pulls the price back in line with other markets.