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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use async_trait::async_trait;
use std::collections::HashMap;
use tokio_tungstenite::tungstenite::Message;
use super::EXCHANGE_NAME;
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;
const WEBSOCKET_URL: &str = "wss://futures.kraken.com/ws/v1";
pub struct KrakenFuturesWSClient {
client: WSClientInternal<KrakenMessageHandler>,
translator: KrakenCommandTranslator,
}
impl_new_constructor!(
KrakenFuturesWSClient,
EXCHANGE_NAME,
WEBSOCKET_URL,
KrakenMessageHandler {},
KrakenCommandTranslator {}
);
#[rustfmt::skip]
impl_trait!(Trade, KrakenFuturesWSClient, subscribe_trade, "trade");
#[rustfmt::skip]
impl_trait!(OrderBook, KrakenFuturesWSClient, subscribe_orderbook, "book");
#[rustfmt::skip]
impl_trait!(Ticker, KrakenFuturesWSClient, subscribe_ticker, "ticker");
panic_bbo!(KrakenFuturesWSClient);
panic_l2_topk!(KrakenFuturesWSClient);
panic_l3_orderbook!(KrakenFuturesWSClient);
panic_candlestick!(KrakenFuturesWSClient);
impl_ws_client_trait!(KrakenFuturesWSClient);
struct KrakenMessageHandler {}
struct KrakenCommandTranslator {}
impl KrakenCommandTranslator {
fn channel_symbols_to_command(channel: &str, symbols: &[String], subscribe: bool) -> String {
format!(
r#"{{"event":"{}","feed":"{}","product_ids":{}}}"#,
if subscribe {
"subscribe"
} else {
"unsubscribe"
},
channel,
serde_json::to_string(symbols).unwrap(),
)
}
}
impl MessageHandler for KrakenMessageHandler {
fn handle_message(&mut self, msg: &str) -> MiscMessage {
let obj = serde_json::from_str::<HashMap<String, Value>>(msg).unwrap();
if obj.contains_key("event") {
let event = obj.get("event").unwrap().as_str().unwrap();
match event {
"error" => panic!("Received {} from {}", msg, EXCHANGE_NAME),
_ => {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Other
}
}
} else if obj.contains_key("feed") {
let feed = obj.get("feed").unwrap().as_str().unwrap();
if feed == "heartbeat" {
debug!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::WebSocket(Message::Ping(Vec::new()))
} else if obj.contains_key("product_id") {
MiscMessage::Normal
} else {
MiscMessage::Other
}
} else {
MiscMessage::Other
}
}
fn get_ping_msg_and_interval(&self) -> Option<(String, u64)> {
None
}
}
impl CommandTranslator for KrakenCommandTranslator {
fn translate_to_commands(&self, subscribe: bool, topics: &[(String, String)]) -> Vec<String> {
let mut commands: Vec<String> = Vec::new();
let mut channel_symbols = HashMap::<String, Vec<String>>::new();
for (channel, symbol) in topics {
match channel_symbols.get_mut(channel) {
Some(symbols) => symbols.push(symbol.to_string()),
None => {
channel_symbols.insert(channel.to_string(), vec![symbol.to_string()]);
}
}
}
for (channel, symbols) in channel_symbols.iter() {
commands.push(Self::channel_symbols_to_command(
channel, symbols, subscribe,
));
}
commands.push(r#"{"event":"subscribe","feed":"heartbeat"}"#.to_string());
commands
}
fn translate_to_candlestick_commands(
&self,
_subscribe: bool,
_symbol_interval_list: &[(String, usize)],
) -> Vec<String> {
panic!("Kraken Futures does NOT have candlestick channel");
}
}
#[cfg(test)]
mod tests {
use crate::common::command_translator::CommandTranslator;
#[test]
fn test_one_symbol() {
let translator = super::KrakenCommandTranslator {};
let commands = translator
.translate_to_commands(true, &vec![("trade".to_string(), "PI_XBTUSD".to_string())]);
assert_eq!(2, commands.len());
assert_eq!(
r#"{"event":"subscribe","feed":"trade","product_ids":["PI_XBTUSD"]}"#,
commands[0]
);
assert_eq!(r#"{"event":"subscribe","feed":"heartbeat"}"#, commands[1]);
}
#[test]
fn test_two_symbols() {
let translator = super::KrakenCommandTranslator {};
let commands = translator.translate_to_commands(
true,
&vec![
("trade".to_string(), "PI_XBTUSD".to_string()),
("trade".to_string(), "PI_ETHUSD".to_string()),
],
);
assert_eq!(2, commands.len());
assert_eq!(
r#"{"event":"subscribe","feed":"trade","product_ids":["PI_XBTUSD","PI_ETHUSD"]}"#,
commands[0]
);
assert_eq!(r#"{"event":"subscribe","feed":"heartbeat"}"#, commands[1]);
}
}