Skip to content

solana-client-tools: add getProgramAccounts fetch helper for zero-copy accounts #3877

Description

@karl-dz

Target repo

Summary

The solana-client-tools crate has a family of helpers for fetching and deserializing zero-copy accounts (T: Pod + PrecomputedDiscriminator):

  • try_fetch_zero_copy_data (single account by pubkey)
  • try_fetch_zero_copy_data_with_commitment
  • try_fetch_multiple_zero_copy_data (multiple accounts by pubkey)

There is no equivalent for fetching every account of a given type from a program (a getProgramAccounts scan filtered by discriminator). Callers that need all accounts of a type currently hand-roll the same loop each time: a getProgramAccounts request with a discriminator memcmp at offset 0, a skip of zero-lamport (closed) accounts, and a checked_from_bytes_with_discriminator::<T> per account. Only the type and a label differ between copies.

Proposed helper

Add a sibling to the existing family:

pub async fn try_fetch_program_zero_copy_accounts<T: Pod + PrecomputedDiscriminator>(
    &self,
    program_id: &Pubkey,
    commitment: CommitmentConfig,
) -> Result<Vec<(Pubkey, ZeroCopyAccountOwnedData<T>)>>
  • Builds the RpcProgramAccountsConfig with a single Memcmp filter at offset 0 carrying T's discriminator, base64 encoding, and the supplied commitment.
  • Skips accounts with lamports == 0.
  • Deserializes each remaining account via ZeroCopyAccountOwnedData<T>, the same checked_from_bytes_with_discriminator path the other helpers use.
  • Returns each account paired with its pubkey.

Take commitment as a parameter to match try_fetch_zero_copy_data_with_commitment, and optionally add a default-commitment convenience wrapper mirroring try_fetch_zero_copy_data.

Motivation

Several callers reimplement this scan-and-deserialize loop. A shared helper removes the duplication and keeps the discriminator-filter and skip-empty details in one place, consistent with the rest of the zero-copy fetch family.

Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions