DeFi Concepts
DeFi is finance rebuilt as open, composable smart contracts. If you squint, it's just stateful microservices trading tokens with ruthlessly-enforced math.
1. The four building blocks
| Primitive | Analogy | Example |
|---|---|---|
| AMM (DEX) | Automated market maker | Uniswap |
| Lending | Pawn shop with math | Aave, Compound |
| Stablecoins | Pegged currency | USDC (fiat-backed), DAI (crypto-collateral) |
| Yield / staking | Savings account with risk | Lido, Curve |
2. AMM — the x·y = k magic
Instead of an order book, Uniswap v2 keeps a pool of two tokens. Price is defined by a constant-product invariant:
$$x \cdot y = k$$
To buy Δy of token Y, you deposit enough X so that new product ≥ k. Slippage emerges naturally: buying more moves the price.
# Python simulator
def swap_x_for_y(x, y, dx, fee=0.003):
dx_net = dx * (1 - fee)
dy = (y * dx_net) / (x + dx_net)
return x + dx, y - dy, dy # new reserves, amount out
x, y = 1000, 1000 # 1:1 pool
x, y, out = swap_x_for_y(x, y, 10)
print(out, y/x) # ~9.87 Y out, new price ~1.02
3. Liquidity providers (LPs)
Anyone can deposit equal value of X and Y; the pool mints LP tokens representing a share. Fees (0.3% per swap on v2) accrue to LPs. Redeem LP tokens → get back proportional slice of pool.
4. Uniswap v3 — concentrated liquidity
Instead of liquidity spread from 0 to ∞, LPs specify a price range. Capital efficiency jumps ~1000× but requires active management. Think "limit orders that earn fees while waiting".
5. Lending (Aave / Compound)
- Utilization-based rates — empty pool = cheap borrow; full pool = expensive (steep curve at 80%+).
- Overcollateralization — you must deposit more than you borrow. No identity needed; the math is the underwriter.
- Liquidations are keeper-bot territory; a profession unto itself.
6. Flash loans
Borrow any amount within one transaction with zero collateral. If you don't repay by the end, the entire tx reverts — so the loan either succeeds or never happened.
// pseudo
function flashLoan(IERC20 token, uint256 amount, bytes calldata data) external {
uint256 before = token.balanceOf(address(this));
token.transfer(msg.sender, amount);
IBorrower(msg.sender).onFlashLoan(token, amount, data);
require(token.balanceOf(address(this)) >= before + fee, "not repaid");
}
Legitimate use: arbitrage, collateral swaps, liquidations. Malicious use: oracle manipulation (Phase 10).
7. Stablecoins — three flavors
| Type | Peg mechanism | Risk |
|---|---|---|
| Fiat-backed (USDC, USDT) | 1 token ↔ 1 USD in bank | Issuer freezes, bank fails |
| Crypto-collateralized (DAI) | Overcollateralized ETH → mint DAI | Collateral crash |
| Algorithmic (failed mostly) | Code tries to maintain peg | Death spiral (see UST/LUNA, 2022) |
8. Staking & yield
- Protocol staking — lock ETH to validate; ~3–5% APY.
- Liquid staking (Lido, Rocket Pool) — deposit ETH, get stETH that you can still DeFi with.
- Yield farming — chase rates across protocols. 100% APY usually means 100% inflation of a governance token.
9. Composability — "money legos"
Because every contract is on the same chain with public ABIs, you can chain them in one tx: flash-loan USDC → deposit to Aave → borrow ETH → swap on Uniswap → supply to Curve LP → repay flash loan. This is why DeFi UX is "a bundle of calls", not a sequence of services.
10. Project
Pool.addLiquidity / removeLiquidity / swap with mint/burn LP tokens. Fuzz-test the invariant x·y ≥ k.Quiz
- 2,000 USDC (linear price)
- ~1,818 USDC — new reserves 11·18,181, k preserved
- 2,200 USDC
- Depends on an oracle