crypto_msg_type/
lib.rs

1mod exchanges;
2
3use std::collections::HashMap;
4
5use serde::{Deserialize, Serialize};
6use strum_macros::{Display, EnumString};
7
8/// Crypto message types.
9///
10/// L2Snapshot and L2TopK are very similar, the former is from RESTful API,
11/// the latter is from websocket.
12#[repr(C)]
13#[derive(Copy, Clone, Eq, PartialEq, Serialize, Deserialize, Display, Debug, EnumString, Hash)]
14#[serde(rename_all = "snake_case")]
15#[strum(serialize_all = "snake_case")]
16pub enum MessageType {
17    /// All other messages
18    Other,
19    /// tick-by-tick trade messages
20    Trade,
21    /// Incremental level2 orderbook updates
22    L2Event,
23    /// Level2 snapshot from RESTful API
24    L2Snapshot,
25    /// Level2 top K snapshots from websocket
26    #[serde(rename = "l2_topk")]
27    #[strum(serialize = "l2_topk")]
28    L2TopK,
29    /// Incremental level3 orderbook updates
30    L3Event,
31    /// Level3 snapshot from RESTful API
32    L3Snapshot,
33    /// Best bid and ask
34    #[serde(rename = "bbo")]
35    #[allow(clippy::upper_case_acronyms)]
36    BBO,
37    /// 24hr rolling window ticker
38    Ticker,
39    /// OHLCV candlestick
40    Candlestick,
41    /// Funding rate
42    FundingRate,
43    /// Open interest
44    OpenInterest,
45    /// Long/short ratio
46    LongShortRatio,
47    /// Taker buy/sell volume
48    TakerVolume,
49}
50
51/// Translate to websocket subscribe/unsubscribe commands.
52///
53/// `configs` Some `msg_type` requires a config, for example,
54/// `Candlestick` requires an `interval` parameter.
55pub fn get_ws_commands(
56    exchange: &str,
57    msg_types: &[MessageType],
58    symbols: &[String],
59    subscribe: bool,
60    configs: Option<&HashMap<String, String>>,
61) -> Vec<String> {
62    if msg_types.is_empty() || symbols.is_empty() {
63        return Vec::new();
64    }
65    match exchange {
66        "binance" => exchanges::binance::get_ws_commands(msg_types, symbols, subscribe, configs),
67        "bitfinex" => exchanges::bitfinex::get_ws_commands(msg_types, symbols, subscribe, configs),
68        "bitmex" => exchanges::bitmex::get_ws_commands(msg_types, symbols, subscribe, configs),
69        "bybit" => exchanges::bybit::get_ws_commands(msg_types, symbols, subscribe, configs),
70        "deribit" => exchanges::deribit::get_ws_commands(msg_types, symbols, subscribe, configs),
71        "ftx" => exchanges::ftx::get_ws_commands(msg_types, symbols, subscribe, configs),
72        "huobi" => exchanges::huobi::get_ws_commands(msg_types, symbols, subscribe, configs),
73        "okex" => exchanges::okex::get_ws_commands(msg_types, symbols, subscribe, configs),
74        "okx" => exchanges::okx::get_ws_commands(msg_types, symbols, subscribe, configs),
75        _ => Vec::new(),
76    }
77}