Week 8 / Blockchain from zero
DeFi: Markets Built From Contracts
- Week 8 of Blockchain from zero
- Swaps, loans and stable money, made of tokens and code.
Week 8 · Blockchain
What You Can Do By The End
- Price a swap in a constant product pool
- Explain collateral and liquidation
- Compare stablecoin designs
- Explain oracles and their attacks
- Assess a protocol's risks
Week 8 · Blockchain
DeFi In One Idea
- Contracts hold tokens and apply the rules.
- Everything settles in a single transaction.
- No account, only a wallet and a signature.
- No support desk can reverse a mistake.
Week 8 · Blockchain
From Order Book To Pool
- An order book lists buy and sell offers.
- An AMM pool holds reserves of two tokens.
- A formula sets the price.
- Anyone can swap at any time.
x * y = k
The Constant Product Rule
- Reserves x and y must keep x times y.
- Adding token A forces token B out.
- Bigger trades get worse prices.
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
- Fee is taken from the input amount.
- Output follows from the reserves.
- Run it with several trade sizes.
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
- Providers deposit both tokens into a pool.
- They receive shares and part of the fees.
- Impermanent loss appears when prices diverge.
- Fees may or may not cover it.
Week 8 · Blockchain
Lending Pools
- Depositors supply tokens and earn interest.
- Borrowers lock collateral first.
- Rates rise as more of the pool is borrowed.
- No one needs to know who you are.
Week 8 · Blockchain
Overcollateralization And Liquidation
- Borrow 100, lock 150 of another token.
- If collateral falls, the loan turns risky.
- Anyone may repay debt and take collateral at a discount.
- Fast crashes can still cause bad debt.
collateral value 150 -> 120
debt 100
ratio 1.50 -> 1.20 (below limit)
liquidator repays debt, gets collateral + bonus
Week 8 · Blockchain
Stablecoin Designs
- Fiat-backed: reserves held by an issuer.
- Crypto-collateralized: overcollateralized loans.
- Algorithmic: incentives with little collateral.
- Each design fails in a different way.
| Design | Backing |
|---|
| Fiat backed | Reserves held by an issuer |
| Crypto backed | Overcollateralised vaults |
| Algorithmic | Rules and incentives only |
Week 8 · Blockchain
Oracles Bring Outside Data
- Contracts see only on-chain data.
- A price oracle reports market prices.
- Liquidations depend on those prices.
- Bad data means bad liquidations.
Week 8 · Blockchain
Oracle Manipulation
- One small pool is easy to move.
- Attacker pushes the price, then borrows against it.
- Safer: many sources, time averages, independent reporters.
- Add limits on sudden price jumps.
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
- Contracts are public and share token standards.
- One transaction can chain many protocols.
- A flash loan must be repaid in the same transaction.
- A flaw in one piece can spread.
Risk map
Layers Of Risk
- Code: bugs and upgrade keys.
- Economics: designs that break under stress.
- Data: oracle failure.
- People: admin powers and governance.
- Markets: slippage, liquidation, front-running.
Week 8 · Blockchain
A Review Checklist
- Read the docs and find who holds admin keys.
- Check audits and open findings.
- Look for time delays on upgrades.
- Look at track record and value held.
- Start small, or start on a test network.
Week 8 · Blockchain
Recap
- AMMs price swaps from reserves.
- Lending relies on collateral and liquidation.
- Stablecoins need real backing.
- Oracles and composability add risk and power.
Week 8 · Blockchain
Next Week: Scaling
- Why Ethereum fees rise when demand grows.
- Rollups, optimistic and zero-knowledge.
- Bridges and the layer 2 landscape.
Week 8 · Blockchain
Questions And Exercise
- Run the AMM script with three trade sizes.
- Find a collateral factor in a lending doc.
- Write your five-line protocol review.