Learn how to create a app that detects if a smart wallet has ETH and prompts users to add funds if needed.
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.
src/wagmi.ts
:
utils.ts
file in the src
directory that creates a client connected to Base and fetches the user’s ETH balance:
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:
+ Add funds to transact
button they will be given three options for topping up their smart wallet: