Skip to main content

hypercall_ws_protocol/
channel.rs

1//! Channels a client can subscribe to on `/ws`.
2//!
3//! This is the single list of channel names. The API rejects a `Subscribe`
4//! for any other name with an `Error` carrying [`UNKNOWN_CHANNEL_CODE`], and
5//! the fanout applies the same check. Before the check existed the API
6//! confirmed any name and then sent nothing, which left a client on a dead
7//! subscription it could not detect.
8
9/// `code` of the `Error` sent for a `Subscribe` naming an unknown channel.
10pub const UNKNOWN_CHANNEL_CODE: &str = "unknown_channel";
11
12/// Former channels. The API answers a subscribe to one of these with an
13/// error that says where the data moved, rather than `unknown_channel`.
14pub const RETIRED_CHANNELS: &[&str] = &["competition", "competition_engagement"];
15
16/// A subscribable `/ws` channel.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub enum WsChannel {
19    Orderbook,
20    Trades,
21    MarketUpdates,
22    OptionsChain,
23    IndexPrices,
24    /// One `IndicativeMarketData` frame per row.
25    IndicativeMarketData,
26    /// `IndicativeMarketDataBatch` frames: the rows of one short batch
27    /// window per frame. Its `Subscribed` confirmation carries
28    /// `"batch": true`.
29    IndicativeMarketDataV2,
30    OrderUpdates,
31    Fills,
32    Portfolio,
33    Liquidation,
34    Rfq,
35    /// Private RFQ V2 change hints. Subscribing requires a signed read grant
36    /// sent with `AuthenticateRfqV2` first.
37    #[cfg(feature = "rfq-v2")]
38    RfqV2,
39    ExperimentalOrderUpdates,
40    ExperimentalComboTrades,
41    ExperimentalComboFills,
42}
43
44impl WsChannel {
45    #[cfg(not(feature = "rfq-v2"))]
46    pub const ALL: [Self; 15] = [
47        Self::Orderbook,
48        Self::Trades,
49        Self::MarketUpdates,
50        Self::OptionsChain,
51        Self::IndexPrices,
52        Self::IndicativeMarketData,
53        Self::IndicativeMarketDataV2,
54        Self::OrderUpdates,
55        Self::Fills,
56        Self::Portfolio,
57        Self::Liquidation,
58        Self::Rfq,
59        Self::ExperimentalOrderUpdates,
60        Self::ExperimentalComboTrades,
61        Self::ExperimentalComboFills,
62    ];
63
64    #[cfg(feature = "rfq-v2")]
65    pub const ALL: [Self; 16] = [
66        Self::Orderbook,
67        Self::Trades,
68        Self::MarketUpdates,
69        Self::OptionsChain,
70        Self::IndexPrices,
71        Self::IndicativeMarketData,
72        Self::IndicativeMarketDataV2,
73        Self::OrderUpdates,
74        Self::Fills,
75        Self::Portfolio,
76        Self::Liquidation,
77        Self::Rfq,
78        Self::RfqV2,
79        Self::ExperimentalOrderUpdates,
80        Self::ExperimentalComboTrades,
81        Self::ExperimentalComboFills,
82    ];
83
84    /// The exact wire name.
85    pub const fn as_str(self) -> &'static str {
86        match self {
87            Self::Orderbook => "orderbook",
88            Self::Trades => "trades",
89            Self::MarketUpdates => "market_updates",
90            Self::OptionsChain => "options_chain",
91            Self::IndexPrices => "index_prices",
92            Self::IndicativeMarketData => "indicative_market_data",
93            Self::IndicativeMarketDataV2 => "indicative_market_data_v2",
94            Self::OrderUpdates => "order_updates",
95            Self::Fills => "fills",
96            Self::Portfolio => "portfolio",
97            Self::Liquidation => "liquidation",
98            Self::Rfq => "rfq",
99            #[cfg(feature = "rfq-v2")]
100            Self::RfqV2 => "rfq_v2",
101            Self::ExperimentalOrderUpdates => "experimental_order_updates",
102            Self::ExperimentalComboTrades => "experimental_combo_trades",
103            Self::ExperimentalComboFills => "experimental_combo_fills",
104        }
105    }
106
107    /// Parse an exact wire name (case-sensitive, as the API has always
108    /// matched channel names).
109    pub fn parse(name: &str) -> Option<Self> {
110        Self::ALL
111            .into_iter()
112            .find(|channel| channel.as_str() == name)
113    }
114
115    /// The message of the `Error` that rejects a subscribe to `name`.
116    pub fn unknown_channel_message(name: &str) -> String {
117        format!("Unknown channel '{name}'")
118    }
119}
120
121impl std::fmt::Display for WsChannel {
122    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        formatter.write_str(self.as_str())
124    }
125}
126
127#[cfg(test)]
128#[path = "channel_test.rs"]
129mod tests;