Hey there, Buidlers! If you've been thinking about diving into the world of on-chain dapps, you've come to the right place.
In this guide, we'll walk you through building an on-chain dapp on the Base chain, starting from scratch.
Whether you're a seasoned developer or just getting started, we'll break it down in an easy-to-follow way. If this is your first time deploying a smart contract on Base, we suggest starting with this guide.
Let's get started!
By the end of this guide, you'll be able to:
First things first, let's set up a basic Next.js app. We'll use Next.js 13, a popular React framework, as our starting point.
Run the following command to create a new Next.js app:
npx create-next-app@latest --use-yarn
During the setup, you'll be prompted to make a few choices. Here's what we recommend:
This script will set up a basic Next.js app with TypeScript and Tailwind CSS (if you choose to use it). Once the setup is complete, run the app to make sure everything's working:
yarn dev
Now that we have our basic app set up, it's time to add some onchain magic. For this, we'll use RainbowKit, Wagmi, and Viem. These libraries make it super easy to interact with the blockchain from your React app.
Install the dependencies with:
npm install @rainbow-me/rainbowkit wagmi viem@2.x @tanstack/react-query
With our dependencies installed, we need to set up our blockchain provider context. In Next.js 14, the root of your app is found in app/layout.tsx
. However, we need to separate the client-side logic into a different file due to Next.js's server-client separation.
Create a new file in the app
folder called providers.tsx
and add the following code:
Next, we need to use our newly created Providers
component in layout.tsx
to wrap the entire application. This will ensure that the blockchain provider context is available throughout your app.
Update layout.tsx
to look like this:
You can customize the ConnectButton as needed. For instance, if you don't want to display the user's balance, you can disable it like this:
With everything set up, it's time to add a Connect Button to your app. This button will allow users to connect their wallets to your app.
Open page.tsx
and import the ConnectButton
component:
You can customize the ConnectButton as needed. For instance, if you don't want to display the user's balance, you can disable it like this:
Now that you have the basic understanding of how you can build an onchain dapp on the Base chain, let's walk you through creating an NFT collection and setting up an NFT gallery app to showcase your NFTs.
Before we dive in, there are a couple of things you'll need to set up:
Wallet Funding: To deploy smart contracts, you'll need to cover gas fees. For this tutorial, we'll use the Base Sepolia test network. You can get Base Sepolia ETH from one of the faucets listed on the Base Network Faucets page.
Let's start by creating an NFT collection using Thirdweb. This collection will serve as the foundation for our NFT gallery app.
Click "Deploy Now"
Source
Minting NFTs:
With your NFT collection set up, it's time to build an NFT gallery app to display your NFTs. We'll use the Thirdweb CLI to generate a new app project using the NFT Gallery template.
Create the App: Run the following command to generate a new app project:
npx thirdweb create --template nft-gallery
Update Configuration: The template is configured for the Ethereum Mainnet by default. We'll update it to work with the Base Sepolia Testnet.
src/consts/parameters.ts
file and update:contractAddress
: Your NFT collection's contract address (found on the Thirdweb dashboard).chain
: base-sepolia
.blockExplorer
: https://sepolia.basescan.org
.src/main.tsx
and replace the content with the following code:Running the Application: To view your NFT collection in the app, start the application by running:
yarn dev
Now that you're done building an onchain dapp on Base, it's about time we deploy it.
Fleek aims to address this by providing a fast and trustless Content Delivery Network (CDN) through the Fleek Network.
Let’s guide you through deploying a site built with the "An Onchain App in 100 Components
" template using Fleek.
Onchain Apps: Basic knowledge of deploying apps and connecting to smart contracts is assumed. For a primer, check out the "Building an Onchain App" tutorial on Base Learn.
Install Dependencies:
curl -fsSL https://bun.sh/install | bash
Verify the app is running by navigating to localhost:3000 and then shut down the server.
Fleek requires static pages, so you'll need to adjust your build process to produce them:
next.config.js
:next.config.js
and modify it as follows:bun run build
and confirm that an out
directory is created.npm install && npm run build
Open a terminal and install the Fleek CLI:
npm install -g @fleek-platform/cli
Log in to Fleek:
fleek login
In your project directory, run:
fleek sites init
out
).npm install && npm run build
).fleek.config.json
).Deploying your site can be triggered automatically through CI/CD.
src/app/page.tsx
.And there you have it! You've just built the foundation of an onchain app on Base. This guide walked you through setting up a Next.js app, manually installing necessary libraries, and adding a wallet connection. You also learned how to get templates from Thirdweb and integrate them into your app, as well as how to deploy your app using Fleek. Now, you can customize and expand your app as needed.
When you're ready to launch on mainnet, make sure to audit your smart contracts to ensure security and reliability. Whether you're building a full-fledged dApp or just exploring the possibilities of Web3, remember that the community and resources available are immense. Don't hesitate to reach out, ask questions, and keep experimenting!
Happy building!
Get Pure Alpha Straight to Your Inbox. Miss this, and you’re missing out.
Insider Secrets - Delivered Right to You. Subscribe now.