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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use async_trait::async_trait;
use std::collections::HashMap;
use tokio_tungstenite::tungstenite::Message;
use crate::{
clients::common_traits::{
Candlestick, Level3OrderBook, OrderBook, OrderBookTopK, Ticker, Trade, BBO,
},
common::{
command_translator::CommandTranslator,
message_handler::{MessageHandler, MiscMessage},
ws_client_internal::WSClientInternal,
},
WSClient,
};
use log::*;
use serde_json::Value;
pub(super) const EXCHANGE_NAME: &str = "ftx";
const WEBSOCKET_URL: &str = "wss://ftx.com/ws/";
pub struct FtxWSClient {
client: WSClientInternal<FtxMessageHandler>,
translator: FtxCommandTranslator,
}
impl_new_constructor!(
FtxWSClient,
EXCHANGE_NAME,
WEBSOCKET_URL,
FtxMessageHandler {},
FtxCommandTranslator {}
);
impl_trait!(Trade, FtxWSClient, subscribe_trade, "trades");
impl_trait!(BBO, FtxWSClient, subscribe_bbo, "ticker");
#[rustfmt::skip]
impl_trait!(OrderBook, FtxWSClient, subscribe_orderbook, "orderbook");
panic_candlestick!(FtxWSClient);
panic_l2_topk!(FtxWSClient);
panic_l3_orderbook!(FtxWSClient);
panic_ticker!(FtxWSClient);
impl_ws_client_trait!(FtxWSClient);
struct FtxMessageHandler {}
struct FtxCommandTranslator {}
impl MessageHandler for FtxMessageHandler {
fn handle_message(&mut self, msg: &str) -> MiscMessage {
let obj = serde_json::from_str::<HashMap<String, Value>>(msg).unwrap();
let msg_type = obj.get("type").unwrap().as_str().unwrap();
match msg_type {
"pong" => MiscMessage::Pong,
"subscribed" | "unsubscribed" | "info" => {
info!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Other
}
"partial" | "update" => MiscMessage::Normal,
"error" => {
let code = obj.get("code").unwrap().as_i64().unwrap();
match code {
400 => {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
}
_ => panic!("Received {} from {}", msg, EXCHANGE_NAME),
}
MiscMessage::Other
}
_ => {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Other
}
}
}
fn get_ping_msg_and_interval(&self) -> Option<(Message, u64)> {
Some((Message::Text(r#"{"op":"ping"}"#.to_string()), 15))
}
}
impl CommandTranslator for FtxCommandTranslator {
fn translate_to_commands(&self, subscribe: bool, topics: &[(String, String)]) -> Vec<String> {
topics
.iter()
.map(|(channel, symbol)| {
format!(
r#"{{"op":"{}","channel":"{}","market":"{}"}}"#,
if subscribe {
"subscribe"
} else {
"unsubscribe"
},
channel,
symbol
)
})
.collect()
}
fn translate_to_candlestick_commands(
&self,
_subscribe: bool,
_symbol_interval_list: &[(String, usize)],
) -> Vec<String> {
panic!("FTX does NOT have candlestick channel");
}
}
#[cfg(test)]
mod tests {
use crate::common::command_translator::CommandTranslator;
#[test]
fn test_one_topic() {
let translator = super::FtxCommandTranslator {};
let commands = translator
.translate_to_commands(true, &vec![("trades".to_string(), "BTC/USD".to_string())]);
assert_eq!(1, commands.len());
assert_eq!(
r#"{"op":"subscribe","channel":"trades","market":"BTC/USD"}"#,
commands[0]
);
}
#[test]
fn test_two_topic() {
let translator = super::FtxCommandTranslator {};
let commands = translator.translate_to_commands(
true,
&vec![
("trades".to_string(), "BTC/USD".to_string()),
("orderbook".to_string(), "BTC/USD".to_string()),
],
);
assert_eq!(2, commands.len());
assert_eq!(
r#"{"op":"subscribe","channel":"trades","market":"BTC/USD"}"#,
commands[0]
);
assert_eq!(
r#"{"op":"subscribe","channel":"orderbook","market":"BTC/USD"}"#,
commands[1]
);
}
}