Wallet balances
In this brief step-by-step guide we will use the Checkout SDK to fetch the tokens (ERC20) balances in a wallet. This list will contain tokens such as IMX, USDC, etc.
Supported tokens
The list of supported tokens per network and feature can be programmatically fetched using the getTokenAllowList()
function.
The getTokenAllowList()
function allows to filter the list of tokens based on type
and chainId
which can be found in the GetTokenAllowListParams
interface.
// Get the list of supported tokens
const tokenType = await checkout.TokenFilterTypes.ALL;
const chainId = connectRes.provider._network.chainId ?? checkout.ChainId.IMTBL_ZKEVM_TESTNET;
const tokenAllowList = await checkoutSDK.getTokenAllowList({ type: tokenType, chainId });
The GetTokenAllowListResult
result provides list of allowed networks with additional information (e.g. nativeCurrency
) that can be used while building your application.
Get all balances
This can be easily done using the getAllBalances()
function. This function returns the native token balance as well as balances for all the ERC20 tokens held by the wallet associated to the provider.
Networks | Native Token | Layer |
---|---|---|
IMTBL_ZKEVM_MAINNET | IMX | Layer 2 |
IMTBL_ZKEVM_TESTNET | IMX | Layer 2 |
IMTBL_ZKEVM_DEVNET | IMX | Layer 2 |
ETHEREUM | ETH | Layer 1 |
SEPOLIA | ETH | Layer 1 |
// Get all token balances of the wallet
const chainId = connectedProvider._network.chainId ?? checkout.ChainId.IMTBL_ZKEVM_TESTNET;
const allBalancesResponse = await checkoutSDK.getAllBalances({ provider: connectedProvider, walletAddress, chainId });
The GetAllBalancesResult
result provides the list of tokens held by the wallet on target network and their balances.
The formattedBalance
property is the balance value formatted using the token decimal
property; this value should be used when showing the value to the application user.
ERC20 Token details
The Checkout SDK exposes the getTokenInfo()
function that can be used to fetch the (ERC20) token details (e.g. symbol).
// Get the details of a particular token
const tokenAddress = "0xD61ffaece032CA6E0C469820707d677Feb4BEDD5";
const tokenInfo = await checkoutSDK.getTokenInfo({ provider: connectedProvider, tokenAddress });
The TokenInfo
result provides an object containing the token details.
Get balance for a token
The SDK also offers the ability to get the balance of a token. This should be preferred above fetching all the balances if network latency is important and the need for the whole list of balances is optional.
In the example below, the getBalance()
function is used to fetch the native token balance of the wallet associated to the provider.
// Get the balance of the native token
const balanceResponse = await checkoutSDK.getBalance({ provider: connectedProvider, walletAddress });
However, by providing the ERC token address as an input parameter, the getBalance()
function will get the ERC20 token balance of the wallet associated to the provider.
// Get the balance of a particular token
const tokenAddress = '0xD61ffaece032CA6E0C469820707d677Feb4BEDD5'
const balanceResponse = await checkoutSDK.getBalance({ provider: connectedProvider, walletAddress, tokenAddress });
In both cases, the function returns the GetBalanceResult interface.