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
use crate::WSClient;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use super::super::ws_client_internal::{MiscMessage, WSClientInternal};
use super::super::{Candlestick, OrderBook, OrderBookTopK, Ticker, Trade, BBO};
use crate::clients::utils::{ensure_frame_size, WS_FRAME_SIZE};
use log::*;
use serde_json::Value;
const EXCHANGE_NAME: &str = "bitget";
const WEBSOCKET_URL: &str = "wss://csocketapi.bitget.com/ws/v1";
const CLIENT_PING_INTERVAL_AND_MSG: (u64, &str) = (30, "ping");
pub struct BitgetSwapWSClient<'a> {
client: WSClientInternal<'a>,
}
fn topics_to_command(chunk: &[String], subscribe: bool) -> String {
format!(
r#"{{"op":"{}","args":{}}}"#,
if subscribe {
"subscribe"
} else {
"unsubscribe"
},
serde_json::to_string(chunk).unwrap()
)
}
fn channels_to_commands(channels: &[String], subscribe: bool) -> Vec<String> {
ensure_frame_size(channels, subscribe, topics_to_command, WS_FRAME_SIZE, None)
}
fn on_misc_msg(msg: &str) -> MiscMessage {
if msg == "pong" {
return MiscMessage::Pong;
}
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();
if event == "error" {
error!("Received {} from {}", msg, EXCHANGE_NAME);
panic!("Received {} from {}", msg, EXCHANGE_NAME);
} else {
info!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Misc
}
} else if obj.contains_key("table") && obj.contains_key("data") {
MiscMessage::Normal
} else if obj.contains_key("action") {
let action = obj.get("action").unwrap().as_str().unwrap();
if action == "ping" {
info!("Received {} from {}", msg, EXCHANGE_NAME);
} else {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
}
MiscMessage::Misc
} else {
warn!("Received {} from {}", msg, EXCHANGE_NAME);
MiscMessage::Misc
}
}
fn to_raw_channel(channel: &str, symbol: &str) -> String {
format!("swap/{}:{}", channel, symbol)
}
#[rustfmt::skip]
impl_trait!(Trade, BitgetSwapWSClient, subscribe_trade, "trade", to_raw_channel);
#[rustfmt::skip]
impl_trait!(OrderBookTopK, BitgetSwapWSClient, subscribe_orderbook_topk, "depth5", to_raw_channel);
#[rustfmt::skip]
impl_trait!(OrderBook, BitgetSwapWSClient, subscribe_orderbook, "depth", to_raw_channel);
#[rustfmt::skip]
impl_trait!(Ticker, BitgetSwapWSClient, subscribe_ticker, "ticker", to_raw_channel);
impl<'a> BBO for BitgetSwapWSClient<'a> {
fn subscribe_bbo(&self, _symbols: &[String]) {
panic!("Bitget does NOT have BBO channel");
}
}
fn to_candlestick_raw_channel(symbol: &str, interval: usize) -> String {
let valid_set: Vec<usize> = vec![60, 300, 900, 1800, 3600, 14400, 43200, 86400, 604800];
if !valid_set.contains(&interval) {
let joined = valid_set
.into_iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(",");
panic!("Bitget available intervals: {}", joined);
}
let channel = format!("candle{}s", interval);
to_raw_channel(&channel, symbol)
}
impl_candlestick!(BitgetSwapWSClient);
define_client!(
BitgetSwapWSClient,
EXCHANGE_NAME,
WEBSOCKET_URL,
channels_to_commands,
on_misc_msg,
Some(CLIENT_PING_INTERVAL_AND_MSG),
None
);