gill

fetchTokenAccounts

function fetchTokenAccounts<TMintAddress, TOwner>(
   rpc, 
   mint, 
   owner, 
   config): Promise<{
  accounts: Account<Token>[];
  mint: Account<Mint>;
  totalBalance: bigint;
}>;

Fetch all the the token accounts for a given mint and owner Address. Automatically fetching the Mint account itself and calculating the total balance of all the owner's token accounts.

Example

const { mint, accounts, totalBalance } = await fetchTokenAccounts(
   rpc,
   "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" as Address, // (mint for USDC on mainnet)
   "nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5" as Address, // owner address
);

Type Parameters

Type ParameterDefault type
TMintAddress extends stringstring
TOwner extends stringstring

Parameters

ParameterTypeDescription
rpcRpc<GetTokenAccountsByOwnerApi & GetAccountInfoApi>-
mint| Address<TMintAddress> | Account<Mint>-
ownerAddress<TOwner>-
config{ abortSignal?: AbortSignal; commitment?: Commitment; dataSlice?: Readonly<{ length: number; offset: number; }>; minContextSlot?: bigint; }-
config.abortSignal?AbortSignal-
config.commitment?CommitmentFetch the details of the accounts as of the highest slot that has reached this level of commitment.
config.dataSlice?Readonly<{ length: number; offset: number; }>Define which slice of the accounts' data you want the RPC to return. Use this to save network bandwidth and encoding time when you do not need the entire buffer. Data slicing is only available for "base58", "base64", and "base64+zstd" encodings.
config.minContextSlot?bigintPrevents accessing stale data by enforcing that the RPC node has processed transactions up to this slot

Returns

Promise<{ accounts: Account<Token>[]; mint: Account<Mint>; totalBalance: bigint; }>

On this page