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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! A module for communicating with the [Bybit API](https://bybit-exchange.github.io/docs/spot/v3/#t-introduction).
//! For example usages, see files in the examples/ directory.

use std::{time::SystemTime, borrow::Cow, marker::PhantomData, vec};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use serde::{Serialize, de::DeserializeOwned};
use serde_json::json;
use generic_api_client::{http::{*, header::HeaderValue}, websocket::*};
use crate::traits::*;

/// The type returned by [Client::request()].
pub type BybitRequestResult<T> = Result<T, BybitRequestError>;
pub type BybitRequestError = RequestError<&'static str, BybitHandlerError>;

/// Options that can be set when creating handlers
pub enum BybitOption {
    /// [Default] variant, does nothing
    Default,
    /// API key
    Key(String),
    /// Api secret
    Secret(String),
    /// Base url for HTTP requests
    HttpUrl(BybitHttpUrl),
    /// Type of authentication used for HTTP requests.
    HttpAuth(BybitHttpAuth),
    /// receive window parameter used for requests
    RecvWindow(i32),
    /// [RequestConfig] used when sending requests.
    /// `url_prefix` will be overridden by [HttpUrl](Self::HttpUrl) unless `HttpUrl` is [BybitHttpUrl::None].
    RequestConfig(RequestConfig),
    /// Base url for WebSocket connections
    WebSocketUrl(BybitWebSocketUrl),
    /// Whether [BybitWebSocketHandler] should perform authentication
    WebSocketAuth(bool),
    /// The topics to subscribe to.
    WebSocketTopics(Vec<String>),
    /// [WebSocketConfig] used for creating [WebSocketConnection]s
    /// `url_prefix` will be overridden by [WebSocketUrl](Self::WebSocketUrl) unless `WebSocketUrl` is [BybitWebSocketUrl::None].
    /// By default, `ignore_duplicate_during_reconnection` is set to `true`.
    WebSocketConfig(WebSocketConfig),
}

/// A `struct` that represents a set of [BybitOption] s.
#[derive(Clone, Debug)]
pub struct BybitOptions {
    /// see [BybitOption::Key]
    pub key: Option<String>,
    /// see [BybitOption::Secret]
    pub secret: Option<String>,
    /// see [BybitOption::HttpUrl]
    pub http_url: BybitHttpUrl,
    /// see [BybitOption::HttpAuth]
    pub http_auth: BybitHttpAuth,
    /// see [BybitOption::RecvWindow]
    pub recv_window: Option<i32>,
    /// see [BybitOption::RequestConfig]
    pub request_config: RequestConfig,
    /// see [BybitOption::WebSocketUrl]
    pub websocket_url: BybitWebSocketUrl,
    /// see [BybitOption::WebSocketAuth]
    pub websocket_auth: bool,
    /// see [BybitOption::WebSocketTopics]
    pub websocket_topics: Vec<String>,
    /// see [BybitOption::WebSocketConfig]
    pub websocket_config: WebSocketConfig,
}

/// A `enum` that represents the base url of the Bybit REST API.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum BybitHttpUrl {
    /// `https://api.bybit.com`
    Bybit,
    /// `https://api.bytick.com`
    Bytick,
    /// `https://api-testnet.bybit.com`
    Test,
    /// The url will not be modified by [BybitRequestHandler]
    None,
}

/// A `enum` that represents the base url of the Bybit WebSocket API.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum BybitWebSocketUrl {
    /// `wss://stream.bybit.com`
    Bybit,
    /// `wss://stream.bytick.com`
    Bytick,
    /// `wss://stream-testnet.bybit.com`
    Test,
    /// The url will not be modified by [BybitWebSocketHandler]
    None,
}

/// Represents the auth type.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum BybitHttpAuth {
    /// [Spot V1](https://bybit-exchange.github.io/docs-legacy/spot/v1/#t-introduction)
    SpotV1,
    /// "Previous Version" APIs except for [Spot V1](https://bybit-exchange.github.io/docs-legacy/spot/v1/#t-introduction),
    /// [USDC Option](https://bybit-exchange.github.io/docs-legacy/usdc/option/#t-introduction), and
    /// [USDC Perpetual](https://bybit-exchange.github.io/docs-legacy/usdc/perpetual/#t-introduction)
    BelowV3,
    /// [USDC Option](https://bybit-exchange.github.io/docs-legacy/usdc/option/#t-introduction) and
    /// [USDC Perpetual](https://bybit-exchange.github.io/docs-legacy/usdc/perpetual/#t-introduction)
    UsdcContractV1,
    /// [V3](https://bybit-exchange.github.io/docs/v3/intro) and [V5](https://bybit-exchange.github.io/docs/v5/intro)
    V3AndAbove,
    /// No authentication (for public APIs)
    None,
}

#[derive(Debug)]
pub enum BybitHandlerError {
    ApiError(serde_json::Value),
    IpBan(serde_json::Value),
    ParseError,
}

/// A `struct` that implements [RequestHandler]
pub struct BybitRequestHandler<'a, R: DeserializeOwned> {
    options: BybitOptions,
    _phantom: PhantomData<&'a R>,
}

pub struct BybitWebSocketHandler {
    message_handler: Box<dyn FnMut(serde_json::Value) + Send>,
    options: BybitOptions,
}

impl<'a, B, R> RequestHandler<B> for BybitRequestHandler<'a, R>
where
    B: Serialize,
    R: DeserializeOwned,
{
    type Successful = R;
    type Unsuccessful = BybitHandlerError;
    type BuildError = &'static str;

    fn request_config(&self) -> RequestConfig {
        let mut config = self.options.request_config.clone();
        if self.options.http_url != BybitHttpUrl::None {
            config.url_prefix = self.options.http_url.as_str().to_owned();
        }
        config
    }

    fn build_request(&self, mut builder: RequestBuilder, request_body: &Option<B>, _: u8) -> Result<Request, Self::BuildError> {
        if self.options.http_auth == BybitHttpAuth::None {
            if let Some(body) = request_body {
                let json = serde_json::to_string(body).or(Err("could not serialize body as application/json"))?;
                builder = builder
                    .header(header::CONTENT_TYPE, "application/json")
                    .body(json);
            }
            return builder.build().or(Err("failed to build request"));
        }

        let key = self.options.key.as_deref().ok_or("API key not set")?;
        let secret = self.options.secret.as_deref().ok_or("API secret not set")?;

        let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); // always after the epoch
        let timestamp = time.as_millis();

        let hmac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap(); // hmac accepts key of any length

        match self.options.http_auth {
            BybitHttpAuth::SpotV1 => Self::v1_auth(builder, request_body, key, timestamp, hmac, true, self.options.recv_window),
            BybitHttpAuth::BelowV3 => Self::v1_auth(builder, request_body, key, timestamp, hmac, false, self.options.recv_window),
            BybitHttpAuth::UsdcContractV1 => Self::v3_auth(builder, request_body, key, timestamp, hmac, true, self.options.recv_window),
            BybitHttpAuth::V3AndAbove => Self::v3_auth(builder, request_body, key, timestamp, hmac, false, self.options.recv_window),
            BybitHttpAuth::None => unreachable!(), // we've already handled this case
        }
    }

    fn handle_response(&self, status: StatusCode, _: HeaderMap, response_body: Bytes) -> Result<Self::Successful, Self::Unsuccessful> {
        if status.is_success() {
            serde_json::from_slice(&response_body).map_err(|error| {
                log::debug!("Failed to parse response due to an error: {}", error);
                BybitHandlerError::ParseError
            })
        } else {
            // https://bybit-exchange.github.io/docs/spot/v3/#t-ratelimits
            let error = match serde_json::from_slice(&response_body) {
                Ok(parsed) => {
                    if status == 403 {
                        BybitHandlerError::IpBan(parsed)
                    } else {
                        BybitHandlerError::ApiError(parsed)
                    }
                }
                Err(error) => {
                    log::debug!("Failed to parse error response due to an error: {}", error);
                    BybitHandlerError::ParseError
                },
            };
            Err(error)
        }
    }
}

impl<'a, R> BybitRequestHandler<'a, R> where R: DeserializeOwned {
    fn v1_auth<B>(builder: RequestBuilder, request_body: &Option<B>, key: &str, timestamp: u128, mut hmac: Hmac<Sha256>, spot: bool, window: Option<i32>)
        -> Result<Request, <BybitRequestHandler<'a, R> as RequestHandler<B>>::BuildError>
    where
        B: Serialize,
    {
        fn sort_and_add<'a>(mut pairs: Vec<(Cow<str>, Cow<'a, str>)>, key: &'a str, timestamp: u128) -> String {
            pairs.push((Cow::Borrowed("api_key"), Cow::Borrowed(key)));
            pairs.push((Cow::Borrowed("timestamp"), Cow::Owned(timestamp.to_string())));
            pairs.sort_unstable();

            let mut urlencoded = String::new();
            for (key, value) in pairs {
                urlencoded.push_str(&key);
                if !value.is_empty() {
                    urlencoded.push('=');
                    urlencoded.push_str(&value);
                }
                urlencoded.push('&');
            }
            urlencoded.pop(); // the last '&'
            urlencoded
        }

        let mut request = builder.build().or(Err("failed to build request"))?;
        if matches!(*request.method(), Method::GET | Method::DELETE) {
            let mut queries: Vec<_> = request.url().query_pairs().collect();
            if let Some(window) = window {
                if spot {
                    queries.push((Cow::Borrowed("recvWindow"), Cow::Owned(window.to_string())));
                } else {
                    queries.push((Cow::Borrowed("recv_window"), Cow::Owned(window.to_string())));
                }
            }
            let query = sort_and_add(queries, key, timestamp);
            request.url_mut().set_query(Some(&query));

            hmac.update(query.as_bytes());
            let signature = hex::encode(hmac.finalize().into_bytes());

            request.url_mut().query_pairs_mut().append_pair("sign", &signature);

            if let Some(body) = request_body {
                if spot {
                    let body_string = serde_urlencoded::to_string(body).or(Err("could not serialize body as application/x-www-form-urlencoded"))?;
                    *request.body_mut() = Some(body_string.into());
                    request.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded"));
                } else {
                    let body_string = serde_json::to_string(body).or(Err("could not serialize body as application/json"))?;
                    *request.body_mut() = Some(body_string.into());
                    request.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
                }
            }
        } else {
            let mut body = if let Some(body) = request_body {
                serde_urlencoded::to_string(body).or(Err("could not serialize body as application/x-www-form-urlencoded"))?
            } else {
                String::new()
            };
            if let Some(window) = window {
                if !body.is_empty() {
                    body.push('&');
                }
                if spot {
                    body.push_str("recvWindow=");
                } else {
                    body.push_str("recv_window=");
                }
                body.push_str(&window.to_string());
            }

            let pairs: Vec<_> = body.split('&')
                .map(|pair| pair.split_once('=').unwrap_or((pair, "")))
                .map(|(k, v)| (Cow::Borrowed(k), Cow::Borrowed(v)))
                .collect();
            let mut sorted_query_string = sort_and_add(pairs, key, timestamp);

            hmac.update(sorted_query_string.as_bytes());
            let signature = hex::encode(hmac.finalize().into_bytes());

            sorted_query_string.push_str(&format!("&sign={signature}"));

            if spot {
                *request.body_mut() = Some(sorted_query_string.into());
                request.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/x-www-form-urlencoded"));
            } else {
                let body: serde_json::Value = serde_urlencoded::from_str(&sorted_query_string).unwrap(); // sorted_query_string is always in urlencoded format
                *request.body_mut() = Some(body.to_string().into());
                request.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
            }
        }
        Ok(request)
    }

    fn v3_auth<B>(mut builder: RequestBuilder, request_body: &Option<B>, key: &str, timestamp: u128, mut hmac: Hmac<Sha256>, version_header: bool, window: Option<i32>)
        -> Result<Request, <BybitRequestHandler<'a, R> as RequestHandler<B>>::BuildError>
    where
        B: Serialize,
    {
        let body = if let Some(body) = request_body {
            let json = serde_json::to_value(body).or(Err("could not serialize body as application/json"))?;
            builder = builder
                .header(header::CONTENT_TYPE, "application/json")
                .body(json.to_string());
            Some(json)
        } else {
            None
        };

        let mut request = builder.build().or(Err("failed to build request"))?;

        let mut sign_contents = format!("{timestamp}{key}");
        if let Some(window) = window {
            sign_contents.push_str(&window.to_string());
        }

        if matches!(*request.method(), Method::GET | Method::DELETE) {
            if let Some(query) = request.url().query() {
                sign_contents.push_str(query);
            }
        } else {
            let body = body.unwrap_or_else(|| {
                *request.body_mut() = Some("{}".into());
                request.headers_mut().insert(header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
                json!({})
            });
            sign_contents.push_str(&body.to_string());
        }

        hmac.update(sign_contents.as_bytes());
        let signature = hex::encode(hmac.finalize().into_bytes());

        let headers = request.headers_mut();
        if version_header {
            headers.insert("X-BAPI-SIGN-TYPE", HeaderValue::from(2));
        }
        headers.insert("X-BAPI-SIGN", HeaderValue::from_str(&signature).unwrap()); // hex digits are valid
        headers.insert("X-BAPI-API-KEY", HeaderValue::from_str(key).or(Err("invalid character in API key"))?);
        headers.insert("X-BAPI-TIMESTAMP", HeaderValue::from(timestamp as u64));
        if let Some(window) = window {
            headers.insert("X-BAPI-RECV-WINDOW", HeaderValue::from(window));
        }
        Ok(request)
    }
}

impl WebSocketHandler for BybitWebSocketHandler {
    fn websocket_config(&self) -> WebSocketConfig {
        let mut config = self.options.websocket_config.clone();
        if self.options.websocket_url != BybitWebSocketUrl::None {
            config.url_prefix = self.options.websocket_url.as_str().to_owned();
        }
        config
    }

    fn handle_start(&mut self) -> Vec<WebSocketMessage> {
        if self.options.websocket_auth {
            if let Some(key) = self.options.key.as_deref() {
                if let Some(secret) = self.options.secret.as_deref() {
                    let time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap(); // always after the epoch
                    let expires = time.as_millis() as u64 + 1000;

                    let mut hmac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap(); // hmac accepts key of any length

                    hmac.update(format!("GET/realtime{expires}").as_bytes());
                    let signature = hex::encode(hmac.finalize().into_bytes());

                    return vec![
                        WebSocketMessage::Text(json!({
                            "op": "auth",
                            "args": [key, expires, signature],
                        }).to_string()),
                    ];
                } else {
                    log::debug!("API secret not set.");
                };
            } else {
                log::debug!("API key not set.");
            };
        }
        self.message_subscribe()
    }

    fn handle_message(&mut self, message: WebSocketMessage) -> Vec<WebSocketMessage> {
        match message {
            WebSocketMessage::Text(message) => {
                let message: serde_json::Value = match serde_json::from_str(&message) {
                    Ok(message) => message,
                    Err(_) => {
                        log::debug!("Invalid JSON received");
                        return vec![];
                    },
                };
                match message["op"].as_str() {
                    Some("auth") => {
                        if message["success"].as_bool() == Some(true) {
                            log::debug!("WebSocket authentication successful");
                        } else {
                            log::debug!("WebSocket authentication unsuccessful; message: {}", message["ret_msg"]);
                        }
                        return self.message_subscribe();
                    },
                    Some("subscribe") => {
                        if message["success"].as_bool() == Some(true) {
                            log::debug!("WebSocket topics subscription successful");
                        } else {
                            log::debug!("WebSocket topics subscription unsuccessful; message: {}", message["ret_msg"]);
                        }
                    },
                    _ => (self.message_handler)(message),
                }
            },
            WebSocketMessage::Binary(_) => log::debug!("Unexpected binary message received"),
            WebSocketMessage::Ping(_) | WebSocketMessage::Pong(_) => (),
        }
        vec![]
    }
}

impl BybitWebSocketHandler {
    #[inline(always)]
    fn message_subscribe(&self) -> Vec<WebSocketMessage> {
        vec![WebSocketMessage::Text(
            json!({ "op": "subscribe", "args": self.options.websocket_topics }).to_string(),
        )]
    }
}

impl BybitHttpUrl {
    /// The URL that this variant represents.
    #[inline(always)]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Bybit => "https://api.bybit.com",
            Self::Bytick => "https://api.bytick.com",
            Self::Test => "https://api-testnet.bybit.com",
            Self::None => "",
        }
    }
}

impl BybitWebSocketUrl {
    /// The URL that this variant represents.
    #[inline(always)]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Bybit => "wss://stream.bybit.com",
            Self::Bytick => "wss://stream.bytick.com",
            Self::Test => "wss://stream-testnet.bybit.com",
            Self::None => "",
        }
    }
}

impl HandlerOptions for BybitOptions {
    type OptionItem = BybitOption;

    fn update(&mut self, option: Self::OptionItem) {
        match option {
            BybitOption::Default => (),
            BybitOption::Key(v) => self.key = Some(v),
            BybitOption::Secret(v) => self.secret = Some(v),
            BybitOption::HttpUrl(v) => self.http_url = v,
            BybitOption::HttpAuth(v) => self.http_auth = v,
            BybitOption::RecvWindow(v) => self.recv_window = Some(v),
            BybitOption::RequestConfig(v) => self.request_config = v,
            BybitOption::WebSocketUrl(v) => self.websocket_url = v,
            BybitOption::WebSocketAuth(v) => self.websocket_auth = v,
            BybitOption::WebSocketTopics(v) => self.websocket_topics = v,
            BybitOption::WebSocketConfig(v) => self.websocket_config = v,
        }
    }
}

impl Default for BybitOptions {
    fn default() -> Self {
        let mut websocket_config = WebSocketConfig::new();
        websocket_config.ignore_duplicate_during_reconnection = true;
        Self {
            key: None,
            secret: None,
            http_url: BybitHttpUrl::Bybit,
            http_auth: BybitHttpAuth::None,
            recv_window: None,
            request_config: RequestConfig::default(),
            websocket_url: BybitWebSocketUrl::Bybit,
            websocket_auth: false,
            websocket_topics: vec![],
            websocket_config,
        }
    }
}

impl<'a, R, B> HttpOption<'a, R, B> for BybitOption
where
    R: DeserializeOwned + 'a,
    B: Serialize,
{
    type RequestHandler = BybitRequestHandler<'a, R>;

    #[inline(always)]
    fn request_handler(options: Self::Options) -> Self::RequestHandler {
        BybitRequestHandler::<'a, R> {
            options,
            _phantom: PhantomData,
        }
    }
}

impl <H: FnMut(serde_json::Value) + Send + 'static> WebSocketOption<H> for BybitOption {
    type WebSocketHandler = BybitWebSocketHandler;

    #[inline(always)]
    fn websocket_handler(handler: H, options: Self::Options) -> Self::WebSocketHandler {
        BybitWebSocketHandler {
            message_handler: Box::new(handler),
            options,
        }
    }
}

impl HandlerOption for BybitOption {
    type Options = BybitOptions;
}

impl Default for BybitOption {
    fn default() -> Self {
        Self::Default
    }
}