How Does an AMM Work? Uniswap, x*y=k and Price Impact
Lesson 2 of 10 · Intermediate
How is a decentralised exchange different from a normal exchange?
On a centralised exchange your assets sit in the exchange’s wallet and trades are matched in an order book: a buyer offers a price, a seller asks a price, and the exchange’s matching engine brings them together. On a decentralised exchange (DEX), your assets stay in your own wallet until the moment of the trade, and the trade is carried out directly with a smart contract.
On a blockchain, maintaining a full order book is expensive and slow, because every order placement and cancellation costs a fee. The solution popularised by Uniswap is the automated market maker, or AMM: instead of waiting for a seller, a buyer trades against a pool that holds two tokens and sets the price with a formula. Uniswap is one of the best-known decentralised exchanges on Ethereum and several other networks, and it has several versions (v2, v3 and v4), each changing the pool logic a little.
Note: in this lesson Uniswap is only an educational example for understanding the mechanism, not a recommendation to use it or to buy its token.
The x*y=k formula in plain English
The simplest AMM uses a constant product formula: if a pool holds x units of token A and y units of token B, the product x × y = k must stay constant after each trade (ignoring fees). The instantaneous price is the ratio of the two balances.
Hypothetical example: a pool holds 100 ETH and 300,000 stablecoins. So k = 30,000,000 and the instantaneous price is 3,000 dollars per ETH.
- Buying 1 ETH: the pool’s ETH becomes 99. To keep k constant, the stablecoin side must rise to about 303,030, so you pay about 3,030 dollars, roughly 1 percent above the instantaneous price.
- Buying 10 ETH: the pool’s ETH becomes 90 and the stablecoin side must reach about 333,333. You pay about 33,333 dollars, an average of about 3,333 per ETH, roughly 11 percent higher. The pool’s instantaneous price after the trade is about 3,700 dollars.
This gap is called price impact. The bigger your order relative to the pool’s depth, the worse the price you get. The pool never “runs out”; only the price of the last units becomes very expensive. On top of price impact, every trade pays a fee (0.3 percent in this hypothetical example) that goes to liquidity providers.
How does an AMM price stay in line with the market? Arbitrage
An AMM does not know the global market price; it sees only its own balances. So who keeps the price right? Arbitrageurs. If after your big purchase the pool’s price reaches 3,700 while other markets say 3,000, bots buy ETH more cheaply elsewhere and sell it into the pool until the gap closes. Their profit comes from the pocket of whoever moved the price and from the liquidity providers whose assets traded at the old price.
This point matters again in the impermanent loss lesson: a price move in the outside market, through arbitrage, changes the composition of the pool’s assets.
Routers and aggregators: the trade path
Most users do not interact with a single pool directly. A router is a contract that finds the path; for example, if there is no direct pool from token A to C, it goes A to B and then B to C. An aggregator goes a step further: it checks several DEXs and may split an order across several pools to reduce price impact.
Two security and practical points:
- Each extra hop means more network fees and more contracts you are trusting.
- To trade, you usually have to give the router contract a spending approval. An unlimited approval to the wrong contract is one of the most common ways wallets are drained. Use only official addresses and revoke old approvals.
Newer generations: concentrated liquidity and hooks in Uniswap v4
In v3 a liquidity provider can place capital only within a chosen price range (concentrated liquidity). This deepens liquidity near the current price, but if price leaves the range, that liquidity stops earning fees and has turned entirely into one of the two tokens.
In Uniswap v4 the idea of the hook was added: a pool can be attached to a separate contract that runs extra code at defined moments, such as before or after a swap or when liquidity is added. Hooks can build dynamic fees, limit orders and other logic. From a risk view, this means each pool may have its own custom code. So a pool being on “Uniswap” does not mean its hook has been vetted; you must evaluate the hook separately.
In summary: an AMM sets price from a formula and pool balances, pool depth determines the real cost of a trade, and each extra layer (router, hook, aggregator) brings both capability and risk.
Common mistakes
- Looking only at the quoted price and missing price impact; in a shallow pool the real execution price can be far from the displayed one.
- Giving unlimited spending approval to any contract a site suggests, without checking the address and without revoking it later.
- Assuming every pool on a famous DEX has been vetted equally; in v4 hooks carry independent code.
- Trading a fake token with the same name; anyone can create a pool for any token. Check the token’s contract address from an official source.
- Ignoring network fees on small trades; sometimes the fee exceeds the hypothetical gain.
What do volume and data say here?
On a DEX, “depth” is what you used to see in an order book in traditional markets:
- Compare pool liquidity with your order size: as a rule of thumb, if your order is a noticeable percentage of one side of the pool, price impact will be large.
- Real volume against TVL: a pool with high TVL but negligible trading volume is probably filled by token incentives.
- On-chain buy and sell data: on a DEX every trade is recorded with its direction, and analytics sites show buys and sells separately, something like a delta. But be aware that a large part of this volume comes from arbitrage bots, not genuine demand.
- Liquidity concentration: in an explorer, see how many addresses supply most of a pool’s liquidity; if one of them leaves, depth drops at once.
📊 See real gold futures volume behind these ideas: free order flow course · today's gold volume map
Frequently asked questions
What is Uniswap?
A decentralised exchange on Ethereum and several other networks that uses liquidity pools and an automated market maker formula instead of an order book to set prices. Trades are made directly from your wallet.
How does an AMM set the price?
From the ratio of the two token balances in the pool. In the simple x*y=k model, each purchase reduces one token and increases the other, moving the price against the buyer. Arbitrageurs keep the price in line with other markets.
What is the difference between an aggregator and a router?
A router finds the trade path within a DEX; an aggregator compares several DEXs and may split the order across pools to reduce price impact. Both are contracts whose official addresses you should confirm.
Education only, not a recommendation to buy or sell; we never give signals. Price examples are illustrative. Test any method on a demo account first, with strict risk management.