Hook is available from version 2.2.4 and Solana support from 2.2.9
Summary
Used to fetch the token balances of an account on a specified network. The default behavior is to return the token balances of the primary account on the current network, but optionally the account, network, includeFiat and includeNativeBalance can be specified.
Currently only supported on Ethereum, Optimism, Polygon, Arbitrum, Base and
Solana networks for logged in users.
Usage
import { useTokenBalances } from "@dynamic-labs/sdk-react-core";
const { tokenBalances, isLoading, isError, error } = useTokenBalances();
[
{
"networkId": 1,
"address": "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0",
"name": "Polygon",
"symbol": "MATIC",
"decimals": 18,
"logoURI": "https://assets.coingecko.com/coins/images/4713/thumb/polygon.png?1698233745",
"balance": 0.7851804304793578,
"rawBalance": 785180430479357800,
"price": 0.703229,
"marketValue": 0.5521616489455683
},
{
"networkId": 1,
"address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"name": "USDC",
"symbol": "USDC",
"decimals": 6,
"logoURI": "https://assets.coingecko.com/coins/images/6319/thumb/usdc.png?1696506694",
"balance": 50,
"rawBalance": 50000000,
"price": 1,
"marketValue": 50
}
]
With arguments
Parameter | Type | Description |
---|
networkId | Number | The network ID |
chainName | ChainEnum | The chain used |
tokenAddresses | String[] | The token addresses |
includeFiat | Boolean | Should include Fiat prices |
includeNativeBalance | Boolean | Should include native balance |
Optionally, you can pass an object with the account address and network id specified. Additionally, you can pass an array of token addresses to filter the results.
import { useTokenBalances } from "@dynamic-labs/sdk-react-core";
const { tokenBalances, isLoading, isError, error } = useTokenBalances({
networkId: 1,
accountAddress: "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0",
includeFiat: true,
includeNativeBalance: true,
});
return (
<ul>
{tokenBalances?.map((tokenBalance) => (
<li key={tokenBalance.address}>
{tokenBalance.name} {tokenBalance.balance} {tokenBalance.symbol} ($
{tokenBalance.price}) | ${tokenBalance.marketValue}
</li>
))}
</ul>
);
import { useTokenBalances } from "@dynamic-labs/sdk-react-core";
const { tokenBalances, isLoading, isError, error } = useTokenBalances({
networkId: 1,
accountAddress: "0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0",
tokenAddresses: ["0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0"],
});
Solana support with Fiat prices (with SDK version 2.2.9)
import { useTokenBalances } from "@dynamic-labs/sdk-react-core";
import { ChainEnum } from "@dynamic-labs/sdk-api";
const { tokenBalances, isLoading, isError, error } = useTokenBalances({
chainName: ChainEnum.Sol,
accountAddress: address,
includeFiat: true,
includeNativeBalance: true,
});