rusty_feeder/exchange/binance/futures/data/
subscription.rs

1//! Subscription message types for Binance Futures WebSocket API
2//!
3//! This module provides functions for creating subscription messages
4//! for various Binance Futures WebSocket streams.
5
6use serde::{Deserialize, Serialize};
7use smallvec::SmallVec;
8use smartstring::alias::String;
9
10/// Creates a trade stream subscription for the given symbols (Futures)
11#[inline]
12pub fn create_trade_subscription(
13    _id: &str,
14    symbols: SmallVec<[String; 8]>,
15    request_id: Option<u64>,
16    _api_key: Option<&str>,
17    _test: bool,
18) -> WebSocketSubscribeRequest {
19    let streams = symbols
20        .iter()
21        .map(|s| format!("{}@aggTrade", s.to_lowercase()).into())
22        .collect();
23
24    WebSocketSubscribeRequest {
25        id: request_id.unwrap_or(1),
26        method: "SUBSCRIBE".into(),
27        params: streams,
28    }
29}
30
31/// Creates an order book stream subscription for the given symbols (Futures)
32#[inline]
33pub fn create_orderbook_subscription(
34    _id: &str,
35    symbols: SmallVec<[String; 8]>,
36    depth: Option<usize>,
37    request_id: Option<u64>,
38    _api_key: Option<&str>,
39    _test: bool,
40) -> WebSocketSubscribeRequest {
41    // Depth can be 5, 10, or 20
42    let depth_str = match depth {
43        Some(5) => "5",
44        Some(10) => "10",
45        Some(20) => "20",
46        _ => "10", // Default to 10
47    };
48
49    let streams = symbols
50        .iter()
51        .map(|s| format!("{}@depth{}", s.to_lowercase(), depth_str).into())
52        .collect();
53
54    WebSocketSubscribeRequest {
55        id: request_id.unwrap_or(1),
56        method: "SUBSCRIBE".into(),
57        params: streams,
58    }
59}
60
61/// Creates a diff depth stream subscription for the given symbols (Futures)
62#[inline]
63pub fn create_diff_depth_subscription(
64    _id: &str,
65    symbols: SmallVec<[String; 8]>,
66    request_id: Option<u64>,
67    _api_key: Option<&str>,
68    _test: bool,
69) -> WebSocketSubscribeRequest {
70    let streams = symbols
71        .iter()
72        .map(|s| format!("{}@depth@100ms", s.to_lowercase()).into())
73        .collect();
74
75    WebSocketSubscribeRequest {
76        id: request_id.unwrap_or(1),
77        method: "SUBSCRIBE".into(),
78        params: streams,
79    }
80}
81
82/// Creates a kline/candlestick stream subscription for the given symbols (Futures)
83#[inline]
84pub fn create_kline_subscription(
85    _id: &str,
86    symbols: SmallVec<[String; 8]>,
87    interval: &str,
88    request_id: Option<u64>,
89    _api_key: Option<&str>,
90    _test: bool,
91) -> WebSocketSubscribeRequest {
92    let streams = symbols
93        .iter()
94        .map(|s| format!("{}@kline_{}", s.to_lowercase(), interval).into())
95        .collect();
96
97    WebSocketSubscribeRequest {
98        id: request_id.unwrap_or(1),
99        method: "SUBSCRIBE".into(),
100        params: streams,
101    }
102}
103
104/// Creates a mark price stream subscription for the given symbols (Futures)
105#[inline]
106pub fn create_mark_price_subscription(
107    _id: &str,
108    symbols: SmallVec<[String; 8]>,
109    request_id: Option<u64>,
110    _api_key: Option<&str>,
111    _test: bool,
112) -> WebSocketSubscribeRequest {
113    let streams = symbols
114        .iter()
115        .map(|s| format!("{}@markPrice@1s", s.to_lowercase()).into())
116        .collect();
117
118    WebSocketSubscribeRequest {
119        id: request_id.unwrap_or(1),
120        method: "SUBSCRIBE".into(),
121        params: streams,
122    }
123}
124
125/// Creates a liquidation order stream subscription for the given symbols (Futures)
126#[inline]
127pub fn create_liquidation_subscription(
128    _id: &str,
129    symbols: SmallVec<[String; 8]>,
130    request_id: Option<u64>,
131    _api_key: Option<&str>,
132    _test: bool,
133) -> WebSocketSubscribeRequest {
134    let streams = symbols
135        .iter()
136        .map(|s| format!("{}@forceOrder", s.to_lowercase()).into())
137        .collect();
138
139    WebSocketSubscribeRequest {
140        id: request_id.unwrap_or(1),
141        method: "SUBSCRIBE".into(),
142        params: streams,
143    }
144}
145
146/// WebSocket subscription request (Futures)
147#[derive(Debug, Clone, Serialize, Deserialize)]
148pub struct WebSocketSubscribeRequest {
149    /// Request ID
150    pub id: u64,
151
152    /// Method ("SUBSCRIBE" or "UNSUBSCRIBE")
153    pub method: String,
154
155    /// Stream names to subscribe to
156    pub params: SmallVec<[String; 8]>,
157}
158
159/// WebSocket subscription response (Futures)
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct WebSocketSubscribeResponse {
162    /// Request ID
163    pub id: u64,
164
165    /// Response code (200 for success)
166    pub result: Option<simd_json::OwnedValue>,
167
168    /// Error code
169    #[serde(default)]
170    pub code: i32,
171
172    /// Error message
173    #[serde(default)]
174    pub msg: String,
175}