Skip to main content

helius_stream/
types.rs

1use std::time::Duration;
2
3/// A Solana account public key — 32 raw bytes.
4pub type Pubkey = [u8; 32];
5
6/// An account update received from the stream.
7#[derive(Debug, Clone)]
8pub struct AccountUpdate {
9    pub pubkey: Pubkey,
10    pub lamports: u64,
11    pub data: Vec<u8>,
12    pub slot: u64,
13    pub write_version: u64,
14}
15
16/// Configuration for a single stream connection.
17#[derive(Debug, Clone)]
18pub struct StreamConfig {
19    pub endpoint: String,
20    pub api_key: String,
21    pub staleness_threshold: Duration,
22}
23
24impl StreamConfig {
25    /// Default mainnet endpoint.
26    pub fn mainnet(api_key: impl Into<String>) -> Self {
27        Self {
28            endpoint: "wss://mainnet.helius-rpc.com/".into(),
29            api_key: api_key.into(),
30            staleness_threshold: Duration::from_millis(500),
31        }
32    }
33
34    /// Default devnet endpoint.
35    pub fn devnet(api_key: impl Into<String>) -> Self {
36        Self {
37            endpoint: "wss://devnet.helius-rpc.com/".into(),
38            api_key: api_key.into(),
39            staleness_threshold: Duration::from_millis(500),
40        }
41    }
42
43    /// Custom endpoint (e.g., an enterprise / dedicated node URL).
44    pub fn custom(endpoint: impl Into<String>, api_key: impl Into<String>) -> Self {
45        Self {
46            endpoint: endpoint.into(),
47            api_key: api_key.into(),
48            staleness_threshold: Duration::from_millis(500),
49        }
50    }
51
52    /// How long without an update before the stream is considered stale.
53    /// Default: 500ms.
54    pub fn staleness_threshold(mut self, d: Duration) -> Self {
55        self.staleness_threshold = d;
56        self
57    }
58
59    pub(crate) fn ws_url(&self) -> String {
60        let sep = if self.endpoint.contains('?') { '&' } else { '?' };
61        format!("{}{}api-key={}", self.endpoint, sep, self.api_key)
62    }
63}
64
65/// Current operational state of the stream.
66#[derive(Debug, Clone, PartialEq)]
67pub enum StreamState {
68    Disconnected,
69    Connecting { attempt: u32 },
70    Connected { since: std::time::SystemTime },
71    Degraded { gap_slots: u64 },
72    Stale { stale_for_ms: u64 },
73    Failed { reason: String },
74}