Building a full-stack Web3 dApp using Next.js (with the App Router) and an EVM chain requires a clean separation between the smart contract logic and the Next.js application.
The most effective structure often involves a monorepo-like setup where your Next.js frontend and your Hardhat/Foundry smart contract project reside side-by-side.
A recommended structure separates the blockchain logic from the application logic.
my-fullstack-dapp/
βββ **contracts/** <-- Smart Contract Project (Hardhat/Foundry)
β βββ contracts/ # Your Solidity files (*.sol)
β βββ scripts/ # Deployment/interaction scripts
β βββ test/ # Contract tests
β βββ hardhat.config.js
βββ **dapp/** <-- Next.js Application (App Router)
β βββ app/ # Next.js Routes & Layouts
β β βββ (main)/ # Route Group for primary pages
β β β βββ layout.tsx
β β β βββ page.tsx # Home page (e.g., wallet connect)
β β β βββ dashboard/ # Protected routes
β β β βββ page.tsx
β β βββ providers.tsx # Client Component wrapping WAGMI/RainbowKit
β β βββ layout.tsx # Root layout
β βββ public/ # Static assets (images, favicon)
β βββ src/
β β βββ components/ # Reusable UI components (buttons, headers)
β β βββ **web3/** # Web3-specific configurations and types
β β β βββ config.ts # Wagmi/Viem configuration (chains, connectors)
β β β βββ abi/ # Copied/generated Contract ABIs (*.json)
β β β βββ hooks/ # Custom Web3 hooks (e.g., useNFTBalance)
β β βββ **lib/** # Utility files & Off-chain logic
β β β βββ utils.ts # General utilities
β β β βββ actions.ts # Next.js Server Actions (for off-chain database)
β β βββ styles/
β βββ .env.local # WalletConnect Project ID, Alchemy/Infura RPC keys
β βββ package.json
βββ package.json # Root package.json (optional, for yarn/npm workspaces)
A. Smart Contract Development (contracts/)
-
Initialize Contracts Project: Use Hardhat or Foundry inside the contracts/ directory.
npx hardhat
-
Write and Test: Develop your Solidity code (e.g., an ERC-20 or NFT contract) and write comprehensive tests.
-
Compile & Deploy: Compile the contracts and write deployment scripts.
npx hardhat compile npx hardhat run scripts/deploy.js --network sepolia -
Copy Artifacts: After deployment, copy the contract's ABI and Address to your Next.js project so the frontend can interact with it.
- ABI: Copy the generated JSON ABI file into dapp/src/web3/abi/.
- Address: Store the deployed address in a configuration file like dapp/src/web3/config.ts or an environment variable.
B. Next.js Frontend Setup (dapp/)
- Initialize Next.js: Create the Next.js app inside the dapp/ directory.
npx create-next-app@latest dapp --ts --app
cd dapp
-
Install Web3 Dependencies:
npm install wagmi viem @rainbow-me/rainbowkit @tanstack/react-query
-
Configure Wagmi/Viem (dapp/src/web3/config.ts): Define the chains and providers for your dApp. You must obtain a WalletConnect Project ID and set it in your .env.local file.
// dapp/src/web3/config.ts
import { createConfig, http } from 'wagmi';
import { mainnet, sepolia } from 'wagmi/chains';
// ... define connectors and transports- Create Web3 Provider Wrapper (dapp/app/providers.tsx): This is a Client Component that wraps your application with the necessary context for Wagmi and RainbowKit.
// dapp/app/providers.tsx - MUST include 'use client'
'use client';
import { WagmiProvider } from 'wagmi';
import { RainbowKitProvider } from '@rainbow-me/rainbowkit';
// ... QueryClientProvider and imports
export function Providers({ children }) {
// ... return providers (WagmiProvider, QueryClientProvider, RainbowKitProvider)
return (/* ... */)
}- Integrate Providers: Include providers.tsx in your root dapp/app/layout.tsx.
C. Component Interaction
- Wallet Connect: Place the from RainbowKit in a Client Component (e.g., dapp/src/components/ConnectWallet.tsx).
- Read Contract Data: Use the useReadContract Wagmi hook in any Client Component to fetch contract data.
- Write Transactions: Use useWriteContract and useWaitForTransactionReceipt to handle sending transactions (e.g., minting, swapping). This video provides a step-by-step tutorial for building a complete Ethereum dApp with Next.js, Wagmi, and RainbowKit, offering a practical demonstration of this directory structure and workflow. Build an Ethereum DApp with Next.js, Wagmi, RainbowKit, and Pinata to Mint NFTs on IPFS.