Install Template
To get started, you can either clone the boilerplate repository for a quick setup or recreate it from scratch to better understand its structure.
π Template Repository
You can find the template here: GitHub - create-builder-codes-dapp
Option 1: Clone and Set Up the Repository
1οΈβ£ Clone the Repository
Run the following command to clone the template:
git clone https://github.com/Solynor/create-builder-codes-dapp.git
cd create-builder-codes-dappThen install dependencies:
pnpm install # or npm install2οΈβ£ Change the Git Remote to Your Own Repository
To push your changes to a personal GitHub repository, update the remote URL:
git remote set-url origin git@github.com:your_name/repo_name.git3οΈβ£ Configure the Environment Variables
Update the .env file with your custom settings - RPC List:
4οΈβ£ Run the Application
Once everything is set up, start the development server:
Your application should now be up and running!
Option 2: Build from Scratch
While cloning the boilerplate is faster, this approach helps you better understand the appβs architecture and dependencies.
1. Install Next.js
Weβll start by setting up a new Next.js 15 project.
π Run the following command:
π Reference: Next.js Installation Guide
2. Project Structure
Your app will be structured as follows:
π app/
app/Houses Next.js routes and pages.
Main entry point for the application.
π components/
components/Stores reusable components for your pages.
π hooks/
hooks/Contains custom React hooks for logic reuse (e.g.,
useAuth(),useFetchData()).
π lib/
lib/config/β Global settings (e.g., API URLs, environment variables).helpers/β Utility functions (e.g.,formatDate(),parseCookies()).services/β API call logic (e.g.,authService.ts,userService.ts).types/β TypeScript interfaces for better type safety.
3. Install & Configure Wagmi (Wallet Connection)
Wagmi is used for wallet connections and blockchain interactions.
π Install dependencies:
π Reference: Wagmi Documentation
π§ Modify the following files:
π lib/config/
index.tsβ Fetches configuration from.envwagmi.tsβ Creates the Wagmi config using AppKit Documentation.chain.tsβ Defines the Hyperliquid EVM testnetaxios.tsβ Installs and configures Axios for API calls
π components/
ContextProvider.tsxβ Wraps the app inWagmiProviderand passes the config.UserProvider.tsxβ Manages user state based on wallet connection (wagmi+localStorage).π Note: Consider improving Builder Fee storage (DB or blockchain instead of local cache).
π lib/store.ts (State Management)
Zustand is used to manage global state efficiently.
π Install Zustand:
Responsibilities of store.ts:
Define user-related state types (
User,Agent,UserStoreState).Create Zustand store for updating, resetting, and accessing user data globally.
π Reference: Zustand Documentation
4. UI & Styling Setup
π Install required packages:
π Reference: shadcn/UI Installation Guide
Configure UI files
tailwind.config.tsβ Customize Tailwind settings (Example)globals.cssβ Define base styles (Example)lib/utils.tsβ Utility functions for stylingcomponents/ui/β Copy & paste shadcn/UI components (e.g.,toast.tsx,button.tsx, etc.).
π Important: shadcn/UI is not a component libraryβit's a collection of reusable UI components that you copy manually.
π Reference: Radix UI (Shadcn's Base Library)
5. Blockchain Connection (Hyperliquid Integration)
Key Components:
π app/page.tsx
Displays either the trading widget (if connected) or the wallet connection button.
π components/
ConnectWallet.tsxβ Wraps aConnect Walletbutton using AppKit.Trade.tsxβ Trading widget for buying/selling assets.
π components/ApproveBuilderFee.tsx
Ensures users approve the Builder Fee before trading.
π lib/services/
approve-builder.tsβ Handles fee approval via Wagmi.πΉ Setup Instructions: Refer to the documentation Dapp Setup Guide for detailed steps on integrating your Dapp with the blockchain.
π lib/services/
builder-info.tsβ Calls Hyperliquid API/infoendpoint (API Docs).
6. Agent-Based Trading
Hyperliquid allows trading via an "agent" account.
Key Files:
components/ApproveAgent.tsxCreates a new account (agent) for secure order placement without signing each time.
The agent cannot move user funds, making it a safer approach.
lib/services/approve-agent.tsCalls Hyperliquid Exchange API for agent approval.
π Security Note: The agentβs private key is generated locallyβensure itβs never exposed or stored insecurely.
7. Order Execution
Handles placing, signing, and submitting trades.
Key Services:
market-order.tsβ/exchange(trade execution)mid-price.tsβ/info(retrieves real-time mid-prices)balances.tsβ/info(fetches user balances)token-details.tsβ/info(retrieves token metadata)signing.tsβ Uses MessagePack for encoding.
8. Final Steps: ESLint & Configuration
π Update ESLint rules (eslint.config.mjs):
π Modify Next.js config (next.config.ts)
Check the example file.
Conclusion
By following this guide, youβve built the Builder Codes Dapp from scratch, gaining a deep understanding of:
β Next.js & TypeScript setup β Wallet connection & Wagmi configuration β API integration with Hyperliquid β Trading execution with agent-based security β UI & state management with shadcn/UI & Zustand
Last updated