getProgramAccounts
Explore getProgramAccounts: Use Cases, Code Examples, Parameters, Response Format, and Expert Tips
The getProgramAccounts RPC method lets you fetch all on‐chain accounts owned by a given program—essential for any application that needs to discover or monitor program‑specific state. Since a program can own thousands of accounts, this method offers powerful filtering to narrow your results and optimize performance.
🔍 Common Use Cases
Token Holders Discovery Find every SPL token account for a specific mint to list all token holders.
User‑Specific Data Retrieval Fetch all accounts created by a program for a user (e.g., a DeFi position, game state).
Custom Account Indexing Locate all instances of a bespoke account type defined by your program.
Program State Monitoring Track every account under a program to observe its overall activity and health.
Explorer & Analytics Tooling Aggregate and analyze program data for dashboards or block explorers.
🛠 Request Parameters
{
"programId": "<base58ProgramPubkey>", // Required
"options": { // Optional
"commitment": "processed|confirmed|finalized",
"encoding": "base64|base58|base64+zstd|jsonParsed",
"filters": [
{ "dataSize": <u64> },
{ "memcmp": { "offset": <usize>, "bytes": "<base58Bytes>" } }
// up to 4 total filters
],
"dataSlice": { "offset": <usize>, "length": <usize> },
"withContext": true|false,
"minContextSlot": <u64>
}
}programId: Base‑58 public key of the on‑chain program (e.g.,
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").commitment: Desired confirmation level.
encoding:
base64(default)base58base64+zstdjsonParsed(highly recommended for supported programs)
filters (max 4):
dataSize: Match accounts by byte length.memcmp: Compare a slice of account data atoffsetwithbytes.
dataSlice: Return only a byte range—ideal for large accounts.
withContext: Wrap results in an object with
context(slot) andvalue(accounts) whentrue.minContextSlot: Ensure data is at least as recent as this slot.
📦 Response Structure
Returns either a raw array or, if withContext: true, an RPC response object:
pubkey: Account’s base‑58 public key.
account:
lamports: Balance in lamports.owner: Program that owns this account.data: Encoded or parsed account data.executable:trueif this account is a program.rentEpoch: Next epoch when rent is due.space: Byte length of the data.
💡 Examples
All USDC Token Accounts Filter by
dataSize: 165andmemcmpat offset0matching the USDC mint key.Accounts Owned by a Wallet Use
dataSize: 165andmemcmpat offset32to match the wallet’s public key.
Code Examples
Example Response
🧠 Developer Tips
Always Filter: Without filters, queries can be extremely heavy and may time out.
No Pagination: Large result sets aren’t paginated—design filters to keep results manageable.
Rate Limits: Heavy usage can hit provider limits (e.g., CoinVera).
Know Your Layout: Effective
memcmpfilters require precise knowledge of account byte structure.Prefer
jsonParsed: For SPL Token, Stake, and other common programs,jsonParsedsaves manual decoding.
By mastering getProgramAccounts and its filters, you’ll efficiently query and manipulate program‑owned accounts at scale on Solana.
Last updated