Documentation Index
Fetch the complete documentation index at: https://docs.fuse.io/llms.txt
Use this file to discover all available pages before exploring further.
Introduction
In this tutorial, we’ll walk through fetching token balances for a Smart Contract Wallet using the Fusebox Web SDK in a Next.js application. We’ll utilize React hooks to manage the State and display the token balances in a table format.Prerequisites
Before starting, ensure you have the following:- Node.js installed on your machine.
- Code Editor: Use your preferred code editor; VS Code is recommended.
- An EOA wallet with a private key. You can use an existing one or create a new wallet.
- A basic understanding of React.js and Next.js.
- An API key from the Fuse Console. Get one here.
Step 1: Set Up a Next.js Project
If you haven’t already set up a Next.js project, you can create one using the following commands:
Step 2: Install Required Packages
Install the necessary packages by running the following command in your project directory:Step 3: Import Libraries
Open the index.tsx file, delete the default code from NextJS. Edit index.tsx file by adding the code below and save it to see the updated result in your browser. First, import the required libraries and put up a default `Hello, World!’ text:Step 4: Run the Development Server
Run npm run dev to start the development server. Visit http://localhost:3000 to view your application.Step 5: Initialize the App State
We are going to first get the Smart Contract Wallet, and it’s owned Tokens and their respective balances, excluding Fuse native token balance. We will handle these information in various state and set/call each as required. We will also implement an App Loading state while waiting for the Promise methods to be resolved. Import the useState method:Token type interface is:
Step 6: implement methods
We will implement the methods:init, getSender, getTokenList and getTokenBalance in one method call. Declare the method as smartWalletBalance. Update the smartWalletBalance method with the following code:
"YOUR_API_KEY" and "YOUR_PRIVATE_KEY" with your actual API key and private key.
Step 7: Update the UI
Update the UI to a Dashboard that displays the information from calling thesmartWalletBalance. Paste the following code in the return section.
Step 8: Run Your Next.js Application
Call thesmartWalletBalance in a useEffect Hook:
http://localhost:3000 in your browser to see the application in action. You should see a table displaying the token names, addresses, and amounts associated with the Smart Contract Wallet.

Step 9: Code breakdown
Let’s break down thesmartWallet function and explain the usage of the getTokenList and getTokenBalance methods:
smartWallet function:
- Initialization:
- We initialize the FuseSDK using the provided API key and credentials using
FuseSDK.init(). - The
withPaymaster: trueoption enables the use of a paymaster.
- We initialize the FuseSDK using the provided API key and credentials using
- Getting Smart Account Address:
- We retrieve the address of the Smart Account using
fuseSDK.wallet.getSender()and set it to thesmartAccountstate variable.
- We retrieve the address of the Smart Account using
- Getting Token List:
- We use
fuseSDK.explorerModule.getTokenList(smartAccount)to fetch the list of tokens held by the Smart Contract Wallet. - This method returns an array of objects, each containing information about a token, such as symbol, name, and address.
- We set this array of token objects to the
tokenNamesstate variable.
- We use
- Extracting Token Addresses:
- We extract the token addresses from the
tokenListarray using themap()function and store them in thetokenAddressesarray.
- We extract the token addresses from the
- Fetching Balances:
- We initialize an empty object,
fetchedBalances, to store the token balances. - We iterate over each
tokenAddressin thetokenAddressesarray. - For each
tokenAddress, we usefuseSDK.explorerModule.getTokenBalance(tokenAddress, smartAccount)to fetch the balance of that token held by the Smart Contract Wallet. - We store each balance in the
fetchedBalancesobject with thetokenAddressas the key.
- We initialize an empty object,
- Updating State:
- Finally, we update the
balancesstate variable with thefetchedBalancesobject containing token balances.
- Finally, we update the
smartWallet function works and utilizes the getTokenList and getTokenBalance methods to fetch token information and balances for a Smart Contract Wallet.
Checkout the complete code. 💻