helius_sdk/api/
rpc_client.rs

1
2use std::str::FromStr;
3use solana_client::rpc_client::RpcClient;
4use solana_client::rpc_config::RpcProgramAccountsConfig;
5use solana_client::rpc_filter::{MemcmpEncodedBytes, RpcFilterType, Memcmp};
6use solana_sdk::account::Account;
7use solana_sdk::pubkey::Pubkey;
8
9pub struct HeliusRpc {
10    connection: RpcClient,
11}
12
13impl HeliusRpc {
14    pub fn new(connection: RpcClient) -> HeliusRpc {
15        return HeliusRpc { connection };
16    }
17
18    pub fn connection(&self) -> &RpcClient {
19        return &self.connection;
20    }
21
22    pub fn get_tps(&self) -> Result<f32, String> {
23        let res = self.connection.get_recent_performance_samples(Some(1));
24        return match res {
25            Ok(samples) => {
26                Ok(samples[0].num_transactions as f32 / samples[0].sample_period_secs as f32)
27            }
28            _ => Err("Failed to get performance samples".to_string()),
29        };
30    }
31
32    pub fn airdrop(
33        &self,
34        pubkey: &Pubkey,
35        lamports: u64,
36    ) -> solana_client::client_error::Result<()> {
37        let signature = self.connection.request_airdrop(pubkey, lamports)?;
38        return self.connection.poll_for_signature(&signature);
39    }
40
41    pub fn get_stake_accounts(&self, address: &String) -> solana_client::client_error::Result<Vec<(Pubkey, Account)>> {
42        return self.connection.get_program_accounts_with_config(
43            &Pubkey::from_str("Stake11111111111111111111111111111111111111").unwrap(),
44            RpcProgramAccountsConfig{
45                filters: Some(vec![
46                    RpcFilterType::DataSize(200),
47                    RpcFilterType::Memcmp(Memcmp::new(
48                        44,
49                        MemcmpEncodedBytes::Bytes(Vec::from(address.as_bytes()))
50                    ))
51                ]),
52                account_config: Default::default(),
53                with_context: None,
54            }
55        );
56    }
57
58    pub fn get_token_holders(&self, mint_address: &String) -> solana_client::client_error::Result<Vec<(Pubkey, Account)>> {
59        return self.connection.get_program_accounts_with_config(
60            &Pubkey::from_str("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").unwrap(),
61            RpcProgramAccountsConfig {
62                filters: Some(vec![
63                    RpcFilterType::DataSize(165),
64                    RpcFilterType::Memcmp(Memcmp::new(
65                        0,
66                        MemcmpEncodedBytes::Bytes(Vec::from(mint_address.as_bytes()))
67                    ))
68                ]),
69                account_config: Default::default(),
70                with_context: None,
71            }
72        );
73    }
74}