Introduction to Program Accounts
The getProgramAccounts method is a powerful RPC function that returns all accounts owned by a specific Solana program. This guide explains how to optimize your queries for better performance and precise results.
Key Parameters
When making a getProgramAccounts request, these parameters are essential:
- programId:
string
The base58-encoded public key of the program you're querying configOrCommitment (optional):
object
Configuration options including:commitment: State commitment levelencoding: Data encoding format (base58,base64, orjsonParsed)dataSlice: Limits returned account data by specifying:offset: Starting byte positionlength: Number of bytes to return
filters: Array of filter conditions:memcmp: Compares bytes at specific offsetdataSize: Matches accounts with exact data size
Response Structure
By default, responses include:
{
"pubkey": "base58-account-key",
"account": {
"lamports": number,
"owner": "base58-program-key",
"data": string|object,
"executable": boolean,
"rentEpoch": number
}
}Practical Applications
This method enables powerful queries like:
๐ Discovering all token accounts for a specific wallet
๐ [Finding all holders of a particular token]
๐ [Locating all users of a specific program]
Optimizing Your Queries
Filter Techniques
- dataSize Filter
Perfect for finding accounts with known data lengths (e.g., SPL token accounts are always 165 bytes) - memcmp Filter
Allows byte-level comparisons at specific offsets to find exact matches
Code Examples
JavaScript Implementation
const accounts = await connection.getProgramAccounts(
TOKEN_PROGRAM_ID,
{
filters: [
{ dataSize: 165 },
{
memcmp: {
offset: 32,
bytes: "FriEL...VP8T"
}
}
]
}
);Rust Implementation
let filters = Some(vec![
RpcFilterType::Memcmp(Memcmp::new(
32,
MemcmpEncodedBytes::Base58("FriEL...VP8T".to_string())
)),
RpcFilterType::DataSize(165)
]);cURL Example
curl http://api.mainnet-beta.solana.com -X POST -H "Content-Type: application/json" -d '
{
"method": "getProgramAccounts",
"params": [
"Tokenkeg...VQ5DA",
{
"filters": [
{ "dataSize": 165 },
{ "memcmp": { "offset": 32, "bytes": "FriEL...VP8T" } }
]
}
]
}'Best Practices
- Always include filters to narrow results
- Use
dataSlicewhen you only need partial account data - Combine
memcmpanddataSizefor precise queries - Avoid making overly broad requests that might timeout
FAQ Section
Q: Why does my query return incomplete results?
A: The method currently doesn't support pagination. For large datasets, use filters to narrow your scope.
Q: How can I improve query performance?
A: Always include relevant filters and consider using dataSlice to reduce returned data size.
Q: What's the maximum size for memcmp bytes?
A: The bytes parameter in memcmp is limited to 129 bytes.
Q: Can I compare numerical values with memcmp?
A: Currently, memcmp only supports exact byte matches, not numerical comparisons.
๐ For more advanced Solana development techniques
Additional Resources
- Solana RPC API Documentation
- Web3.js Program Account Reference
- SPL Token Program Specifications