Blockchain and Web3 course Β· Module 10: DeFi & Staking

DeFi - Decentralized Finance

3 min read Β·
In this lesson5

Welcome to the Underground Market! Atlas here. Today you'll learn about DeFi - decentralized finance, meaning exchanges and financial services operating without intermediaries.

What is DeFi?

DeFi (Decentralized Finance) is an ecosystem of financial applications built on blockchain. Instead of banks and exchanges - smart contracts!

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              TRADITIONAL FINANCE vs DeFi                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  TRADITIONAL (CeFi)          β”‚  DECENTRALIZED (DeFi)            β”‚
β”‚  ─────────────────           β”‚  ─────────────────────           β”‚
β”‚  Bank                        β”‚  Smart Contract                  β”‚
β”‚  Business hours              β”‚  24/7/365                        β”‚
β”‚  KYC required                β”‚  Permissionless                  β”‚
β”‚  Controlled by institution   β”‚  Controlled by user              β”‚
β”‚  Requires trust              β”‚  Trustless (code is law)         β”‚
β”‚  Slow processing             β”‚  Fast transactions               β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Main DeFi Categories

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    DeFi ECOSYSTEM                                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  DEX (Decentralized Exchange)                                   β”‚
β”‚  └── Uniswap, SushiSwap, Curve                                  β”‚
β”‚                                                                  β”‚
β”‚  Lending/Borrowing                                              β”‚
β”‚  └── Aave, Compound, MakerDAO                                   β”‚
β”‚                                                                  β”‚
β”‚  Stablecoins                                                    β”‚
β”‚  └── DAI, USDC, USDT                                            β”‚
β”‚                                                                  β”‚
β”‚  Yield Farming                                                  β”‚
β”‚  └── Yearn, Convex                                              β”‚
β”‚                                                                  β”‚
β”‚  Derivatives                                                    β”‚
β”‚  └── dYdX, GMX                                                  β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

AMM - Automated Market Maker

Traditional exchanges use an order book. DEXes use AMM:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    AMM (Uniswap v2)                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚  Liquidity Pool: ETH/USDC                                       β”‚
β”‚                                                                  β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚    ETH: 100          β”‚         USDC: 200,000            β”‚    β”‚
β”‚  β”‚                      β”‚                                   β”‚    β”‚
β”‚  β”‚    x * y = k (constant product formula)                 β”‚    β”‚
β”‚  β”‚    100 * 200,000 = 20,000,000                          β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                                                  β”‚
β”‚  Price: 200,000 / 100 = 2,000 USDC per ETH                      β”‚
β”‚                                                                  β”‚
β”‚  Swap: You want to buy 1 ETH                                    β”‚
β”‚  New state: 99 ETH, ~202,020 USDC                               β”‚
β”‚  You pay: ~2,020 USDC (slippage!)                               β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Swap on Uniswap

import { ethers } from "ethers";

// Uniswap V2 Router
const ROUTER_ABI = [
  "function swapExactETHForTokens(uint amountOutMin, address[] path, address to, uint deadline) payable returns (uint[] amounts)",
  "function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] path, address to, uint deadline) returns (uint[] amounts)",
  "function getAmountsOut(uint amountIn, address[] path) view returns (uint[] amounts)"
];

const router = new ethers.Contract(UNISWAP_ROUTER, ROUTER_ABI, wallet);

// Check price
const amountsOut = await router.getAmountsOut(
  ethers.parseEther("1"), // 1 ETH
  [WETH_ADDRESS, USDC_ADDRESS]
);
console.log("1 ETH =", ethers.formatUnits(amountsOut[1], 6), "USDC");

// Swap ETH β†’ Token
const tx = await router.swapExactETHForTokens(
  amountsOut[1] * 95n / 100n, // min 95% (5% slippage)
  [WETH_ADDRESS, USDC_ADDRESS],
  myAddress,
  Math.floor(Date.now() / 1000) + 60 * 20, // deadline 20 min
  { value: ethers.parseEther("1") }
);
await tx.wait();

Summary

  • DeFi - finance without intermediaries
  • DEX - decentralized exchanges
  • AMM - Automated Market Maker
  • Liquidity Pools
  • Swap on Uniswap
  • Slippage and protection
Code for this lesson: index.js
// DeFi Fundamentals - basic concepts

console.log("=== DEFI FUNDAMENTALS ===\n");

const defiConcepts = {
  "DEX (Decentralized Exchange)": {
    description: "Token exchange without intermediaries",
    examples: ["Uniswap", "SushiSwap", "Curve", "Raydium (Solana)"],
    mechanism: "AMM (Automated Market Maker)"
  },

  "Lending/Borrowing": {
    description: "Lending and earning on deposits",
    examples: ["Aave", "Compound", "MakerDAO"],
    mechanism: "Overcollateralized loans"
  },

  "Yield Farming": {
    description: "Optimizing returns from different protocols",
    examples: ["Yearn Finance", "Convex", "Beefy"],
    mechanism: "Auto-compounding, strategy vaults"
  },

  "Liquid Staking": {
    description: "Staking while maintaining liquidity",
    examples: ["Lido (stETH)", "Rocket Pool", "Marinade (Solana)"],
    mechanism: "Liquid Staking Derivatives (LSDs)"
  },

  "Stablecoins": {
    description: "Tokens pegged to USD",
    examples: ["USDC (fiat-backed)", "DAI (crypto-backed)", "FRAX (algorithmic)"],
    mechanism: "Different stabilization mechanisms"
  }
};

Object.entries(defiConcepts).forEach(([name, data]) => {
  console.log(`${name}`);
  console.log(`   ${data.description}`);
  console.log(`   Mechanism: ${data.mechanism}`);
  console.log(`   Examples: ${data.examples.join(", ")}`);
  console.log();
});

console.log("=== KEY METRICS ===\n");

const metrics = {
  "TVL (Total Value Locked)": "Total assets in the protocol",
  "APY (Annual Percentage Yield)": "Annual return with compounding",
  "APR (Annual Percentage Rate)": "Annual return without compounding",
  "Impermanent Loss": "Loss from providing liquidity vs HODL",
  "Utilization Rate": "% of borrowed funds from the pool"
};

Object.entries(metrics).forEach(([metric, desc]) => {
  console.log(`  ${metric}: ${desc}`);
});

console.log("\n=== TOP DEFI PROTOCOLS BY TVL ===\n");

const protocols = [
  { name: "Lido", tvl: "$28B", category: "Liquid Staking" },
  { name: "Aave", tvl: "$12B", category: "Lending" },
  { name: "MakerDAO", tvl: "$8B", category: "CDP/Stablecoin" },
  { name: "Uniswap", tvl: "$5B", category: "DEX" },
  { name: "Curve", tvl: "$4B", category: "DEX (Stables)" }
];

protocols.forEach((p, i) => {
  console.log(`${i+1}. ${p.name} - ${p.tvl} (${p.category})`);
});

console.log("\nSource: DeFiLlama.com");

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. DeFi (Decentralized Finance) is:

  2. 2. DEX (Decentralized Exchange) is:

These are 2 of 17 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Complete the swap code:

  • Code editor

    Complete the addLiquidity code:

  • Code editor

    Complete the removeLiquidity code:

  • Code editor

    Complete the getPrice function:

  • Vertical ordering

    Arrange the steps of a swap on Uniswap:

  • Vertical ordering

    Arrange the steps for adding liquidity:

  • Horizontal ordering

    Arrange the parameters for calling swapExactETHForTokens:

  • Horizontal ordering

    Arrange the parameters of addLiquidity:

  • Click in order

    Click in the order of the steps for creating an LP position:

  • Click in order

    Click in the order of executing the swap:

  • Code editor

    Complete the executeOperation callback:

  • Code editor

    Complete the deposit code:

  • Code editor

    Complete the borrow code:

  • Code editor

    Complete the repay code:

  • Vertical ordering

    Arrange the flash loan steps (all in 1 transaction):

  • Vertical ordering

    Arrange the lending steps (borrowing with collateral):

  • Horizontal ordering

    Arrange the parameters of flashLoan (Aave):

  • Horizontal ordering

    Arrange the parameters of deposit (Aave):

  • Click in order

    Click in the order of the borrowing steps:

  • Click in order

    Click in the order of checking for liquidation:

  • Code editor

    Complete the getLatestPrice code:

  • Code editor

    Complete the calculateRewards function:

  • Vertical ordering

    Arrange the yield farming steps:

  • Horizontal ordering

    Arrange the steps for staking LP tokens:

  • Click in order

    Click in the order of calculating TVL (Total Value Locked):

  1. QuestionQuiz: DeFi
  2. QuestionQuiz: DEX
  3. QuestionQuiz: AMM
  4. QuestionQuiz: Liquidity Pool
  5. Coding taskSwap ETH for USDC
  6. Coding taskAdding Liquidity
  7. Coding taskRemoving Liquidity
  8. Coding taskCalculating Price
  9. QuestionQuiz: Uniswap
  10. QuestionQuiz: Slippage
  11. QuestionQuiz: x * y = k
  12. QuestionQuiz: LP Tokens
  13. Put in orderSorting: Swap on Uniswap
  14. Put in orderSorting: Adding Liquidity
  15. Put in orderSyntax: swapExactETHForTokens
  16. Put in orderSyntax: addLiquidity
  17. Click in orderBuilding: LP Position
  18. Click in orderBuilding: Swap Flow
  19. QuestionQuiz: Impermanent Loss
  20. QuestionQuiz: Flash Loans
  21. QuestionQuiz: Collateralization
  22. QuestionQuiz: Oracles
  23. Coding taskFlash Loan Implementation
  24. Coding taskLending: Deposit
  25. Coding taskBorrowing
  26. Coding taskRepay Loan
  27. Put in orderSorting: Flash Loan Flow
  28. Put in orderSorting: Lending Flow
  29. Put in orderSyntax: flashLoan
  30. Put in orderSyntax: deposit (lending)
  31. Click in orderBuilding: Borrow Flow
  32. Click in orderBuilding: Liquidation Check
  33. QuestionQuiz: Liquidation
  34. QuestionQuiz: Concentrated Liquidity
  35. QuestionQuiz: MEV
  36. QuestionQuiz: Sandwich Attack
  37. Coding taskChainlink Price Feed
  38. Coding taskStaking Rewards
  39. Put in orderSorting: Yield Farming
  40. Put in orderSyntax: stake LP
  41. Click in orderBuilding: TVL Check
  42. QuestionQuiz: TVL
  43. Project to submitSend Your Project to the Mentor
  44. ConversationEnd of Module 10