Blockchain and Web3 course Β· Module 11: dApp Development

Building dApps - Decentralized Applications

3 min read Β·
In this lesson6

Welcome to the Heart of the City! Atlas here. Today we'll combine everything you've learned - we'll build a real decentralized application (dApp)!

dApp Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    dApp ARCHITECTURE                              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                  β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                    FRONTEND                              β”‚   β”‚
β”‚   β”‚  React / Next.js / Vue                                  β”‚   β”‚
β”‚   β”‚  + ethers.js / wagmi / viem                             β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                              β”‚                                   β”‚
β”‚                              β”‚ JSON-RPC                          β”‚
β”‚                              β–Ό                                   β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                    WALLET                                β”‚   β”‚
β”‚   β”‚  MetaMask / WalletConnect / Coinbase Wallet             β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                              β”‚                                   β”‚
β”‚                              β”‚ Transactions                      β”‚
β”‚                              β–Ό                                   β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚   β”‚                    BLOCKCHAIN                            β”‚   β”‚
β”‚   β”‚  Ethereum / Polygon / Arbitrum                          β”‚   β”‚
β”‚   β”‚  Smart Contracts                                        β”‚   β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                                                                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Connecting to MetaMask

import { ethers } from "ethers";

// Check if MetaMask is installed
if (typeof window.ethereum === "undefined") {
  throw new Error("MetaMask is not installed!");
}

// Request connection
const accounts = await window.ethereum.request({
  method: "eth_requestAccounts"
});
const userAddress = accounts[0];

// Create provider and signer
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();

console.log("Connected:", userAddress);

Wagmi - React Hooks for Web3

// wagmi is the most popular React library for Web3
import { useAccount, useConnect, useDisconnect, useBalance } from "wagmi";
import { injected } from "wagmi/connectors";

function WalletConnect() {
  const { address, isConnected } = useAccount();
  const { connect } = useConnect();
  const { disconnect } = useDisconnect();
  const { data: balance } = useBalance({ address });

  if (isConnected) {
    return (
      <div>
        <p>Connected: {address}</p>
        <p>Balance: {balance?.formatted} ETH</p>
        <button onClick={() => disconnect()}>Disconnect</button>
      </div>
    );
  }

  return (
    <button onClick={() => connect({ connector: injected() })}>
      Connect Wallet
    </button>
  );
}

wagmi Configuration

import { createConfig, http } from "wagmi";
import { mainnet, sepolia } from "wagmi/chains";
import { injected, walletConnect } from "wagmi/connectors";

const config = createConfig({
  chains: [mainnet, sepolia],
  connectors: [
    injected(),
    walletConnect({ projectId: "YOUR_PROJECT_ID" }),
  ],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
});

Interacting with a Contract (wagmi)

import { useReadContract, useWriteContract } from "wagmi";

const CONTRACT_ADDRESS = "0x...";
const CONTRACT_ABI = [...];

function TokenBalance() {
  const { data: balance } = useReadContract({
    address: CONTRACT_ADDRESS,
    abi: CONTRACT_ABI,
    functionName: "balanceOf",
    args: [userAddress],
  });

  const { writeContract, isPending } = useWriteContract();

  const handleTransfer = () => {
    writeContract({
      address: CONTRACT_ADDRESS,
      abi: CONTRACT_ABI,
      functionName: "transfer",
      args: [recipientAddress, amount],
    });
  };

  return (
    <div>
      <p>Balance: {balance?.toString()}</p>
      <button onClick={handleTransfer} disabled={isPending}>
        {isPending ? "Sending..." : "Transfer"}
      </button>
    </div>
  );
}

Summary

  • dApp Architecture (Frontend + Wallet + Blockchain)
  • Connecting to MetaMask
  • wagmi - React hooks for Web3
  • useAccount, useConnect, useBalance
  • useReadContract, useWriteContract
  • WalletConnect
Code for this lesson: index.js
// dApp Architecture Overview

console.log("=== DAPP ARCHITECTURE ===\n");

const architecture = {
  "Frontend": {
    tech: ["Next.js 14", "React", "TypeScript", "Tailwind CSS"],
    role: "UI, wallet connection, transaction building",
    libraries: ["wagmi", "viem", "RainbowKit", "ethers.js"]
  },
  "Smart Contracts": {
    tech: ["Solidity", "Hardhat/Foundry"],
    role: "Business logic on-chain",
    networks: ["Ethereum", "Arbitrum", "Base", "Polygon"]
  },
  "Indexer": {
    tech: ["The Graph", "Ponder", "Envio"],
    role: "Query historical blockchain data",
    why: "Direct RPC queries are slow/limited"
  },
  "Backend (optional)": {
    tech: ["Node.js", "Vercel Functions", "Supabase"],
    role: "Off-chain data, user accounts, notifications",
    examples: ["Email notifications", "User preferences", "Analytics"]
  },
  "Storage": {
    tech: ["IPFS", "Arweave", "Pinata"],
    role: "NFT metadata, large files",
    why: "On-chain storage is expensive"
  }
};

Object.entries(architecture).forEach(([layer, data]) => {
  console.log(`${layer}`);
  console.log(`   Tech: ${data.tech.join(", ")}`);
  console.log(`   Role: ${data.role}`);
  if (data.libraries) console.log(`   Libraries: ${data.libraries.join(", ")}`);
  if (data.networks) console.log(`   Networks: ${data.networks.join(", ")}`);
  if (data.why) console.log(`   Why: ${data.why}`);
  console.log();
});

console.log("=== TYPICAL DAPP FLOW ===\n");

const flow = [
  "1. User connects wallet (RainbowKit/WalletConnect)",
  "2. Frontend reads contract state (viem/wagmi hooks)",
  "3. User initiates action (e.g., swap, mint)",
  "4. Frontend builds transaction",
  "5. Wallet prompts for signature",
  "6. Transaction sent to network",
  "7. Wait for confirmation",
  "8. Update UI with new state",
  "9. Indexer updates for historical queries"
];

flow.forEach(step => console.log(`  ${step}`));

console.log("\n=== PROJECT STRUCTURE ===\n");

const structure = `
my-dapp/
β”œβ”€β”€ contracts/              # Solidity contracts
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   └── MyContract.sol
β”‚   β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ script/
β”‚   └── foundry.toml
β”œβ”€β”€ frontend/               # Next.js app
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ layout.tsx
β”‚   β”‚   β”œβ”€β”€ page.tsx
β”‚   β”‚   └── providers.tsx
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ ConnectButton.tsx
β”‚   β”‚   └── ContractInteraction.tsx
β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   └── useContract.ts
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ contracts.ts    # ABI + addresses
β”‚   β”‚   └── wagmi.ts        # Wagmi config
β”‚   └── package.json
β”œβ”€β”€ subgraph/               # The Graph indexer
β”‚   β”œβ”€β”€ schema.graphql
β”‚   β”œβ”€β”€ subgraph.yaml
β”‚   └── src/mapping.ts
└── README.md
`;

console.log(structure);

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. A dApp (Decentralized Application) is:

  2. 2. MetaMask is:

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

Hands-on tasks in the game

  • Horizontal ordering

    Arrange the layers of the dApp stack from the user to the blockchain:

  • Click in order

    Arrange the libraries from the highest level of abstraction to the lowest:

  • Vertical ordering

    Arrange the blockchain transaction states in order:

  • Code editor

    Complete the component code

  • Code editor

    Complete the code reading balanceOf

  • Horizontal ordering

    Arrange the wagmi configuration elements in the correct order:

  • Code editor

    Complete the transfer function code

  • Code editor

    Complete the RainbowKit setup

  • Click in order

    Click the transaction states in the order they occur:

  • Vertical ordering

    Arrange the dApp setup steps in the correct order:

  • Code editor

    Complete the switch chain code

  • Code editor

    Complete the ENS resolution code

  • Horizontal ordering

    Arrange the key parameters of useReadContract:

  • Click in order

    Click the chain-related operations in the logical order they're used in a dApp:

  • Code editor

    Complete the code displaying the balance

  • Code editor

    Complete the ConnectKit setup

  • Vertical ordering

    Arrange the network-switching flow in a dApp:

  • Click in order

    Click the components of the wagmi ecosystem from core to UI:

  • Code editor

    Complete the GraphQL query

  • Code editor

    Complete the full component

  • Horizontal ordering

    Arrange the parameters of useWriteContract in order of use:

  1. QuestionQuiz: dApp
  2. QuestionQuiz: MetaMask
  3. QuestionQuiz: window.ethereum
  4. QuestionQuiz: eth_requestAccounts
  5. QuestionQuiz: wagmi
  6. QuestionQuiz: useAccount
  7. Put in orderHorizontal Sorting: dApp Stack
  8. Click in orderSorting: Web3 Libraries
  9. QuestionQuiz: useConnect
  10. QuestionQuiz: useReadContract
  11. QuestionQuiz: useWriteContract
  12. QuestionQuiz: RainbowKit
  13. Put in orderSorting: Transaction Flow
  14. Coding taskGap: Wallet Connection
  15. Coding taskGap: useReadContract for ERC-20
  16. QuestionQuiz: ConnectKit
  17. QuestionQuiz: useSwitchChain
  18. QuestionQuiz: useBalance
  19. QuestionQuiz: viem
  20. Put in orderHorizontal Sorting: wagmi config
  21. Coding taskGap: useWriteContract for a Transfer
  22. Coding taskGap: RainbowKit Integration
  23. Click in orderSorting: Transaction States
  24. QuestionQuiz: The Graph
  25. QuestionQuiz: Subgraph
  26. QuestionQuiz: ENS
  27. QuestionQuiz: Web3Modal
  28. Put in orderSorting: dApp Setup Flow
  29. Coding taskGap: Chain Switching
  30. Coding taskGap: ENS Display
  31. Put in orderHorizontal Sorting: useReadContract config
  32. Click in orderSorting: Chain Operations
  33. QuestionQuiz: Account Abstraction (ERC-4337)
  34. QuestionQuiz: GraphQL in The Graph
  35. QuestionQuiz: useWaitForTransactionReceipt
  36. Coding taskGap: useBalance Hook
  37. Coding taskGap: ConnectKit Setup
  38. Put in orderSorting: Chain Switch Flow
  39. Click in orderSorting: wagmi Ecosystem
  40. Coding taskGap: The Graph Query
  41. Coding taskGap: Full Wallet Component
  42. Put in orderHorizontal Sorting: useWriteContract config
  43. Project to submitSend Your Project to the Mentor
  44. ConversationEnd of Module 11