1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use crate::WSClient;
use std::collections::HashMap;
use std::sync::mpsc::Sender;
use super::{
ws_client_internal::{MiscMessage, WSClientInternal},
Candlestick, Level3OrderBook, OrderBook, OrderBookTopK, Ticker, Trade, BBO,
};
use log::*;
use serde_json::Value;
pub(super) const EXCHANGE_NAME: &str = "binance";
pub(super) const WEBSOCKET_URL: &str = "wss://stream.opsnest.com/stream";
const CLIENT_PING_INTERVAL_AND_MSG: (u64, &str) = (240, r#"{"event":"ping"}"#);
pub struct BinanceOptionWSClient {
client: WSClientInternal,
}
fn channels_to_commands(channels: &[String], subscribe: bool) -> Vec<String> {
let raw_channels: Vec<&String> = channels.iter().filter(|ch| !ch.starts_with('{')).collect();
let mut all_commands: Vec<String> = channels
.iter()
.filter(|ch| ch.starts_with('{'))
.map(|s| s.to_string())
.collect();
if !raw_channels.is_empty() {
all_commands.append(&mut vec![format!(
r#"{{"id":9527,"method":"{}","params":{}}}"#,
if subscribe {
"SUBSCRIBE"
} else {
"UNSUBSCRIBE"
},
serde_json::to_string(&raw_channels).unwrap()
)])
};
all_commands
}
fn on_misc_msg(msg: &str) -> MiscMessage {
if msg == r#"{"id":9527}"# {
return MiscMessage::Misc;
} else if msg == r#"{"event":"pong"}"# {
return MiscMessage::Pong;
}
let resp = serde_json::from_str::<HashMap<String, Value>>(msg);
if resp.is_err() {
error!("{} is not a JSON string, {}", msg, EXCHANGE_NAME);
return MiscMessage::Misc;
}
let obj = resp.unwrap();
if obj.contains_key("code") {
panic!("Received {} from {}", msg, EXCHANGE_NAME);
}
if let Some(result) = obj.get("result") {
if serde_json::Value::Null == *result {
return MiscMessage::Misc;
}
}
if !obj.contains_key("stream") || !obj.contains_key("data") {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
return MiscMessage::Misc;
}
MiscMessage::Normal
}
impl_new_constructor!(
BinanceOptionWSClient,
EXCHANGE_NAME,
WEBSOCKET_URL,
channels_to_commands,
on_misc_msg,
Some(CLIENT_PING_INTERVAL_AND_MSG),
None
);
impl_ws_client_trait!(BinanceOptionWSClient);
fn to_raw_channel(channel: &str, pair: &str) -> String {
format!("{}@{}", pair, channel)
}
#[rustfmt::skip]
impl_trait!(Trade, BinanceOptionWSClient, subscribe_trade, "trade", to_raw_channel);
#[rustfmt::skip]
impl_trait!(Ticker, BinanceOptionWSClient, subscribe_ticker, "ticker", to_raw_channel);
#[rustfmt::skip]
impl_trait!(BBO, BinanceOptionWSClient, subscribe_bbo, "bookTicker", to_raw_channel);
#[rustfmt::skip]
impl_trait!(OrderBook, BinanceOptionWSClient, subscribe_orderbook, "depth@100ms", to_raw_channel);
#[rustfmt::skip]
impl_trait!(OrderBookTopK, BinanceOptionWSClient, subscribe_orderbook_topk, "depth10", to_raw_channel);
fn to_candlestick_raw_channel(symbol: &str, interval: usize) -> String {
let interval_str = match interval {
60 => "1m",
300 => "5m",
900 => "15m",
1800 => "30m",
3600 => "1h",
14400 => "4h",
86400 => "1d",
604800 => "1w",
_ => panic!("Binance has intervals 1m,5m,15m,30m,1h4h,1d,1w"),
};
format!("{}@kline_{}", symbol, interval_str)
}
impl_candlestick!(BinanceOptionWSClient);
panic_l3_orderbook!(BinanceOptionWSClient);