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
use crate::WSClient;
use std::collections::HashMap;

use super::ws_client_internal::WSClientInternal;
use serde_json::{json, Value};

const WEBSOCKET_URL: &str = "wss://www.bitmex.com/realtime";

// TODO: https://www.bitmex.com/app/wsAPI#Heartbeats

/// The WebSocket client for BitMEX, including Swap and Futures(<https://www.bitmex.com/app/wsAPI>).
pub struct BitMEXWSClient<'a> {
    client: WSClientInternal<'a>,
}

fn serialize_command(channels: &[String], subscribe: bool) -> Vec<String> {
    let mut object = HashMap::<&str, Value>::new();

    object.insert(
        "op",
        serde_json::to_value(if subscribe {
            "subscribe"
        } else {
            "unsubscribe"
        })
        .unwrap(),
    );

    object.insert("args", json!(channels));

    vec![serde_json::to_string(&object).unwrap()]
}

define_client!(BitMEXWSClient, WEBSOCKET_URL, serialize_command);