← 1-Year PathQ2 · DeFi

Week 23 — AMMs & Lending

How automated market makers and lending protocols actually work under the hood.

Week 23 of 52 · ~7 hours · 13 slides · exam + project

The DeFi Engine Room

Constant-product pools and over-collateralized lending.

What you will learn

  • Understand the constant-product AMM formula
  • Explain how lending protocols set rates
  • See the incentives that keep pools healthy

How a liquidity pool prices trades

Liquidity Pool TOKEN A x amount TOKEN B y amount x · y = k (constant product) Trades swap between A and B against the pool — price set by the ratio.
How a liquidity pool prices trades

Atomic settlement

Seller: units releases trust units Buyer: consideration releases payment Escrow / swap atomic Both or neither Atomic settlement: the swap completes fully or not at all — no partial risk.
Atomic settlement

The AMM formula

Uniswap-style AMMs use the constant-product rule: x × y = k, where x and y are the two tokens' reserves. When you swap, the product stays constant — so trading moves the price. More of one token in the pool → its price falls relative to the other. Price is set by math, not order books.

💡 A swap, step by step

A pool holds 100 ETH and 200,000 USDC (k = 20,000,000). You swap in 10 ETH. To keep k constant, the pool gives you USDC such that the new reserves multiply to k. You receive fewer USDC than the 'spot' rate because your trade moves the price — that's slippage, and it's inherent to AMMs.

Liquidity providers & fees

LPs deposit both tokens and earn a share of trading fees. In return they bear impermanent loss: if the two assets' prices diverge, the LP's value is less than if they'd just held both. Fees usually compensate — but only if volume is high enough.

Over-collateralized lending

Lending protocols (Aave, Compound) let you borrow only if you deposit more collateral than you borrow — typically 1.5x or more. That over-collateralization replaces credit scores and legal recourse with code: if your collateral's value falls, it's automatically liquidated to protect lenders.

💡 Health factor

Borrow against $1,500 of ETH to take out $1,000 of USDC. If ETH falls and your collateral drops toward $1,000, your health factor approaches 1.0, and liquidators repay your loan and seize your collateral (plus a bonus). Watch the health factor — it's your margin of safety.

Why this design is resilient

AMMs and over-collateralized lending remove the need to trust a counterparty: math and collateral do the work. That's the DeFi thesis — replace trust with incentives. But the same code that removes trust also concentrates it in the smart contract itself.

❓ Quick check

The constant-product AMM formula is:

A) x + y = k
B) x × y = k
C) x − y = k
D) x ÷ y = k
(Knowledge check — full exam is next)

Key takeaways

  • AMMs price via x·y = k; trades move the price (slippage)
  • LPs earn fees but bear impermanent loss
  • Lending is over-collateralized; health factor = margin of safety

📝 Weekly Exam — pass with 80% to unlock next week

10 questions. Review the Deep Dive and courses before attempting.

1. In a constant-product AMM, price is set by:
Price = reserve ratio.
2. Slippage in an AMM is:
Your trade moves the pool price.
3. Impermanent loss affects:
LP loss from divergence.
4. Over-collateralization means:
Collateral > loan.
5. A health factor near 1.0 means:
1.0 = liquidation threshold.
6. Liquidators are incentivized by:
They earn a bonus for liquidating.
7. LP fees compensate for:
Fees offset IL.
8. The DeFi thesis is to replace trust with:
Code + incentives replace counterparty trust.
9. If you swap into a small pool, you'll experience:
Small pools = high price impact.
10. The main new risk DeFi introduces vs. traditional finance is:
Code bugs are a new failure mode.
Your score: —

🛠 Weekly Project

Simulate an AMM swap on paper.

1
Set up a small pool: 100 of token A and 100 of token B (k = 10,000).
2
Compute the price of A in B terms (B/A ratio).
3
Swap 10 A in and solve for the new reserves (keep k constant).
4
Compute your effective (slipped) price vs. the pre-swap price.
5
Write one sentence on why small pools have more slippage.
Open tool →
← Week 22  |  Week 24 →