kalshi_rust/websocket/
channels.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4/// Available WebSocket subscription channels.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6#[serde(rename_all = "snake_case")]
7pub enum Channel {
8    /// Real-time orderbook updates (snapshots + deltas)
9    OrderbookDelta,
10    /// Market ticker updates (price, volume, open interest)
11    Ticker,
12    /// Public trade feed
13    Trade,
14    /// User fills (requires authentication)
15    Fill,
16    /// User market positions (requires authentication)
17    MarketPosition,
18    /// Market lifecycle events (created, activated, settled, etc.)
19    MarketLifecycleV2,
20    /// Event lifecycle events
21    EventLifecycle,
22    /// Multivariate market lookups
23    Multivariate,
24    /// RFQ and quote events (requires authentication)
25    Communications,
26}
27
28impl fmt::Display for Channel {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        let s = match self {
31            Channel::OrderbookDelta => "orderbook_delta",
32            Channel::Ticker => "ticker",
33            Channel::Trade => "trade",
34            Channel::Fill => "fill",
35            Channel::MarketPosition => "market_position",
36            Channel::MarketLifecycleV2 => "market_lifecycle_v2",
37            Channel::EventLifecycle => "event_lifecycle",
38            Channel::Multivariate => "multivariate",
39            Channel::Communications => "communications",
40        };
41        write!(f, "{}", s)
42    }
43}
44
45impl Channel {
46    /// Returns true if this channel requires authentication.
47    pub fn requires_auth(&self) -> bool {
48        matches!(
49            self,
50            Channel::Fill | Channel::MarketPosition | Channel::Communications
51        )
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_channel_display() {
61        assert_eq!(Channel::OrderbookDelta.to_string(), "orderbook_delta");
62        assert_eq!(Channel::Ticker.to_string(), "ticker");
63        assert_eq!(Channel::Trade.to_string(), "trade");
64        assert_eq!(Channel::Fill.to_string(), "fill");
65        assert_eq!(Channel::MarketPosition.to_string(), "market_position");
66        assert_eq!(
67            Channel::MarketLifecycleV2.to_string(),
68            "market_lifecycle_v2"
69        );
70        assert_eq!(Channel::EventLifecycle.to_string(), "event_lifecycle");
71        assert_eq!(Channel::Multivariate.to_string(), "multivariate");
72        assert_eq!(Channel::Communications.to_string(), "communications");
73    }
74
75    #[test]
76    fn test_channel_requires_auth() {
77        // Public channels
78        assert!(!Channel::OrderbookDelta.requires_auth());
79        assert!(!Channel::Ticker.requires_auth());
80        assert!(!Channel::Trade.requires_auth());
81        assert!(!Channel::MarketLifecycleV2.requires_auth());
82        assert!(!Channel::EventLifecycle.requires_auth());
83        assert!(!Channel::Multivariate.requires_auth());
84
85        // Private channels
86        assert!(Channel::Fill.requires_auth());
87        assert!(Channel::MarketPosition.requires_auth());
88        assert!(Channel::Communications.requires_auth());
89    }
90
91    #[test]
92    fn test_channel_serialization() {
93        let json = serde_json::to_string(&Channel::OrderbookDelta).unwrap();
94        assert_eq!(json, "\"orderbook_delta\"");
95
96        let json = serde_json::to_string(&Channel::Fill).unwrap();
97        assert_eq!(json, "\"fill\"");
98
99        let json = serde_json::to_string(&Channel::MarketLifecycleV2).unwrap();
100        assert_eq!(json, "\"market_lifecycle_v2\"");
101    }
102
103    #[test]
104    fn test_channel_deserialization() {
105        let channel: Channel = serde_json::from_str("\"orderbook_delta\"").unwrap();
106        assert_eq!(channel, Channel::OrderbookDelta);
107
108        let channel: Channel = serde_json::from_str("\"fill\"").unwrap();
109        assert_eq!(channel, Channel::Fill);
110
111        let channel: Channel = serde_json::from_str("\"market_lifecycle_v2\"").unwrap();
112        assert_eq!(channel, Channel::MarketLifecycleV2);
113    }
114}