BlockchainIntermediate16 min read

DeFi: Decentralized Finance Explained from Scratch

A comprehensive guide to the world of decentralized finance — what it is, how Uniswap, Aave, and Compound work, liquidity pools, impermanent loss, and how to participate safely.

1

What Is DeFi?

Decentralized Finance (DeFi) refers to financial services — lending, borrowing, trading, earning interest, insurance — built on public blockchains and governed by smart contracts rather than centralized institutions.

The fundamental difference from traditional finance: there is no bank, broker, or company in the middle. When you swap tokens on Uniswap, borrow USDC on Aave, or earn yield on Compound, you interact directly with smart contract code. No account approval, no credit check, no business hours — the protocol runs 24/7.

Scale: As of 2024, DeFi protocols hold over $80 billion in Total Value Locked (TVL). Peak was $180 billion in November 2021 before the bear market.

Key DeFi primitives: - DEXs (Decentralized Exchanges): Trade tokens without a centralized intermediary — Uniswap, Curve, dYdX - Lending Protocols: Earn interest on deposits, borrow against collateral — Aave, Compound, MakerDAO - Yield Aggregators: Auto-compound yield across protocols — Yearn Finance, Convex - Perpetuals: Trade leveraged positions trustlessly — dYdX, GMX, Hyperliquid - Options: Decentralized options markets — Lyra, Dopex - Insurance: Smart contract coverage — Nexus Mutual

2

Automated Market Makers and DEXs

Traditional exchanges match buyers with sellers in order books. DEXs use a fundamentally different mechanism — Automated Market Makers (AMMs).

Uniswap v2: The constant product formula Instead of an order book, Uniswap uses liquidity pools — reserves of two tokens locked in a smart contract. The price is determined by the ratio of assets:

x × y = k (where x and y are token reserves, k is a constant)

Example: Pool has 100 ETH and 300,000 USDC (price = $3,000/ETH). Someone buys 10 ETH: - New ETH reserve: 90 ETH - USDC needed to maintain k: 300,000 × (100/90) = 333,333 USDC - They paid 33,333 USDC for 10 ETH = $3,333/ETH average (vs $3,000 spot)

The price difference is slippage — it increases with trade size relative to pool depth.

Uniswap v3: Concentrated Liquidity LPs can provide liquidity within a specific price range (e.g., only between $2,500-$3,500 for ETH/USDC). This dramatically increases capital efficiency — up to 4,000× more efficient — but requires active management when price exits the range.

Curve Finance: Optimizes for stablecoin swaps using a hybrid formula (StableSwap) — enabling $100M USDC→USDT trades with less than $500 slippage. Dominates stablecoin and pegged-asset trading.

3

Lending and Borrowing: Aave and Compound

DeFi lending protocols allow users to earn interest without a bank and borrow without a credit check.

How Aave works (supply side): 1. Deposit 10,000 USDC → receive 10,000 aUSDC (interest-bearing token) 2. aUSDC balance increases every second as interest accrues 3. Redeem aUSDC for USDC + accumulated interest at any time

Interest rate model: Rates are algorithmically determined by the utilization rate (what fraction of deposits are currently borrowed). High utilization → high rates (incentivizes more deposits). Low utilization → low rates (incentivizes more borrowing). This creates a self-balancing market.

Borrowing (borrow side): 1. Deposit ETH as collateral (e.g., $10,000 of ETH) 2. Borrow up to 80% LTV ($8,000 USDC) against collateral 3. Pay variable borrow interest rate

Health Factor: collateral value / (debt × liquidation threshold). If Health Factor drops below 1.0, any address can liquidate the position — repaying debt and receiving collateral at 5-10% discount.

Flash Loans: Aave's revolutionary innovation — uncollateralized loans within a single transaction. Borrow $100M, use it, repay it — all in one atomic transaction (≤12 seconds on Ethereum). If not repaid in the same transaction, the whole thing reverts. Used for arbitrage, collateral swaps, and by attackers for price manipulation.

4

Liquidity Pools and Impermanent Loss

Liquidity providers (LPs) deposit equal values of two tokens into a pool, earning a share of trading fees (0.05–1% per swap). But there's a critical risk: impermanent loss (IL).

Impermanent loss example: You deposit 1 ETH + $2,000 USDC when ETH = $2,000 (50/50 split, $4,000 total). ETH price doubles to $4,000.

  • If you had just held: 1 ETH ($4,000) + $2,000 = $6,000
  • As LP: The pool auto-rebalances. You now hold ≈0.707 ETH + $2,828 USDC = $5,657
  • Impermanent loss: $6,000 − $5,657 = $343 (5.7%) vs just holding

This loss is "impermanent" because if ETH returns to $2,000, the loss disappears. If you withdraw at $4,000, the loss locks in permanently.

When is LP profitable despite IL? - When trading fees earned exceed impermanent loss - High-volume pairs like ETH/USDC on Uniswap v3 can generate 20-50% APY in fees - Stablecoin pairs (USDC/USDT) have minimal IL since both assets maintain $1 - Low-volatility pairs: wrapped assets (WBTC/renBTC), same-asset derivatives

Rule of thumb: If you believe an asset will move more than 2× in either direction relative to the paired asset, IL will likely exceed fees earned. Stablecoin pools are safest for LP income.

5

Yield Farming and Liquidity Mining

Yield farming: Using DeFi protocols in combination to maximize returns — depositing LP tokens as collateral to borrow more capital, then farming with that capital. Can generate high APYs but compounds risk.

Liquidity mining: Protocols incentivize liquidity by distributing their native governance token to LPs in addition to trading fees. In 2020-2021, protocols like Compound (COMP), Uniswap (UNI), and SushiSwap (SUSHI) distributed billions in token rewards.

The incentive flywheel: 1. Protocol distributes tokens to LPs 2. High APYs attract more liquidity 3. More liquidity enables larger trades with less slippage 4. More trading volume → more fee revenue for LPs 5. Protocol value increases → governance tokens appreciate

The sustainability problem: Token emissions create selling pressure as farmers sell rewards immediately. When token prices drop, APYs fall, LPs exit, liquidity drops, fees fall, token prices fall further — a death spiral. Most aggressive yield farming programs from 2021 have collapsed. Sustainable DeFi projects focus on protocol revenue (real fees) rather than token emission yields.

Real yield: The new DeFi narrative — protocols like GMX and Gains Network distribute actual trading fees (in ETH or stablecoins) to stakers, not inflationary token emissions. More sustainable but lower peak APYs.

6

DeFi Risks and Safety Practices

DeFi carries unique risks that traditional finance doesn't have:

Smart contract bugs: Code can have exploitable vulnerabilities. Major hacks: - Euler Finance (2023): $197M drained via flash loan - Wormhole bridge (2022): $325M stolen via signature forgery - Nomad bridge (2022): $190M — attackers simply copied the initial exploit transaction - Compound (2021): $90M accidentally sent to wrong addresses via governance bug

Oracle manipulation: Flash loans can manipulate price oracles within one transaction, enabling undercollateralized borrowing. The $130M Cream Finance hack used this technique.

Rug pulls: New project launches with attractive yields → developers drain liquidity → token crashes to zero. Red flags: anonymous team, unaudited contracts, admin keys with no timelock, pre-sale tokens unlocking immediately.

Composability risk: DeFi protocols interlock. When one fails, effects cascade. The 2022 UST collapse caused cascading liquidations across Anchor, Mirror, and dozens of dependent protocols.

How to protect yourself: - Only use protocols with 1+ year of live operation without hacks - Check audits (multiple, from reputable firms like Trail of Bits, OpenZeppelin, Certik) - Use DeFiLlama to monitor TVL trends — rapid TVL outflows precede failures - Never invest more than you can afford to lose in any single protocol - Start small, understand the mechanism before committing large amounts

Practice in a risk-free environment

Apply the concepts using virtual funds and live market data. NexChange is an educational simulation, not a real-money exchange.

Continue learning