Skip to content

Latest commit

Β 

History

History
104 lines (86 loc) Β· 5.07 KB

File metadata and controls

104 lines (86 loc) Β· 5.07 KB

Next.js App

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.

1. πŸ“‚ Monorepo-Style Directory Structure

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)

3. πŸ“ Step-by-Step Guide

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.