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:

BuyerPriceSeller
wants to buy 1 BTC$69,000wants to sell 1 BTC
wants to buy 2 BTC$68,500wants 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:

x×y=kx \times y = k

Where:

  • xx = token A reserve

  • yy = token B reserve

  • kk = 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:

x×y=10×20,000=200,000x \times y = 10 \times 20,000 = 200,000

That’s our constant.

Now someone wants to buy 1 ETH.

Step 1: ETH decreases → 9 ETH remain

x=9x = 9

Step 2: To keep k constant, y must increase:

y=200,000/9=22,222.22y = 200,000 / 9 = 22,222.22

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) =

PA=yxP_A = \frac{y}{x}
  • When x decreases (ETH bought), price rises.

  • When x increases (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 y=k/xy = k/x, 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 xy=kx*y=k 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 TypeFormulaUse CaseExample
Constant Product (x*y=k)StandardVolatile pairs (ETH/USDT)Uniswap v2, PancakeSwap
Constant Sum (x+y=k)Keeps price stablePegged assets (USDC/DAI)Rare
Hybrid (Curve’s StableSwap)Mix of product + sumStablecoinsCurve Finance
Weighted AMMxwy(1w)=kx^w * y^{(1-w)} = kCustom ratiosBalancer (e.g., 80/20 pools)
Dynamic AMMsAdjust formulas over timeEfficient liquidityUniswap v3 (concentrated liquidity)

⚙️ 12. Inside the Smart Contract — What AMM Code Does

An AMM smart contract typically:

  1. Stores reserves (token A and B)

  2. Computes output using xy=kx*y=k

  3. Applies trading fee

  4. Updates new reserves

  5. Mints/burns LP tokens

  6. Transfers tokens to trader

So it’s a fully on-chain exchange engine written in Solidity.

Example pseudocode:

function swap(uint amountIn, address tokenIn) { uint amountOut = getAmountOut(amountIn, reserveIn, reserveOut); reserveIn += amountIn; reserveOut -= amountOut; transfer(tokenOut, msg.sender, amountOut); }

🧠 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.

StepETHUSDTPrice (USDT/ETH)Action
Start1020,0002000Initial
After Trade922,2222469ETH becomes costlier

Now another trader swaps 1 ETH → opposite trade:

StepETHUSDTPriceComment
After Trade1020,0002000Price rebalanced

💡 Constant product k stayed 200,000 throughout.


🧭 17. AMM Limitations

IssueExplanation
SlippageLarge trades move price significantly
Impermanent LossLPs lose value when price diverges
Front-runningBots exploit transaction order
Low Capital EfficiencyLiquidity 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

ConceptDescriptionExample
AMMCode that sets prices & executes tradesUniswap, Curve
FormulaMath relationship controlling poolx*y=k
Liquidity PoolReserve of token pairsETH/USDT
LP TokenProof of liquidity contributionUNI-V2
SlippagePrice difference from pool ratioLarge swaps
ArbitrageExternal traders balance pricesBinance vs Uniswap
FeeReward for LPs0.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

Popular posts from this blog