Blockchain and Web3 course Β· Module 11: dApp Development
Building dApps - Decentralized Applications
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. A dApp (Decentralized Application) is:
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:
Tasks after this lesson
- QuestionQuiz: dApp
- QuestionQuiz: MetaMask
- QuestionQuiz: window.ethereum
- QuestionQuiz: eth_requestAccounts
- QuestionQuiz: wagmi
- QuestionQuiz: useAccount
- Put in orderHorizontal Sorting: dApp Stack
- Click in orderSorting: Web3 Libraries
- QuestionQuiz: useConnect
- QuestionQuiz: useReadContract
- QuestionQuiz: useWriteContract
- QuestionQuiz: RainbowKit
- Put in orderSorting: Transaction Flow
- Coding taskGap: Wallet Connection
- Coding taskGap: useReadContract for ERC-20
- QuestionQuiz: ConnectKit
- QuestionQuiz: useSwitchChain
- QuestionQuiz: useBalance
- QuestionQuiz: viem
- Put in orderHorizontal Sorting: wagmi config
- Coding taskGap: useWriteContract for a Transfer
- Coding taskGap: RainbowKit Integration
- Click in orderSorting: Transaction States
- QuestionQuiz: The Graph
- QuestionQuiz: Subgraph
- QuestionQuiz: ENS
- QuestionQuiz: Web3Modal
- Put in orderSorting: dApp Setup Flow
- Coding taskGap: Chain Switching
- Coding taskGap: ENS Display
- Put in orderHorizontal Sorting: useReadContract config
- Click in orderSorting: Chain Operations
- QuestionQuiz: Account Abstraction (ERC-4337)
- QuestionQuiz: GraphQL in The Graph
- QuestionQuiz: useWaitForTransactionReceipt
- Coding taskGap: useBalance Hook
- Coding taskGap: ConnectKit Setup
- Put in orderSorting: Chain Switch Flow
- Click in orderSorting: wagmi Ecosystem
- Coding taskGap: The Graph Query
- Coding taskGap: Full Wallet Component
- Put in orderHorizontal Sorting: useWriteContract config
- Project to submitSend Your Project to the Mentor
- ConversationEnd of Module 11