ccxt-exchanges 0.1.5

Exchange implementations for CCXT Rust
Documentation
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
//! Bybit-specific error handling.
//!
//! This module provides error parsing for Bybit API responses,
//! mapping Bybit error codes to ccxt-core Error types.

use ccxt_core::error::Error;
use serde_json::Value;
use std::time::Duration;

/// Bybit error codes and their meanings.
///
/// Reference: https://bybit-exchange.github.io/docs/v5/error
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BybitErrorCode {
    /// Invalid request (10001)
    InvalidRequest,
    /// Invalid API key (10003)
    InvalidApiKey,
    /// Invalid signature (10004)
    InvalidSignature,
    /// Permission denied (10005)
    PermissionDenied,
    /// Rate limit exceeded (10006)
    RateLimitExceeded,
    /// IP not allowed (10010)
    IpNotAllowed,
    /// Bad symbol / Invalid symbol (10016)
    BadSymbol,
    /// Insufficient funds (110001)
    InsufficientFunds,
    /// Insufficient available balance (110007)
    InsufficientAvailableBalance,
    /// Order not found (110008)
    OrderNotFound,
    /// Unknown error code
    Unknown(i64),
}

impl BybitErrorCode {
    /// Parses a Bybit error code into a BybitErrorCode.
    pub fn from_code(code: i64) -> Self {
        match code {
            10001 => BybitErrorCode::InvalidRequest,
            10003 => BybitErrorCode::InvalidApiKey,
            10004 => BybitErrorCode::InvalidSignature,
            10005 => BybitErrorCode::PermissionDenied,
            10006 => BybitErrorCode::RateLimitExceeded,
            10010 => BybitErrorCode::IpNotAllowed,
            10016 => BybitErrorCode::BadSymbol,
            110001 => BybitErrorCode::InsufficientFunds,
            110007 => BybitErrorCode::InsufficientAvailableBalance,
            110008 => BybitErrorCode::OrderNotFound,
            n => BybitErrorCode::Unknown(n),
        }
    }

    /// Returns the numeric code for this error.
    pub fn code(&self) -> i64 {
        match self {
            BybitErrorCode::InvalidRequest => 10001,
            BybitErrorCode::InvalidApiKey => 10003,
            BybitErrorCode::InvalidSignature => 10004,
            BybitErrorCode::PermissionDenied => 10005,
            BybitErrorCode::RateLimitExceeded => 10006,
            BybitErrorCode::IpNotAllowed => 10010,
            BybitErrorCode::BadSymbol => 10016,
            BybitErrorCode::InsufficientFunds => 110001,
            BybitErrorCode::InsufficientAvailableBalance => 110007,
            BybitErrorCode::OrderNotFound => 110008,
            BybitErrorCode::Unknown(n) => *n,
        }
    }
}

/// Parses a Bybit API error response and converts it to a ccxt-core Error.
///
/// Bybit API responses follow this format:
/// ```json
/// {
///     "retCode": 10003,
///     "retMsg": "Invalid API key",
///     "result": {},
///     "time": 1234567890123
/// }
/// ```
///
/// # Arguments
///
/// * `response` - The JSON response from Bybit API
///
/// # Returns
///
/// A ccxt-core Error mapped from the Bybit error code.
///
/// # Example
///
/// ```rust
/// use ccxt_exchanges::bybit::error::parse_error;
/// use serde_json::json;
///
/// let response = json!({
///     "retCode": 10003,
///     "retMsg": "Invalid API key"
/// });
///
/// let error = parse_error(&response);
/// assert!(error.as_authentication().is_some());
/// ```
pub fn parse_error(response: &Value) -> Error {
    let code = response
        .get("retCode")
        .and_then(serde_json::Value::as_i64)
        .unwrap_or(0);

    let msg = response
        .get("retMsg")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("Unknown error");

    let error_code = BybitErrorCode::from_code(code);

    match error_code {
        BybitErrorCode::InvalidApiKey
        | BybitErrorCode::InvalidSignature
        | BybitErrorCode::PermissionDenied
        | BybitErrorCode::IpNotAllowed => Error::authentication(msg.to_string()),
        BybitErrorCode::RateLimitExceeded => {
            // Bybit typically suggests waiting 1 second on rate limit
            Error::rate_limit(msg.to_string(), Some(Duration::from_secs(1)))
        }
        BybitErrorCode::InvalidRequest => Error::invalid_request(msg.to_string()),
        BybitErrorCode::InsufficientFunds | BybitErrorCode::InsufficientAvailableBalance => {
            Error::insufficient_balance(msg.to_string())
        }
        BybitErrorCode::BadSymbol => Error::bad_symbol(msg.to_string()),
        BybitErrorCode::OrderNotFound | BybitErrorCode::Unknown(_) => {
            Error::exchange(code.to_string(), msg)
        }
    }
}

/// Checks if a Bybit API response indicates an error.
///
/// Bybit uses 0 as the success code. Any other code indicates an error.
///
/// # Arguments
///
/// * `response` - The JSON response from Bybit API
///
/// # Returns
///
/// `true` if the response indicates an error, `false` otherwise.
pub fn is_error_response(response: &Value) -> bool {
    response.get("retCode").and_then(serde_json::Value::as_i64) != Some(0)
}

/// Extracts the error code from a Bybit API response.
///
/// # Arguments
///
/// * `response` - The JSON response from Bybit API
///
/// # Returns
///
/// The error code as an i64, or 0 if not found.
pub fn extract_error_code(response: &Value) -> i64 {
    response
        .get("retCode")
        .and_then(serde_json::Value::as_i64)
        .unwrap_or(0)
}

/// Extracts the error message from a Bybit API response.
///
/// # Arguments
///
/// * `response` - The JSON response from Bybit API
///
/// # Returns
///
/// The error message as a string, or "Unknown error" if not found.
pub fn extract_error_message(response: &Value) -> &str {
    response
        .get("retMsg")
        .and_then(serde_json::Value::as_str)
        .unwrap_or("Unknown error")
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn test_parse_authentication_error_invalid_api_key() {
        let response = json!({
            "retCode": 10003,
            "retMsg": "Invalid API key"
        });

        let error = parse_error(&response);
        assert!(error.as_authentication().is_some());
        assert!(error.to_string().contains("Invalid API key"));
    }

    #[test]
    fn test_parse_authentication_error_invalid_signature() {
        let response = json!({
            "retCode": 10004,
            "retMsg": "Invalid signature"
        });

        let error = parse_error(&response);
        assert!(error.as_authentication().is_some());
        assert!(error.to_string().contains("Invalid signature"));
    }

    #[test]
    fn test_parse_authentication_error_permission_denied() {
        let response = json!({
            "retCode": 10005,
            "retMsg": "Permission denied"
        });

        let error = parse_error(&response);
        assert!(error.as_authentication().is_some());
        assert!(error.to_string().contains("Permission denied"));
    }

    #[test]
    fn test_parse_authentication_error_ip_not_allowed() {
        let response = json!({
            "retCode": 10010,
            "retMsg": "IP not allowed"
        });

        let error = parse_error(&response);
        assert!(error.as_authentication().is_some());
        assert!(error.to_string().contains("IP not allowed"));
    }

    #[test]
    fn test_parse_rate_limit_error() {
        let response = json!({
            "retCode": 10006,
            "retMsg": "Rate limit exceeded"
        });

        let error = parse_error(&response);
        assert!(error.as_rate_limit().is_some());
        let (msg, retry_after) = error.as_rate_limit().unwrap();
        assert!(msg.contains("Rate limit"));
        assert!(retry_after.is_some());
    }

    #[test]
    fn test_parse_invalid_request_error() {
        let response = json!({
            "retCode": 10001,
            "retMsg": "Invalid request parameters"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Invalid request"));
    }

    #[test]
    fn test_parse_insufficient_funds_error() {
        let response = json!({
            "retCode": 110001,
            "retMsg": "Insufficient balance"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Insufficient balance"));
    }

    #[test]
    fn test_parse_insufficient_available_balance_error() {
        let response = json!({
            "retCode": 110007,
            "retMsg": "Insufficient available balance"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Insufficient"));
    }

    #[test]
    fn test_parse_bad_symbol_error() {
        let response = json!({
            "retCode": 10016,
            "retMsg": "Invalid symbol"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Bad symbol"));
    }

    #[test]
    fn test_parse_order_not_found_error() {
        let response = json!({
            "retCode": 110008,
            "retMsg": "Order not found"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Order not found"));
    }

    #[test]
    fn test_parse_unknown_error() {
        let response = json!({
            "retCode": 99999,
            "retMsg": "Some unknown error"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Some unknown error"));
    }

    #[test]
    fn test_parse_error_missing_code() {
        let response = json!({
            "retMsg": "Error without code"
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Error without code"));
    }

    #[test]
    fn test_parse_error_missing_message() {
        let response = json!({
            "retCode": 10003
        });

        let error = parse_error(&response);
        let display = error.to_string();
        assert!(display.contains("Unknown error"));
    }

    #[test]
    fn test_is_error_response_success() {
        let response = json!({
            "retCode": 0,
            "retMsg": "OK",
            "result": {}
        });

        assert!(!is_error_response(&response));
    }

    #[test]
    fn test_is_error_response_error() {
        let response = json!({
            "retCode": 10003,
            "retMsg": "Invalid API key"
        });

        assert!(is_error_response(&response));
    }

    #[test]
    fn test_is_error_response_missing_code() {
        let response = json!({
            "retMsg": "No code field"
        });

        // Missing code should be treated as error
        assert!(is_error_response(&response));
    }

    #[test]
    fn test_extract_error_code() {
        let response = json!({
            "retCode": 10003,
            "retMsg": "Invalid API key"
        });

        assert_eq!(extract_error_code(&response), 10003);
    }

    #[test]
    fn test_extract_error_code_missing() {
        let response = json!({
            "retMsg": "No code"
        });

        assert_eq!(extract_error_code(&response), 0);
    }

    #[test]
    fn test_extract_error_message() {
        let response = json!({
            "retCode": 10003,
            "retMsg": "Invalid API key"
        });

        assert_eq!(extract_error_message(&response), "Invalid API key");
    }

    #[test]
    fn test_extract_error_message_missing() {
        let response = json!({
            "retCode": 10003
        });

        assert_eq!(extract_error_message(&response), "Unknown error");
    }

    #[test]
    fn test_bybit_error_code_from_code() {
        assert_eq!(
            BybitErrorCode::from_code(10001),
            BybitErrorCode::InvalidRequest
        );
        assert_eq!(
            BybitErrorCode::from_code(10003),
            BybitErrorCode::InvalidApiKey
        );
        assert_eq!(
            BybitErrorCode::from_code(10004),
            BybitErrorCode::InvalidSignature
        );
        assert_eq!(
            BybitErrorCode::from_code(10005),
            BybitErrorCode::PermissionDenied
        );
        assert_eq!(
            BybitErrorCode::from_code(10006),
            BybitErrorCode::RateLimitExceeded
        );
        assert_eq!(
            BybitErrorCode::from_code(10010),
            BybitErrorCode::IpNotAllowed
        );
        assert_eq!(BybitErrorCode::from_code(10016), BybitErrorCode::BadSymbol);
        assert_eq!(
            BybitErrorCode::from_code(110001),
            BybitErrorCode::InsufficientFunds
        );
        assert_eq!(
            BybitErrorCode::from_code(110007),
            BybitErrorCode::InsufficientAvailableBalance
        );
        assert_eq!(
            BybitErrorCode::from_code(110008),
            BybitErrorCode::OrderNotFound
        );
        assert_eq!(
            BybitErrorCode::from_code(99999),
            BybitErrorCode::Unknown(99999)
        );
    }

    #[test]
    fn test_bybit_error_code_code() {
        assert_eq!(BybitErrorCode::InvalidRequest.code(), 10001);
        assert_eq!(BybitErrorCode::InvalidApiKey.code(), 10003);
        assert_eq!(BybitErrorCode::InvalidSignature.code(), 10004);
        assert_eq!(BybitErrorCode::PermissionDenied.code(), 10005);
        assert_eq!(BybitErrorCode::RateLimitExceeded.code(), 10006);
        assert_eq!(BybitErrorCode::IpNotAllowed.code(), 10010);
        assert_eq!(BybitErrorCode::BadSymbol.code(), 10016);
        assert_eq!(BybitErrorCode::InsufficientFunds.code(), 110001);
        assert_eq!(BybitErrorCode::InsufficientAvailableBalance.code(), 110007);
        assert_eq!(BybitErrorCode::OrderNotFound.code(), 110008);
        assert_eq!(BybitErrorCode::Unknown(12345).code(), 12345);
    }
}