solana_foundation_delegation_program_cli/
lib.rs

1use {
2    solana_client::{
3        rpc_client::RpcClient,
4        rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
5        rpc_filter::*,
6    },
7    solana_foundation_delegation_program_registry::state::{Participant, ParticipantState},
8    solana_sdk::{program_pack::Pack, pubkey::Pubkey},
9    std::collections::HashMap,
10};
11
12pub fn get_participants_with_state(
13    rpc_client: &RpcClient,
14    state: Option<ParticipantState>,
15) -> Result<HashMap<Pubkey, Participant>, Box<dyn std::error::Error>> {
16    let accounts = rpc_client.get_program_accounts_with_config(
17        &solana_foundation_delegation_program_registry::id(),
18        RpcProgramAccountsConfig {
19            account_config: RpcAccountInfoConfig {
20                encoding: Some(solana_account_decoder::UiAccountEncoding::Base64Zstd),
21                commitment: Some(rpc_client.commitment()), // TODO: Remove this line after updating to solana v1.6.10
22                ..RpcAccountInfoConfig::default()
23            },
24            filters: Some(vec![RpcFilterType::DataSize(
25                Participant::get_packed_len() as u64
26            )]),
27            ..RpcProgramAccountsConfig::default()
28        },
29    )?;
30
31    Ok(accounts
32        .into_iter()
33        .filter_map(|(address, account)| {
34            Participant::unpack_from_slice(&account.data)
35                .ok()
36                .map(|p| (address, p))
37        })
38        .filter(|(_, p)| {
39            if let Some(ref state) = state {
40                return p.state == *state;
41            }
42            true
43        })
44        .collect())
45}
46
47pub fn get_participants(
48    rpc_client: &RpcClient,
49) -> Result<HashMap<Pubkey, Participant>, Box<dyn std::error::Error>> {
50    get_participants_with_state(rpc_client, None)
51}