Back to Blockchain & Web3

Module 5: DeFi Development

Build decentralized financial applications on blockchain

💰 What is DeFi?

DeFi (Decentralized Finance) recreates traditional financial services (banking, lending, trading) without intermediaries like banks. Everything runs on smart contracts.

Simple Analogy

Traditional finance is like a restaurant where waiters take your order and bring food. DeFi is like a vending machine - you interact directly, no middleman needed.

Traditional Finance vs DeFi

Traditional Finance

  • • Banks control your money
  • • Need permission to access
  • • Limited hours (9-5)
  • • High fees
  • • Slow (days for transfers)
  • • Requires identity verification

DeFi

  • • You control your money
  • • Permissionless (anyone can use)
  • • 24/7 availability
  • • Lower fees
  • • Fast (minutes)
  • • Pseudonymous

💡 Key DeFi Concepts:

  • Non-custodial: You hold your own keys
  • Composability: DeFi apps work together like Lego blocks
  • Transparency: All transactions are public
  • Programmable: Smart contracts automate everything

🪙 Token Standards

Token standards define how tokens behave on Ethereum. They ensure compatibility across wallets and dApps.

ERC-20: Fungible Tokens

Standard for cryptocurrencies and utility tokens. Each token is identical and interchangeable (like dollars - one $1 bill equals any other $1 bill).

// ERC-20 Interface
interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

Examples:

USDC, DAI, LINK, UNI - Most DeFi tokens are ERC-20

ERC-721: Non-Fungible Tokens (NFTs)

Each token is unique and not interchangeable (like trading cards - each one is different).

// ERC-721 Key Functions
interface IERC721 {
    function ownerOf(uint256 tokenId) external view returns (address);
    function transferFrom(address from, address to, uint256 tokenId) external;
    function approve(address to, uint256 tokenId) external;
    function tokenURI(uint256 tokenId) external view returns (string memory);
    
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
}

Examples:

CryptoPunks, Bored Ape Yacht Club, digital art, game items

ERC-1155: Multi-Token Standard

Can handle both fungible and non-fungible tokens in one contract. Efficient for gaming.

Use Cases:

Gaming (100 swords + 1 unique legendary sword in same contract)

📚 Learn More:

🔄 DEX & AMM: Decentralized Trading

Decentralized Exchanges (DEX) let you trade tokens without a centralized company. Automated Market Makers (AMM) use math instead of order books.

How AMMs Work

Simple Analogy: Seesaw

Imagine a seesaw with ETH on one side and USDC on the other. The AMM keeps them balanced using a formula. When you buy ETH, you add USDC and remove ETH, tilting the seesaw and changing the price.

// Constant Product Formula (Uniswap V2)
// x * y = k (constant)
// x = amount of token A
// y = amount of token B
// k = constant product

// Example:
// Pool has 100 ETH and 200,000 USDC
// k = 100 * 200,000 = 20,000,000

// If you buy 10 ETH:
// New ETH amount: 90
// New USDC amount: k / 90 = 222,222
// You pay: 222,222 - 200,000 = 22,222 USDC
// Price per ETH: 2,222 USDC

Liquidity Pools

What are they?

Smart contracts holding pairs of tokens (e.g., ETH/USDC pool)

Liquidity Providers (LPs)

Users who deposit tokens into pools and earn trading fees

LP Tokens

Receipt tokens representing your share of the pool

Impermanent Loss

Risk that token prices diverge, reducing your value vs holding

💡 Popular DEXs:

  • Uniswap: Largest DEX, simple AMM
  • Curve: Optimized for stablecoins
  • Balancer: Multi-token pools
  • SushiSwap: Uniswap fork with extra features

📚 Learn More:

🏦 Lending & Borrowing

DeFi lending protocols let you earn interest by lending crypto or borrow against your holdings - all without a bank.

How It Works

Lending (Supply)

Deposit your tokens into a lending pool. Earn interest from borrowers. Withdraw anytime.

Example: Deposit 1000 USDC, earn 5% APY = 50 USDC per year

Borrowing

Deposit collateral (e.g., ETH), borrow other tokens (e.g., USDC). Pay interest. Must maintain collateral ratio or get liquidated.

Example: Deposit $10,000 ETH, borrow $6,000 USDC (60% collateral ratio)

Liquidation

If collateral value drops too much, liquidators can repay your debt and take your collateral at a discount. This protects lenders.

If ETH drops and your collateral is now worth $7,000, you might get liquidated

💡 Popular Lending Protocols:

  • Aave: Largest lending protocol, flash loans
  • Compound: Algorithmic interest rates
  • MakerDAO: Mint DAI stablecoin against collateral

📚 Learn More:

🎯 What's Next?

You now understand DeFi fundamentals! In the next module, we'll learn how to build Web3 frontends that interact with smart contracts.