Learn how to create a app that detects if a smart wallet has ETH and prompts users to add funds if needed.
In this tutorial, you’ll learn how to build an onchain app that checks a user’s wallet balance and either allows them to mint an NFT or prompts them to add funds. We’ll use the OnchainKit App Template as a starting point.
By the end of this tutorial you should be able to:
You should be familiar with React and TypeScript. If you’re new to these technologies, consider reviewing their official documentation first.
This tutorial uses Coinbase’s Onchain Kit. Familiarity with its basic concepts will be helpful.
You’ll need to set up an account on with Coinbase Developer Platform (CDP) Account. The CDP provides various tools and services for blockchain development, including access to API endpoints and other resources that will be instrumental in your project. Once you’ve created your account, you’ll be ready to move forward with integrating these services into your application.
CDP Configurations:
If you see a “something went wrong” error message when navigating to pay.coinbase.com, make sure you have “enforce secure initialization” disabled on the Onramp config page in Coinbase Developer Platform Dashboard.
To get started, clone the Onchain Kit App Template by running
in your terminal, then navigate into the project directory with:
Next, install the necessary dependencies by executing bun install
followed by bun install viem
.
After setting up the project, you’ll need to configure your environment variables. Create a .env
file in the root directory of your project and add the following line: NEXT_PUBLIC_WC_PROJECT_ID=your_project_id_here
. Remember to replace ‘your_project_id_here’ with your actual project ID. Additionally, don’t forget to configure your apiKey in the src/app/components/OnchainProviders.tsx
file.
To make the app work only with smart wallets, modify src/wagmi.ts
:
Now well implement a check on the user’s wallet to see if they have enough funds. Before we implement this check, let’s create a helper function that grabs the user’s Ethereum balance using viem. To do so, create a utils.ts
file in the src
directory that creates a client connected to Base and fetches the user’s ETH balance:
Next, import the getBalance()
function into your main component file (e.g., src/app/page.tsx
). You will want to add a few react hooks to fetch the balance and store it as a state variable. Add the following lines of code to your page.tsx
file:
Now that we know the user’s balance, we can then have them mint an NFT or prompt them to fund their wallet if they do not have enough ETH.
The end state is to show their balance along with the appropriate call to actions like so:
Update your component’s return statement with the following code:
Sweet! Now our conditional rendering is in full force. If a user clicks on the + Add funds to transact
button they will be given three options for topping up their smart wallet:
Congratulations! You’ve built a app that checks a user’s smart wallet balance and provides appropriate options based on their funds. This app can serve as a foundation for more complex onchain applications that require users to have funded smart wallets.