11use crate :: error:: { HeliusError , Result } ;
2+ use crate :: types:: Cluster ;
23use crate :: types:: { RpcTransactionsConfig , TransactionNotification } ;
34use futures_util:: {
45 future:: { ready, BoxFuture , FutureExt } ,
@@ -29,7 +30,9 @@ use tokio_tungstenite::{
2930 MaybeTlsStream , WebSocketStream ,
3031} ;
3132
32- pub const ENHANCED_WEBSOCKET_URL : & str = "wss://atlas-mainnet.helius-rpc.com/?api-key=" ;
33+ pub const ENHANCED_WEBSOCKET_URL_MAINNET : & str = "wss://atlas-mainnet.helius-rpc.com/?api-key=" ;
34+ pub const ENHANCED_WEBSOCKET_URL_DEVNET : & str = "wss://atlas-devnet.helius-rpc.com/?api-key=" ;
35+
3336pub const DEFAULT_PING_DURATION_SECONDS : u64 = 10 ;
3437pub const DEFAULT_MAX_FAILED_PINGS : usize = 3 ;
3538
@@ -52,6 +55,55 @@ pub struct EnhancedWebsocket {
5255}
5356
5457impl EnhancedWebsocket {
58+ /// Constructs the complete websocket URL for connecting to Helius's enhanced websocket endpoints.
59+ ///
60+ /// # Arguments
61+ ///
62+ /// * `cluster` - The Solana cluster to connect to (MainnetBeta or Devnet)
63+ /// * `api_key` - Your Helius API key
64+ ///
65+ /// # Returns
66+ ///
67+ /// Returns a Result containing the formatted websocket URL or an error if an unsupported cluster is specified.
68+ ///
69+ /// # Errors
70+ ///
71+ /// Returns `HeliusError::EnhancedWebsocket` if the specified cluster is not MainnetBeta or Devnet.
72+ /// Note: StakedMainnetBeta is not supported for websocket connections.
73+ ///
74+ /// # Examples
75+ ///
76+ /// ```rust
77+ /// use helius::websocket::EnhancedWebsocket;
78+ /// use helius::types::Cluster;
79+ ///
80+ /// let api_key = "your_api_key";
81+ ///
82+ /// // For Mainnet
83+ /// let mainnet_url = EnhancedWebsocket::get_url(&Cluster::MainnetBeta, api_key).expect("Failed to get URL");
84+ /// println!("Mainnet URL: {}", mainnet_url);
85+ /// assert!(mainnet_url.eq("wss://atlas-mainnet.helius-rpc.com/?api-key=your_api_key"));
86+ ///
87+ /// // For Devnet
88+ /// let devnet_url = EnhancedWebsocket::get_url(&Cluster::Devnet, api_key).expect("Failed to get URL");
89+ /// println!("Devnet URL: {}", devnet_url);
90+ /// assert!(devnet_url.eq("wss://atlas-devnet.helius-rpc.com/?api-key=your_api_key"));
91+ ///
92+ /// // For Staked Mainnet (will error)
93+ /// let staked_result = EnhancedWebsocket::get_url(&Cluster::StakedMainnetBeta, api_key);
94+ /// assert!(staked_result.is_err());
95+ /// ```
96+ pub fn get_url ( cluster : & Cluster , api_key : & str ) -> Result < String > {
97+ match cluster {
98+ Cluster :: MainnetBeta => Ok ( format ! ( "{}{}" , ENHANCED_WEBSOCKET_URL_MAINNET , api_key) ) ,
99+ Cluster :: Devnet => Ok ( format ! ( "{}{}" , ENHANCED_WEBSOCKET_URL_DEVNET , api_key) ) ,
100+ Cluster :: StakedMainnetBeta => Err ( HeliusError :: EnhancedWebsocket {
101+ reason : "Unsupported cluster" . into ( ) ,
102+ message : "only mainnet and devnet are supported" . into ( ) ,
103+ } ) ,
104+ }
105+ }
106+
55107 /// Expects enhanced websocket endpoint: wss://atlas-mainnet.helius-rpc.com?api-key=<API_KEY>
56108 pub async fn new ( url : & str , ping_interval_secs : Option < u64 > , pong_timeout_secs : Option < u64 > ) -> Result < Self > {
57109 let ( ws, _response) = connect_async ( url) . await . map_err ( HeliusError :: Tungstenite ) ?;
0 commit comments