8 min read
Overview
Hello readers! To kick off Solana Summer and the current whitelist meta, we thought it would be helpful to dig into all of the token accounts you and your users have using the getParsedProgramAccounts method. This tool is convenient for querying different programs on Solana, including the Solana SPL Token Account library.
Prefer a video walkthrough? Follow along with Noah and learn how to get all tokens held by a Solana wallet in 13 minutes.
This guide will walk you through creating a simple script that will query Solana's mainnet and return all of the Token Accounts owned by that wallet and their account balances.
Here is a video if you prefer to watch:
What You Will Need
-
Nodejs (version 16.15 or higher)
-
Typescript experience and ts-node installed
-
Basic Understanding of some Solana Principles:
Programs: smart contracts on Solana. In this example, we will use the SPL Token Program, which defines typical use cases for fungible and non-fungible tokens on Solana.
Program Filters: many on-chain program queries pull extensive data sets, so it is essential to narrow your search. We will use the GetProgramAccountsFilter type to help us narrow down our search.
Set Up Your Environment
Create a new project directory and file, index.ts, in your terminal with:
mkdir sol-get-accounts
cd sol-get-accounts
echo > index.ts
Initialize your project:
yarn init --yes
or
npm init --yes
Install Solana Web3 dependencies:
yarn add @solana/web3.js@1 @solana/spl-token
or
npm install @solana/web3.js@1 @solana/spl-token
Open index.ts in a code editor and add the following dependencies:
import { Connection, GetProgramAccountsFilter } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
You can now access Logs for your RPC endpoints, helping you troubleshoot issues more effectively. If you encounter an issue with your RPC calls, simply check the logs in your QuickNode dashboard to identify and resolve problems quickly. Learn more about log history limits on our pricing page.
**Establish a Connection to Solana Mainnet
**
To build on Solana, you'll need an API endpoint to connect with the network. You're welcome to use public nodes or deploy and manage your own infrastructure, however, if you'd like 8x faster response times you can leave the heavy lifting to us.
See why over 50% of projects on Solana choose QuickNode and sign up for free here.
Make sure to launch your node under the Solana Mainnet and copy the HTTP link:
const rpcEndpoint = 'https://example.solana-mainnet.quiknode.pro/000000/';
const solanaConnection = new Connection(rpcEndpoint);
Define the wallet that you want to query as a string:
const walletToQuery = 'YOUR_PUBLIC_KEY'; //example: vines1vzrYbzLMRdu58ou5XTby4qAqVRLmqo36NKPTg
Great! We are ready to build our token query.
Create your Token Account Query
Create a new async function, getTokenAccounts and require a parameter of wallet and solanaConnection:
async function getTokenAccounts(wallet: string, solanaConnection: Connection) {
}