Skip to content

Commit

Permalink
feat: allow create Helius from Config with actually using this config
Browse files Browse the repository at this point in the history
  • Loading branch information
xoac committed Sep 25, 2024
1 parent 35045a9 commit 8944d8d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
35 changes: 35 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
use std::sync::Arc;

use reqwest::Client;
use solana_client::nonblocking::rpc_client::RpcClient as AsyncSolanaRpcClient;
use url::Url;

use crate::error::{HeliusError, Result};
use crate::rpc_client::RpcClient;
use crate::types::{Cluster, HeliusEndpoints};
use crate::Helius;

/// Configuration settings for the Helius client
///
Expand Down Expand Up @@ -40,4 +48,31 @@ impl Config {
endpoints,
})
}

pub fn rpc_client_with_reqwest_client(&self, client: Client) -> crate::error::Result<RpcClient> {
RpcClient::new(Arc::new(client), Arc::new(self.clone()))
}

pub fn client_with_async_solana(self) -> crate::error::Result<Helius> {
let mut rpc_url_with_api_key: Url = self.endpoints.rpc.parse()?;
rpc_url_with_api_key
.query_pairs_mut()
.append_pair("api_key", &self.api_key)
.finish();

let reqwest_client = Client::new();

// DAS client
let rpc_client = self.rpc_client_with_reqwest_client(reqwest_client.clone())?;

let async_rpc_client = Arc::new(AsyncSolanaRpcClient::new(rpc_url_with_api_key.to_string()));

Ok(Helius {
config: Arc::new(self.clone()),
client: reqwest_client,
rpc_client: Arc::new(rpc_client),
async_rpc_client: Some(async_rpc_client),
ws_client: None,
})
}
}
10 changes: 6 additions & 4 deletions src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ impl RpcClient {
/// Returns `HeliusError` if the URL isn't formatted correctly or the `RequestHandler` fails to initialize
pub fn new(client: Arc<Client>, config: Arc<Config>) -> Result<Self> {
let handler: RequestHandler = RequestHandler::new(client)?;
let url: String = format!("{}/?api-key={}", config.endpoints.rpc, config.api_key);
let solana_client: Arc<SolanaRpcClient> = Arc::new(SolanaRpcClient::new(url));
let mut url: Url = config.endpoints.rpc.parse()?;
url.query_pairs_mut().append_pair("api-key", &config.api_key);
let solana_client = Arc::new(SolanaRpcClient::new(url.to_string()));

Ok(RpcClient {
handler,
Expand All @@ -81,8 +82,9 @@ impl RpcClient {
R: Debug + Serialize + Send + Sync,
T: Debug + DeserializeOwned + Default,
{
let base_url: String = format!("{}/?api-key={}", self.config.endpoints.rpc, self.config.api_key);
let url: Url = Url::parse(&base_url).expect("Failed to parse URL");
// TODO: why construct URL on every request?
let mut url: Url = self.config.endpoints.rpc.parse()?;
url.query_pairs_mut().append_pair("api-key", &self.config.api_key);

let rpc_request: RpcRequest<R> = RpcRequest::new(method.to_string(), request);
let rpc_response: RpcResponse<T> = self.handler.send(Method::POST, url, Some(&rpc_request)).await?;
Expand Down

0 comments on commit 8944d8d

Please sign in to comment.