helius_rust_client/client/
init.rs

1use reqwest::Client as RestClient;
2use solana_client::nonblocking::rpc_client::RpcClient;
3use solana_client::rpc_client::Mocks;
4use solana_sdk::{commitment_config::CommitmentConfig, genesis_config::ClusterType};
5use std::time::Duration;
6
7pub const API_URL_V0: &str = "https://api.helius.xyz/v0";
8pub const API_URL_V1: &str = "https://api.helius.xyz/v1";
9pub const MAINNET_RPC_URL: &str = "https://rpc.helius.xyz/?api-key=";
10pub const DEVNET_RPC_URL: &str = "https://rpc-devnet.helius.xyz/?api-key=";
11
12pub struct HeliusClient {
13    pub rpc_client: RpcClient,
14    pub http_client: RestClient,
15    pub cluster: ClusterType,
16    pub(crate) api_key: String,
17}
18
19impl HeliusClient {
20    pub fn new(api_key: String, cluster_type: ClusterType) -> Self {
21        let url = format_url(api_key.clone(), cluster_type);
22        HeliusClient {
23            rpc_client: RpcClient::new(url),
24            cluster: cluster_type,
25            api_key,
26            http_client: reqwest::Client::new(),
27        }
28    }
29
30    pub fn new_with_commitment(
31        api_key: String,
32        cluster_type: ClusterType,
33        commitment_config: CommitmentConfig,
34    ) -> Self {
35        let url = format_url(api_key.clone(), cluster_type);
36        HeliusClient {
37            rpc_client: RpcClient::new_with_commitment(url, commitment_config),
38            cluster: cluster_type,
39            api_key,
40            http_client: reqwest::Client::new(),
41        }
42    }
43
44    pub fn new_with_timeout(api_key: String, cluster_type: ClusterType, timeout: Duration) -> Self {
45        let url = format_url(api_key.clone(), cluster_type);
46        HeliusClient {
47            rpc_client: RpcClient::new_with_timeout(url, timeout),
48            cluster: cluster_type,
49            api_key,
50            http_client: reqwest::Client::new(),
51        }
52    }
53
54    pub fn new_with_timeout_and_commitment(
55        api_key: String,
56        cluster_type: ClusterType,
57        timeout: Duration,
58        commitment_config: CommitmentConfig,
59    ) -> Self {
60        let url = format_url(api_key.clone(), cluster_type);
61        HeliusClient {
62            rpc_client: RpcClient::new_with_timeout_and_commitment(url, timeout, commitment_config),
63            cluster: cluster_type,
64            api_key,
65            http_client: reqwest::Client::new(),
66        }
67    }
68
69    pub fn new_with_timeouts_and_commitment(
70        api_key: String,
71        cluster_type: ClusterType,
72        timeout: Duration,
73        commitment_config: CommitmentConfig,
74        confirm_transaction_initial_timeout: Duration,
75    ) -> Self {
76        let url = format_url(api_key.clone(), cluster_type);
77        HeliusClient {
78            rpc_client: RpcClient::new_with_timeouts_and_commitment(
79                url,
80                timeout,
81                commitment_config,
82                confirm_transaction_initial_timeout,
83            ),
84            cluster: cluster_type,
85            api_key,
86            http_client: reqwest::Client::new(),
87        }
88    }
89
90    pub fn new_mock(api_key: String, cluster_type: ClusterType) -> Self {
91        let url = format_url(api_key.clone(), cluster_type);
92        HeliusClient {
93            rpc_client: RpcClient::new_mock(url),
94            cluster: cluster_type,
95            api_key,
96            http_client: reqwest::Client::new(),
97        }
98    }
99
100    pub fn new_mock_with_mocks(api_key: String, cluster_type: ClusterType, mocks: Mocks) -> Self {
101        let url = format_url(api_key.clone(), cluster_type);
102        HeliusClient {
103            rpc_client: RpcClient::new_mock_with_mocks(url, mocks),
104            cluster: cluster_type,
105            api_key,
106            http_client: reqwest::Client::new(),
107        }
108    }
109}
110
111fn format_url(api_key: String, cluster_type: ClusterType) -> String {
112    match cluster_type {
113        ClusterType::Testnet => panic!("Testnet cluster not supported"),
114        ClusterType::MainnetBeta => format!("{}{}", MAINNET_RPC_URL, api_key),
115        ClusterType::Devnet => format!("{}{}", DEVNET_RPC_URL, api_key),
116        ClusterType::Development => panic!("Local cluster not supported"),
117    }
118}