1use solana_client::{nonblocking::rpc_client::RpcClient, rpc_config::RpcProgramAccountsConfig, rpc_filter::{Memcmp, RpcFilterType}};
2use steel::*;
3
4pub struct Client {
5 rpc: RpcClient,
6}
7
8impl Client {
9 pub fn new(rpc: RpcClient) -> Self {
10 Self { rpc }
11 }
12
13 pub async fn get_program_account<T>(&self, pubkey: Pubkey) -> Result<T, anyhow::Error>
14 where T: AccountDeserialize + Discriminator + Clone {
15 let account = self.rpc.get_account(&pubkey).await?;
16 let account = T::try_from_bytes(&account.data).unwrap().clone();
17 Ok(account)
18 }
19
20 pub async fn get_program_accounts<T>(&self, program_id: Pubkey, filters: Vec<RpcFilterType>) -> Result<Vec<(Pubkey, T)>, anyhow::Error>
21 where T: AccountDeserialize + Discriminator + Clone {
22 let mut all_filters = vec![
23 RpcFilterType::Memcmp(Memcmp::new_raw_bytes(
24 0,
25 T::discriminator().to_le_bytes().to_vec(),
26 )),
27 ];
28 all_filters.extend(filters);
29 let accounts = self
30 .rpc
31 .get_program_accounts_with_config(
32 &program_id,
33 RpcProgramAccountsConfig {
34 filters: Some(all_filters),
35 ..Default::default()
36 },
37 )
38 .await?
39 .into_iter()
40 .map(|(pubkey, account)| {
41 let account = T::try_from_bytes(&account.data).unwrap().clone();
42 (pubkey, account)
43 })
44 .collect();
45 Ok(accounts)
46 }
47}