nautilus_binance/spot/websocket/public_json/
messages.rs1use nautilus_network::websocket::WebSocketClient;
19use serde::{Deserialize, Serialize};
20use ustr::Ustr;
21
22use crate::{
23 common::enums::{BinanceKlineInterval, BinanceWsMethod},
24 spot::websocket::streams::messages::{
25 BinanceWsErrorMsg, BinanceWsErrorResponse, BinanceWsResponse,
26 },
27};
28
29#[derive(Debug, Clone)]
31pub enum BinanceSpotPublicWsMessage {
32 Trade(BinanceSpotTradeMsg),
34 BookTicker(BinanceSpotBookTickerMsg),
36 DepthSnapshot(BinanceSpotPartialDepthMsg),
38 DepthDiff(BinanceSpotDepthDiffMsg),
40 Kline(BinanceSpotKlineMsg),
42 ServerShutdown(BinanceSpotServerShutdownMsg),
44 RawJson(serde_json::Value),
46 Error(BinanceWsErrorMsg),
48 Reconnected,
50}
51
52#[allow(
54 missing_debug_implementations,
55 clippy::large_enum_variant,
56 reason = "Commands are ephemeral and immediately consumed"
57)]
58pub enum BinanceSpotPublicWsCommand {
59 SetClient(WebSocketClient),
61 Disconnect,
63 Subscribe { streams: Vec<String> },
65 Unsubscribe { streams: Vec<String> },
67}
68
69#[derive(Debug, Clone, Serialize)]
71pub struct BinanceWsSubscription {
72 pub method: BinanceWsMethod,
74 pub params: Vec<String>,
76 pub id: u64,
78}
79
80impl BinanceWsSubscription {
81 #[must_use]
83 pub fn subscribe(streams: Vec<String>, id: u64) -> Self {
84 Self {
85 method: BinanceWsMethod::Subscribe,
86 params: streams,
87 id,
88 }
89 }
90
91 #[must_use]
93 pub fn unsubscribe(streams: Vec<String>, id: u64) -> Self {
94 Self {
95 method: BinanceWsMethod::Unsubscribe,
96 params: streams,
97 id,
98 }
99 }
100}
101
102#[derive(Debug, Clone, Deserialize)]
104pub struct BinanceCombinedStreamEvent {
105 pub stream: String,
107 pub data: serde_json::Value,
109}
110
111#[derive(Debug, Clone, Deserialize)]
113pub struct BinanceSpotTradeMsg {
114 #[serde(rename = "e")]
116 pub event_type: String,
117 #[serde(rename = "E")]
119 pub event_time: i64,
120 #[serde(rename = "s")]
122 pub symbol: Ustr,
123 #[serde(rename = "t")]
125 pub trade_id: u64,
126 #[serde(rename = "p")]
128 pub price: String,
129 #[serde(rename = "q")]
131 pub quantity: String,
132 #[serde(rename = "T")]
134 pub trade_time: i64,
135 #[serde(rename = "m")]
137 pub is_buyer_maker: bool,
138}
139
140#[derive(Debug, Clone, Deserialize)]
142pub struct BinanceSpotBookTickerMsg {
143 #[serde(rename = "e", default)]
145 pub event_type: Option<String>,
146 #[serde(rename = "E", default)]
148 pub event_time: Option<i64>,
149 #[serde(rename = "s")]
151 pub symbol: Ustr,
152 #[serde(rename = "u")]
154 pub book_update_id: u64,
155 #[serde(rename = "b")]
157 pub best_bid_price: String,
158 #[serde(rename = "B")]
160 pub best_bid_qty: String,
161 #[serde(rename = "a")]
163 pub best_ask_price: String,
164 #[serde(rename = "A")]
166 pub best_ask_qty: String,
167 #[serde(rename = "T", default)]
169 pub transaction_time: Option<i64>,
170}
171
172#[derive(Debug, Clone)]
174pub struct BinanceSpotPartialDepthMsg {
175 pub symbol: Ustr,
177 pub last_update_id: u64,
179 pub bids: Vec<[String; 2]>,
181 pub asks: Vec<[String; 2]>,
183}
184
185#[derive(Debug, Clone, Deserialize)]
187pub struct BinanceSpotPartialDepthPayload {
188 #[serde(rename = "lastUpdateId")]
190 pub last_update_id: u64,
191 pub bids: Vec<[String; 2]>,
193 pub asks: Vec<[String; 2]>,
195}
196
197#[derive(Debug, Clone, Deserialize)]
199pub struct BinanceSpotDepthDiffMsg {
200 #[serde(rename = "e")]
202 pub event_type: String,
203 #[serde(rename = "E")]
205 pub event_time: i64,
206 #[serde(rename = "s")]
208 pub symbol: Ustr,
209 #[serde(rename = "U")]
211 pub first_update_id: u64,
212 #[serde(rename = "u")]
214 pub final_update_id: u64,
215 #[serde(rename = "b")]
217 pub bids: Vec<[String; 2]>,
218 #[serde(rename = "a")]
220 pub asks: Vec<[String; 2]>,
221}
222
223#[derive(Debug, Clone, Deserialize)]
225pub struct BinanceSpotKlineMsg {
226 #[serde(rename = "e")]
228 pub event_type: String,
229 #[serde(rename = "E")]
231 pub event_time: i64,
232 #[serde(rename = "s")]
234 pub symbol: Ustr,
235 #[serde(rename = "k")]
237 pub kline: BinanceSpotKlineData,
238}
239
240#[derive(Debug, Clone, Deserialize)]
242pub struct BinanceSpotKlineData {
243 #[serde(rename = "t")]
245 pub start_time: i64,
246 #[serde(rename = "T")]
248 pub close_time: i64,
249 #[serde(rename = "s")]
251 pub symbol: Ustr,
252 #[serde(rename = "i")]
254 pub interval: BinanceKlineInterval,
255 #[serde(rename = "f")]
257 pub first_trade_id: i64,
258 #[serde(rename = "L")]
260 pub last_trade_id: i64,
261 #[serde(rename = "o")]
263 pub open: String,
264 #[serde(rename = "c")]
266 pub close: String,
267 #[serde(rename = "h")]
269 pub high: String,
270 #[serde(rename = "l")]
272 pub low: String,
273 #[serde(rename = "v")]
275 pub volume: String,
276 #[serde(rename = "n")]
278 pub num_trades: i64,
279 #[serde(rename = "x")]
281 pub is_closed: bool,
282}
283
284#[derive(Debug, Clone, Deserialize)]
286pub struct BinanceSpotServerShutdownMsg {
287 #[serde(rename = "e")]
289 pub event_type: String,
290 #[serde(rename = "E")]
292 pub event_time: i64,
293}
294
295pub type BinanceSpotWsResponse = BinanceWsResponse;
296pub type BinanceSpotWsErrorResponse = BinanceWsErrorResponse;