getTokenBalanceBySymbol
This function fetches SPL token balance by token symbol and network.
Usage
page.tsx
import { useConnection } from "@solana/wallet-adapter-react";
import { getTokenBalanceBySymbol } from "@okito/sdk";
const {connection} = useConnection();
const target = "WALLET_ADDRESS"; // The account for which balance is requested
const tokenSymbol = "USDC"; // Token symbol (e.g., "USDC", "USDT")
const network = "mainnet-beta"; // Network type
const tokenBalance = await getTokenBalanceBySymbol(connection, target, tokenSymbol, network)Capabilities
- Symbol-to-mint resolution for common tokens
- Same balance shape as
getTokenBalanceByMint - Network-aware lookups (mainnet-beta, devnet)
Props
| Name | Type | Default |
|---|---|---|
connection | ConnectionThe Solana `Connection` object. | |
target | PublicKey | stringThe wallet address for which the token balance is being retrieved. | |
tokenSymbol | stringThe symbol of the SPL Token (e.g., 'USDC', 'RAY'). | |
network | 'mainnet-beta' | 'devnet'The network to use for resolving the token symbol to a mint address. | |
Response
| Name | Type | Default |
|---|---|---|
success | booleanIndicates if the balance was fetched successfully. If `false`, check the `error` property. | |
balance | BalanceAn object containing balance details if successful, otherwise `null`. | |
error | string | undefinedAn error message if the operation failed. | |
Balance
If the success flag is true, the balance property will contain an object with the following structure.
| Name | Type | Default |
|---|---|---|
amount | stringThe raw token balance as a string, without accounting for decimals. e.g., '1500000'. | |
decimals | numberThe number of decimals for this token mint. e.g., 6. | |
uiAmount | number | nullThe token balance as a floating-point number, with decimals applied. Can be inaccurate for very large numbers. | |
uiAmountString | string | undefinedA string representation of the token balance, with decimals applied. e.g., '1.5'. This is the safest and most precise value to use for display purposes. | |
Last updated on