Study notesWeek 8: DeFi: Markets Built From Contracts
Week 8 / Blockchain from zero

DeFi: Markets Built From Contracts

Week 8 · Blockchain

What You Can Do By The End

Week 8 · Blockchain

DeFi In One Idea

Week 8 · Blockchain

From Order Book To Pool

x * y = k

The Constant Product Rule

02856841121005025201010204050100
pool: 1000 A, 1000 B   (k = 1,000,000)
trader adds 100 A -> pool 1100 A
B must be 1,000,000 / 1100 = 909.09
trader receives 90.91 B (not 100)
Week 8 · Blockchain

Simulating A Swap In Python

def quote(dx, x, y, fee=0.003):
    dx_eff = dx * (1 - fee)
    return y * dx_eff / (x + dx_eff)

x, y = 1000.0, 1000.0
for dx in (10, 100, 500):
    print(dx, round(quote(dx, x, y), 2))
Week 8 · Blockchain

Liquidity Providers

Week 8 · Blockchain

Lending Pools

Week 8 · Blockchain

Overcollateralization And Liquidation

collateral value  150  ->  120
debt              100
ratio 1.50 -> 1.20 (below limit)
liquidator repays debt, gets collateral + bonus
Week 8 · Blockchain

Stablecoin Designs

DesignBacking
Fiat backedReserves held by an issuer
Crypto backedOvercollateralised vaults
AlgorithmicRules and incentives only
Week 8 · Blockchain

Oracles Bring Outside Data

Week 8 · Blockchain

Oracle Manipulation

1. flash-borrow a large amount
2. swap to inflate price in a thin pool
3. borrow against the inflated price
4. repay the flash loan, keep the profit
Week 8 · Blockchain

Composability And Flash Loans

Risk map

Layers Of Risk

Week 8 · Blockchain

A Review Checklist

Week 8 · Blockchain

Recap

Week 8 · Blockchain

Next Week: Scaling

Week 8 · Blockchain

Questions And Exercise