How to get all assets in a FunWallet
Get all assets for a FunWallet.
Overview
Fetching the current token balances for a wallet can be a real hassle. You have to index the blockchain to get accurate results or make many smart contract calls. FunWallet removes all of those complexities and provides a getAssets() so that you can get back a list of all assets in your FunWallet. In this guide, I’ll show you how to use it and describe the outputs. This guide will assume some basic knowledge covered in the Quick Start but I’ll quickly walk through all the steps again.
Import required objects
import {FunWallet,Auth,configureEnvironment,generatePrivateKey,} from "@funkit/core";
Configuring the environment
To begin, we need to set up the environment for your FunWallet. We’ll be using the Goerli testnet and a gasless sponsor. Please visit our dashboard to receive your own production apiKey.
await configureEnvironment({chain: "goerli",gasSponsor: {sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad",},apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3",});
Creating FunWallet with private key
We’ll use a private key as the authentication method and I’ll provide us with one. This is the FunWallet we will use to getAssets() from.
const privateKey = generatePrivateKey();const auth = new Auth({ privateKey });const funWallet = new FunWallet({users: [{ userId: auth.getAddress() }],uniqueId: auth.getWalletUniqueId(),});
Getting all assets
Now that we have a FunWallet, we can use it as an example to figure out what assets we have in it. Let me first describe some of the parameters we can input.
chain - We can decide to input a chain to get assets from. If empty, it will default to environment options.
onlyVerifiedTokens - This defaults to false to get all tokens, but you can set it to true if you want to filter out spam tokens using Alchemy lists.
checkStatus - This defaults to false, but you can set it to true if you to check if the address has any pending Lido withdrawals
I’ll just use all defaults, so we can see everything.
const assets = await wallet.getAssets();console.log(assets);
Return format
The return format is broken up into two categories, tokens and NFTs. The returns are in hexadecimal format so you’ll need to convert it and notice that we have no NFTs in the wallet.
{tokens: {'0x07865c6e87b9f70255377e024ace6630c1eaa37f': {contractAddress: '0x07865c6e87b9f70255377e024ace6630c1eaa37f',tokenBalance: '0x000000000000000000000000000000000000000000000000000000457ffea2fe',symbol: 'USDC',decimals: 6,logo: null},'0x0a1f0598a561af6b84a726be007f581e812c3cdd': {contractAddress: '0x0a1f0598a561af6b84a726be007f581e812c3cdd',tokenBalance: '0x0000000000000000000000000000000000000000000000410d583fdf9613bd32',symbol: 'DAI',decimals: 18,logo: null},'0x11fe4b6ae13d2a6055c8d9cf65c55bac32b5d844': {contractAddress: '0x11fe4b6ae13d2a6055c8d9cf65c55bac32b5d844',tokenBalance: '0x0000000000000000000000000000000000000000000000003e8a03d8789f1f68',symbol: 'DAI',decimals: 18,logo: null},'0x4903c3ce6d80431b54fedacaa7c3dc9d4d4ef61b': {contractAddress: '0x4903c3ce6d80431b54fedacaa7c3dc9d4d4ef61b',tokenBalance: '0x0000000000000000000000000000000000000000000000000000000000000575',symbol: 'TEST',decimals: 18,logo: null},'0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6': {contractAddress: '0xb4fbf271143f4fbf7b91a5ded31805e42b2208d6',tokenBalance: '0x000000000000000000000000000000000000000000000000000eabc0200911fd',symbol: 'WETH',decimals: 18,logo: null},'0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE': { tokenBalance: '0x35484461d16011', price: 1648.57 }},nfts: []}
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE is the native token of the chain. Here it is ETH.
Summary
Putting together all the pieces we have getAssets() that you can use to get assets in a FunWallet.
getAssets.js
import { Auth, configureEnvironment, FunWallet, generatePrivateKey } from "@funkit/core";const getAssets = async () => {await configureEnvironment({chain: "goerli",gasSponsor: {sponsorAddress: "0xCB5D0b4569A39C217c243a436AC3feEe5dFeb9Ad"},apiKey: "ZrhepzWGxm74D0sqstuhT6dGrJxhoy8SZIToX6I3"})const privateKey = generatePrivateKey()const auth = new Auth({ privateKey })const wallet = new FunWallet({users: [{ userId: await auth.getAddress() }],uniqueId: await auth.getWalletUniqueId()})const assets = await wallet.getAssets()console.log(assets)}getAssets()