Automated Market Maker
🧩 1. The Root Problem: Why Do We Need an “Automated” Market Maker?
Let’s start from the question “why.”
Traditional markets (like stock exchanges or Binance) work with Order Books — a list of buyers and sellers with their prices.
For example:
| Buyer | Price | Seller |
|---|---|---|
| wants to buy 1 BTC | $69,000 | wants to sell 1 BTC |
| wants to buy 2 BTC | $68,500 | wants to sell 3 BTC at $69,200 |
The exchange matches buyers and sellers manually or algorithmically.
But in DeFi, there is:
-
No central exchange,
-
No order-matching engine,
-
No market maker institutions (like in Wall Street).
Yet we still want 24/7 on-chain trading between assets like ETH ↔ USDT, DAI ↔ WBTC, etc.
So, question:
👉 How can a blockchain enable trading without an order book or centralized market makers?
Answer: by using math and smart contracts.
That math-based mechanism is called an Automated Market Maker (AMM).
⚙️ 2. Definition — What is an AMM?
An Automated Market Maker (AMM) is a smart contract that uses a mathematical formula to price assets and execute trades automatically inside a liquidity pool — without needing buyers and sellers to match orders.
So instead of humans setting prices, code sets prices based on liquidity pool ratios.
🧠 3. Core Idea — Code as the Market Maker
In centralized finance, market makers are institutions that always keep buy/sell orders to ensure liquidity.
In DeFi:
-
The smart contract plays that role.
-
The formula ensures that there’s always a price available for any trade.
-
Anyone can become a liquidity provider (LP), meaning everyone can be a “mini market maker.”
Hence the name — Automated Market Maker.
🔢 4. The Core Formula — Constant Product Market Maker (Uniswap v2)
The most famous AMM formula is:
Where:
-
= token A reserve
-
= token B reserve
-
= constant (always remains same)
This is called the Constant Product Formula — introduced by Uniswap.
💡 5. How It Works (Step-by-Step Example)
Let’s say there’s a pool of:
-
10 ETH
-
20,000 USDT
So:
That’s our constant.
Now someone wants to buy 1 ETH.
Step 1: ETH decreases → 9 ETH remain
Step 2: To keep k constant, y must increase:
So, pool now holds 22,222 USDT.
That means trader paid:
→ 22,222 − 20,000 = 2,222 USDT for 1 ETH.
Hence, new price = 2,222 USDT/ETH.
✅ Price automatically adjusted due to the change in pool ratio.
No order book, no negotiation — just math.
🧮 6. The Price Formula Inside AMM
Price of Token A (in terms of Token B) =
-
When
xdecreases (ETH bought), price rises. -
When
xincreases (ETH sold), price falls.
This creates a self-balancing mechanism.
💸 7. Fee Distribution
When trades happen, a fee (usually 0.3%) is taken and added back to the pool.
That means:
-
Liquidity providers earn this fee.
-
Pool value increases over time.
That’s how LPs earn yield → which powers yield farming.
📈 8. The Curve Shape — Constant Product Graph
If you plot , you get a hyperbola.
Meaning:
-
You can never drain the pool completely.
-
The closer you get to emptying one side, the price skyrockets.
That’s why large trades cause slippage — price moves significantly as one asset gets scarce.
🧩 9. Slippage (Price Impact)
When a trade is large relative to pool size,
the formula forces a nonlinear price rise.
Example:
-
Pool = 10 ETH, 20,000 USDT.
-
Buying 0.1 ETH → small impact.
-
Buying 5 ETH → huge price rise (ETH now much more expensive).
This ensures liquidity always exists, but prices move sharply on large trades.
⚖️ 10. Arbitrage — The Balancer of AMMs
Because AMMs price assets via formulas, they may diverge from real market prices (on Binance, Coinbase, etc.).
Example:
-
Binance ETH = $2000,
-
Uniswap ETH = $2100.
Arbitrage traders notice this and:
-
Buy ETH on Binance ($2000),
-
Sell ETH on Uniswap ($2100),
-
Profit $100 per ETH.
Their trades push the pool back to equilibrium —
AMMs rely on arbitrage to maintain accurate prices.
So arbitrageurs are like natural regulators of AMM prices.
🧱 11. Variants of AMMs (Evolution Beyond Uniswap v2)
| AMM Type | Formula | Use Case | Example |
|---|---|---|---|
| Constant Product (x*y=k) | Standard | Volatile pairs (ETH/USDT) | Uniswap v2, PancakeSwap |
| Constant Sum (x+y=k) | Keeps price stable | Pegged assets (USDC/DAI) | Rare |
| Hybrid (Curve’s StableSwap) | Mix of product + sum | Stablecoins | Curve Finance |
| Weighted AMM | Custom ratios | Balancer (e.g., 80/20 pools) | |
| Dynamic AMMs | Adjust formulas over time | Efficient liquidity | Uniswap v3 (concentrated liquidity) |
⚙️ 12. Inside the Smart Contract — What AMM Code Does
An AMM smart contract typically:
-
Stores reserves (token A and B)
-
Computes output using
-
Applies trading fee
-
Updates new reserves
-
Mints/burns LP tokens
-
Transfers tokens to trader
So it’s a fully on-chain exchange engine written in Solidity.
Example pseudocode:
🧠 13. Key Mathematical Insight — Why It Works
The genius of AMMs is that liquidity is continuous.
Even if:
-
There’s only one LP,
-
No one else trading,
You can still buy or sell any amount,
because the formula always gives a valid price.
You just pay more for larger trades.
It’s a deterministic market — price = math, not mood.
💥 14. Impermanent Loss Connection
When prices of assets in the pool move in the external market,
the pool’s internal ratio adjusts —
causing the LP’s asset value to diverge from just holding.
That’s called impermanent loss,
and it directly comes from how AMM math works.
We’ll unpack that mathematically next if you want.
🧩 15. Real-World Analogy
Imagine a vending machine that sells apples for oranges. 🍎 ↔ 🍊
-
The machine starts with 10 apples and 10 oranges.
-
Every time someone buys apples, the machine increases the apple price.
-
Every time someone buys oranges, orange price rises.
It never “runs out” — it just makes the next unit more expensive.
That’s exactly what AMMs do.
🧮 16. Example of AMM in Action
Starting pool:
10 ETH + 20,000 USDT
k = 200,000
Trader swaps 1 ETH for USDT.
| Step | ETH | USDT | Price (USDT/ETH) | Action |
|---|---|---|---|---|
| Start | 10 | 20,000 | 2000 | Initial |
| After Trade | 9 | 22,222 | 2469 | ETH becomes costlier |
Now another trader swaps 1 ETH → opposite trade:
| Step | ETH | USDT | Price | Comment |
|---|---|---|---|---|
| After Trade | 10 | 20,000 | 2000 | Price rebalanced |
💡 Constant product k stayed 200,000 throughout.
🧭 17. AMM Limitations
| Issue | Explanation |
|---|---|
| Slippage | Large trades move price significantly |
| Impermanent Loss | LPs lose value when price diverges |
| Front-running | Bots exploit transaction order |
| Low Capital Efficiency | Liquidity is spread thin across all prices (fixed in Uniswap v3) |
🧱 18. AMM Upgrades — Uniswap v3 & Beyond
Uniswap v3 introduced Concentrated Liquidity AMMs:
-
LPs choose price ranges where they want to provide liquidity.
-
Higher efficiency, less idle capital.
-
More complex but more powerful.
It’s like replacing one big pool with many small custom pools.
✅ 19. Summary Table
| Concept | Description | Example |
|---|---|---|
| AMM | Code that sets prices & executes trades | Uniswap, Curve |
| Formula | Math relationship controlling pool | x*y=k |
| Liquidity Pool | Reserve of token pairs | ETH/USDT |
| LP Token | Proof of liquidity contribution | UNI-V2 |
| Slippage | Price difference from pool ratio | Large swaps |
| Arbitrage | External traders balance prices | Binance vs Uniswap |
| Fee | Reward for LPs | 0.3% typical |
🌍 20. In One Line
An Automated Market Maker (AMM) is a decentralized pricing algorithm that uses math (like x*y=k) inside liquidity pools to allow instant, trustless trading — replacing traditional order books and human market makers.
Comments
Post a Comment