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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use async_trait::async_trait;
use std::collections::{BTreeMap, 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 = "coinbase_pro";
const WEBSOCKET_URL: &str = "wss://ws-feed.exchange.coinbase.com";
pub struct CoinbaseProWSClient {
client: WSClientInternal<CoinbaseProMessageHandler>,
translator: CoinbaseProCommandTranslator,
}
impl_new_constructor!(
CoinbaseProWSClient,
EXCHANGE_NAME,
WEBSOCKET_URL,
CoinbaseProMessageHandler {},
CoinbaseProCommandTranslator {}
);
impl_trait!(Trade, CoinbaseProWSClient, subscribe_trade, "matches");
impl_trait!(Ticker, CoinbaseProWSClient, subscribe_ticker, "ticker");
#[rustfmt::skip]
impl_trait!(OrderBook, CoinbaseProWSClient, subscribe_orderbook, "level2");
#[rustfmt::skip]
impl_trait!(Level3OrderBook, CoinbaseProWSClient, subscribe_l3_orderbook, "full");
panic_bbo!(CoinbaseProWSClient);
panic_candlestick!(CoinbaseProWSClient);
panic_l2_topk!(CoinbaseProWSClient);
impl_ws_client_trait!(CoinbaseProWSClient);
struct CoinbaseProMessageHandler {}
struct CoinbaseProCommandTranslator {}
impl MessageHandler for CoinbaseProMessageHandler {
fn handle_message(&mut self, msg: &str) -> MiscMessage {
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::Other;
}
let obj = resp.unwrap();
match obj.get("type").unwrap().as_str().unwrap() {
"error" => {
error!("Received {} from {}", msg, EXCHANGE_NAME);
if obj.contains_key("reason")
&& obj
.get("reason")
.unwrap()
.as_str()
.unwrap()
.contains("is not a valid product")
{
panic!("Received {} from {}", msg, EXCHANGE_NAME);
} else {
MiscMessage::Other
}
}
"subscriptions" => {
info!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Other
}
"heartbeat" => {
debug!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Other
}
_ => MiscMessage::Normal,
}
}
fn get_ping_msg_and_interval(&self) -> Option<(Message, u64)> {
None
}
}
impl CommandTranslator for CoinbaseProCommandTranslator {
fn translate_to_commands(&self, subscribe: bool, topics: &[(String, String)]) -> Vec<String> {
let mut commands: Vec<String> = Vec::new();
let mut channel_symbols = BTreeMap::<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()]);
}
}
}
if !channel_symbols.is_empty() {
let mut command = String::new();
command.push_str(
format!(
r#"{{"type":"{}","channels": ["#,
if subscribe {
"subscribe"
} else {
"unsubscribe"
}
)
.as_str(),
);
for (channel, symbols) in channel_symbols.iter() {
command.push_str(
format!(
r#"{{"name":"{}","product_ids":{}}}"#,
channel,
serde_json::to_string(symbols).unwrap(),
)
.as_str(),
);
command.push(',')
}
command.pop();
command.push_str("]}");
commands.push(command);
}
commands
}
fn translate_to_candlestick_commands(
&self,
_subscribe: bool,
_symbol_interval_list: &[(String, usize)],
) -> Vec<String> {
panic!("CoinbasePro does NOT have candlestick channel");
}
}
#[cfg(test)]
mod tests {
use crate::common::command_translator::CommandTranslator;
#[test]
fn test_two_symbols() {
let translator = super::CoinbaseProCommandTranslator {};
let commands = translator.translate_to_commands(
true,
&[
("matches".to_string(), "BTC-USD".to_string()),
("matches".to_string(), "ETH-USD".to_string()),
],
);
assert_eq!(1, commands.len());
assert_eq!(
r#"{"type":"subscribe","channels": [{"name":"matches","product_ids":["BTC-USD","ETH-USD"]}]}"#,
commands[0]
);
}
#[test]
fn test_two_channels() {
let translator = super::CoinbaseProCommandTranslator {};
let commands = translator.translate_to_commands(
true,
&[
("matches".to_string(), "BTC-USD".to_string()),
("level2".to_string(), "BTC-USD".to_string()),
],
);
assert_eq!(1, commands.len());
assert_eq!(
r#"{"type":"subscribe","channels": [{"name":"level2","product_ids":["BTC-USD"]},{"name":"matches","product_ids":["BTC-USD"]}]}"#,
commands[0]
);
}
}