Create a new async function, getTokenAccounts and require a parameter of wallet and solanaConnection:
javascript
async function getTokenAccounts(wallet: string, solanaConnection: Connection) {
}
Establish filters
First, let's define our filters. The filters we will be using are:
- dataSize is a filter used to look for accounts of a specific size. For token accounts, this is a known quantity, 165.
- memcmp, or "memory comparison" filter, is used to narrow our search within an account. Specifically, we will use offset to determine where in our account's 165 bytes to search (this is another known value for this Program: the owner's public key starts at 32) and bytes to set what we will be searching for: in this case, the users' wallet address.
- Source information about SPL token account data can be found here.
Inside getTokenAccounts, define a variable called filters.
javascript
const filters:GetProgramAccountsFilter[] = [
{
dataSize: 165, //size of account (bytes)
},
{
memcmp: {
offset: 32, //location of our query in the account (bytes)
bytes: wallet, //our search criteria, a base58 encoded string
}
}
];
This should reduce our query to look for only token accounts owned by our wallet.
Get Program Accounts
Now call the getParsedProgramAccounts method passing in the SPL Token Program ID and our filters.
create your token account query
const accounts = await solanaConnection.getParsedProgramAccounts(
TOKEN_PROGRAM_ID, //SPL Token Program, new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
{filters: filters}
);
const accounts = await solanaConnection.getParsedProgramAccounts(
TOKEN_PROGRAM_ID, //SPL Token Program, new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
{filters: filters}
);
const accounts = await solanaConnection.getParsedProgramAccounts(
TOKEN_PROGRAM_ID, //SPL Token Program, new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
{filters: filters}
);
The previous call returns an array of all matching token accounts with the following structure:
create your token account query
{
pubkey: PublicKey, //Token Account Public Key
account: AccountInfo //Object including information about our token account
}[]
{
pubkey: PublicKey, //Token Account Public Key
account: AccountInfo //Object including information about our token account
}[]
{
pubkey: PublicKey, //Token Account Public Key
account: AccountInfo //Object including information about our token account
}[]
You can log accounts.length to see how many token accounts the user has:
create your token account query
console.log(`Found ${accounts.length} token account(s) for wallet ${wallet}.`);
console.log(`Found ${accounts.length} token account(s) for wallet ${wallet}.`);
console.log(`Found ${accounts.length} token account(s) for wallet ${wallet}.`);
Parse the Results
Build a simple forEach loop to iterate through our results and log our results. Inside your getTokenAccounts function add:
create your token account query
accounts.forEach((account, i) => {
//Parse the account data
const parsedAccountInfo:any = account.account.data;
const mintAddress:string = parsedAccountInfo["parsed"]["info"]["mint"];
const tokenBalance: number = parsedAccountInfo["parsed"]["info"]["tokenAmount"]["uiAmount"];
//Log results
console.log(`Token Account No. ${i + 1}: ${account.pubkey.toString()}`);
console.log(`--Token Mint: ${mintAddress}`);
console.log(`--Token Balance: ${tokenBalance}`);
});
accounts.forEach((account, i) => {
//Parse the account data
const parsedAccountInfo:any = account.account.data;
const mintAddress:string = parsedAccountInfo["parsed"]["info"]["mint"];
const tokenBalance: number = parsedAccountInfo["parsed"]["info"]["tokenAmount"]["uiAmount"];
//Log results
console.log(`Token Account No. ${i + 1}: ${account.pubkey.toString()}`);
console.log(`--Token Mint: ${mintAddress}`);
console.log(`--Token Balance: ${tokenBalance}`);
});
accounts.forEach((account, i) => {
//Parse the account data
const parsedAccountInfo:any = account.account.data;
const mintAddress:string = parsedAccountInfo["parsed"]["info"]["mint"];
const tokenBalance: number = parsedAccountInfo["parsed"]["info"]["tokenAmount"]["uiAmount"];
//Log results
console.log(`Token Account No. ${i + 1}: ${account.pubkey.toString()}`);
console.log(`--Token Mint: ${mintAddress}`);
console.log(`--Token Balance: ${tokenBalance}`);
});