deribit-base 0.3.1

Base library with common structs, traits, and logic for Deribit API clients
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
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
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 22/7/25
******************************************************************************/

//! Deribit API Constants
//!
//! This module contains all the constants used throughout the Deribit API implementation,
//! including URLs, limits, timeouts, and other configuration values based on the
//! official Deribit API v2.1.1 documentation.

// =============================================================================
// API ENDPOINTS
// =============================================================================

/// Deribit production WebSocket URL
pub const DERIBIT_WS_URL_PROD: &str = "wss://www.deribit.com/ws/api/v2";

/// Deribit test WebSocket URL
pub const DERIBIT_WS_URL_TEST: &str = "wss://test.deribit.com/ws/api/v2";

/// Deribit production HTTP API URL
pub const DERIBIT_HTTP_URL_PROD: &str = "https://www.deribit.com/api/v2";

/// Deribit test HTTP API URL
pub const DERIBIT_HTTP_URL_TEST: &str = "https://test.deribit.com/api/v2";

// =============================================================================
// RATE LIMITS
// =============================================================================

/// Maximum requests per second for authenticated users
pub const MAX_REQUESTS_PER_SECOND_AUTH: u32 = 20;

/// Maximum requests per second for non-authenticated users
pub const MAX_REQUESTS_PER_SECOND_UNAUTH: u32 = 10;

/// Maximum subscriptions per connection
pub const MAX_SUBSCRIPTIONS_PER_CONNECTION: u32 = 200;

/// Maximum message size in bytes
pub const MAX_MESSAGE_SIZE_BYTES: usize = 65536; // 64KB

// =============================================================================
// TIMEOUTS
// =============================================================================

/// Default connection timeout in milliseconds
pub const DEFAULT_CONNECTION_TIMEOUT_MS: u64 = 5000;

/// Default request timeout in milliseconds
pub const DEFAULT_REQUEST_TIMEOUT_MS: u64 = 10000;

/// Heartbeat interval in milliseconds
pub const HEARTBEAT_INTERVAL_MS: u64 = 10000;

/// Maximum time to wait for heartbeat response in milliseconds
pub const HEARTBEAT_TIMEOUT_MS: u64 = 5000;

// =============================================================================
// CURRENCIES
// =============================================================================

/// Supported cryptocurrencies
pub const SUPPORTED_CRYPTOCURRENCIES: &[&str] = &["BTC", "ETH", "SOL", "USDC", "USDT", "EURR"];

/// Bitcoin currency code
pub const CURRENCY_BTC: &str = "BTC";

/// Ethereum currency code
pub const CURRENCY_ETH: &str = "ETH";

/// Solana currency code
pub const CURRENCY_SOL: &str = "SOL";

/// USD Coin currency code
pub const CURRENCY_USDC: &str = "USDC";

/// Tether currency code
pub const CURRENCY_USDT: &str = "USDT";

/// Euro stablecoin currency code
pub const CURRENCY_EURR: &str = "EURR";

// =============================================================================
// INSTRUMENT TYPES
// =============================================================================

/// Future instrument type
pub const INSTRUMENT_TYPE_FUTURE: &str = "future";

/// Option instrument type
pub const INSTRUMENT_TYPE_OPTION: &str = "option";

/// Perpetual instrument type
pub const INSTRUMENT_TYPE_PERPETUAL: &str = "perpetual";

/// Spot instrument type
pub const INSTRUMENT_TYPE_SPOT: &str = "spot";

/// Future combo instrument type
pub const INSTRUMENT_TYPE_FUTURE_COMBO: &str = "future_combo";

/// Option combo instrument type
pub const INSTRUMENT_TYPE_OPTION_COMBO: &str = "option_combo";

// =============================================================================
// ORDER LIMITS
// =============================================================================

/// Minimum order amount for BTC
pub const MIN_ORDER_AMOUNT_BTC: f64 = 0.0001;

/// Minimum order amount for ETH
pub const MIN_ORDER_AMOUNT_ETH: f64 = 0.001;

/// Minimum order amount for SOL
pub const MIN_ORDER_AMOUNT_SOL: f64 = 0.1;

/// Maximum order amount (no specific limit, but practical limit)
pub const MAX_ORDER_AMOUNT: f64 = 1_000_000.0;

/// Maximum number of open orders per instrument
pub const MAX_OPEN_ORDERS_PER_INSTRUMENT: u32 = 500;

/// Maximum number of open orders total
pub const MAX_OPEN_ORDERS_TOTAL: u32 = 2000;

// =============================================================================
// PRECISION
// =============================================================================

/// Price precision for BTC instruments (8 decimal places)
pub const PRICE_PRECISION_BTC: u8 = 8;

/// Price precision for ETH instruments (4 decimal places)
pub const PRICE_PRECISION_ETH: u8 = 4;

/// Price precision for SOL instruments (4 decimal places)
pub const PRICE_PRECISION_SOL: u8 = 4;

/// Amount precision for BTC (4 decimal places)
pub const AMOUNT_PRECISION_BTC: u8 = 4;

/// Amount precision for ETH (3 decimal places)
pub const AMOUNT_PRECISION_ETH: u8 = 3;

/// Amount precision for SOL (1 decimal place)
pub const AMOUNT_PRECISION_SOL: u8 = 1;

// =============================================================================
// JSON-RPC
// =============================================================================

/// JSON-RPC version
pub const JSONRPC_VERSION: &str = "2.0";

/// Default JSON-RPC request ID
pub const DEFAULT_REQUEST_ID: u64 = 1;

// =============================================================================
// WEBSOCKET CHANNELS
// =============================================================================

/// Book channel prefix
pub const CHANNEL_BOOK: &str = "book";

/// Trades channel prefix
pub const CHANNEL_TRADES: &str = "trades";

/// Ticker channel prefix
pub const CHANNEL_TICKER: &str = "ticker";

/// Quote channel prefix
pub const CHANNEL_QUOTE: &str = "quote";

/// User orders channel
pub const CHANNEL_USER_ORDERS: &str = "user.orders";

/// User trades channel
pub const CHANNEL_USER_TRADES: &str = "user.trades";

/// User portfolio channel
pub const CHANNEL_USER_PORTFOLIO: &str = "user.portfolio";

// =============================================================================
// FIX PROTOCOL
// =============================================================================

/// FIX version 4.4
pub const FIX_VERSION: &str = "FIX.4.4";

/// FIX message delimiter (SOH - Start of Header)
pub const FIX_DELIMITER: char = '\x01';

/// FIX message delimiter as string
pub const FIX_DELIMITER_STR: &str = "\x01";

/// Default FIX heartbeat interval in seconds
pub const FIX_HEARTBEAT_INTERVAL: u32 = 30;

// =============================================================================
// ERROR HANDLING
// =============================================================================

/// Maximum retry attempts for failed requests
pub const MAX_RETRY_ATTEMPTS: u8 = 3;

/// Base delay for exponential backoff in milliseconds
pub const RETRY_BASE_DELAY_MS: u64 = 1000;

/// Maximum delay for exponential backoff in milliseconds
pub const RETRY_MAX_DELAY_MS: u64 = 30000;

// =============================================================================
// MARKET DATA
// =============================================================================

/// Maximum depth levels for order book
pub const MAX_ORDER_BOOK_DEPTH: u32 = 10000;

/// Default order book depth
pub const DEFAULT_ORDER_BOOK_DEPTH: u32 = 20;

/// Maximum number of recent trades to fetch
pub const MAX_RECENT_TRADES: u32 = 10000;

/// Default number of recent trades
pub const DEFAULT_RECENT_TRADES: u32 = 100;

// =============================================================================
// AUTHENTICATION
// =============================================================================

/// Access token expiration time in seconds (8 hours)
pub const ACCESS_TOKEN_EXPIRATION_SEC: u64 = 28800;

/// Refresh token expiration time in seconds (30 days)
pub const REFRESH_TOKEN_EXPIRATION_SEC: u64 = 2592000;

/// Minimum time before token expiration to refresh (5 minutes)
pub const TOKEN_REFRESH_BUFFER_SEC: u64 = 300;

// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================

/// Get minimum order amount for a given currency
pub fn get_min_order_amount(currency: &str) -> f64 {
    match currency {
        CURRENCY_BTC => MIN_ORDER_AMOUNT_BTC,
        CURRENCY_ETH => MIN_ORDER_AMOUNT_ETH,
        CURRENCY_SOL => MIN_ORDER_AMOUNT_SOL,
        _ => MIN_ORDER_AMOUNT_BTC, // Default to BTC
    }
}

/// Get price precision for a given currency
pub fn get_price_precision(currency: &str) -> u8 {
    match currency {
        CURRENCY_BTC => PRICE_PRECISION_BTC,
        CURRENCY_ETH => PRICE_PRECISION_ETH,
        CURRENCY_SOL => PRICE_PRECISION_SOL,
        _ => PRICE_PRECISION_BTC, // Default to BTC
    }
}

/// Get amount precision for a given currency
pub fn get_amount_precision(currency: &str) -> u8 {
    match currency {
        CURRENCY_BTC => AMOUNT_PRECISION_BTC,
        CURRENCY_ETH => AMOUNT_PRECISION_ETH,
        CURRENCY_SOL => AMOUNT_PRECISION_SOL,
        _ => AMOUNT_PRECISION_BTC, // Default to BTC
    }
}

/// Check if a currency is supported
pub fn is_supported_currency(currency: &str) -> bool {
    matches!(
        currency,
        CURRENCY_BTC | CURRENCY_ETH | CURRENCY_SOL | CURRENCY_USDC | CURRENCY_USDT | CURRENCY_EURR
    )
}

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

    #[test]
    fn test_api_urls() {
        assert!(DERIBIT_WS_URL_PROD.starts_with("wss://"));
        assert!(DERIBIT_WS_URL_TEST.starts_with("wss://"));
        assert!(DERIBIT_HTTP_URL_PROD.starts_with("https://"));
        assert!(DERIBIT_HTTP_URL_TEST.starts_with("https://"));

        assert!(DERIBIT_WS_URL_PROD.contains("www.deribit.com"));
        assert!(DERIBIT_WS_URL_TEST.contains("test.deribit.com"));
    }

    #[test]
    fn test_supported_currencies() {
        assert!(is_supported_currency(CURRENCY_BTC));
        assert!(is_supported_currency(CURRENCY_ETH));
        assert!(is_supported_currency(CURRENCY_SOL));
        assert!(is_supported_currency(CURRENCY_USDC));
        assert!(is_supported_currency(CURRENCY_USDT));
        assert!(is_supported_currency(CURRENCY_EURR));

        assert!(!is_supported_currency("INVALID"));
        assert!(!is_supported_currency("XRP"));
    }

    #[test]
    fn test_min_order_amounts() {
        assert_eq!(get_min_order_amount(CURRENCY_BTC), MIN_ORDER_AMOUNT_BTC);
        assert_eq!(get_min_order_amount(CURRENCY_ETH), MIN_ORDER_AMOUNT_ETH);
        assert_eq!(get_min_order_amount(CURRENCY_SOL), MIN_ORDER_AMOUNT_SOL);

        // Test default fallback
        assert_eq!(get_min_order_amount("UNKNOWN"), MIN_ORDER_AMOUNT_BTC);
    }

    #[test]
    fn test_precision_functions() {
        assert_eq!(get_price_precision(CURRENCY_BTC), PRICE_PRECISION_BTC);
        assert_eq!(get_price_precision(CURRENCY_ETH), PRICE_PRECISION_ETH);
        assert_eq!(get_price_precision(CURRENCY_SOL), PRICE_PRECISION_SOL);

        assert_eq!(get_amount_precision(CURRENCY_BTC), AMOUNT_PRECISION_BTC);
        assert_eq!(get_amount_precision(CURRENCY_ETH), AMOUNT_PRECISION_ETH);
        assert_eq!(get_amount_precision(CURRENCY_SOL), AMOUNT_PRECISION_SOL);
    }

    #[test]
    fn test_rate_limits() {
        // Validate rate limit relationships - these are important business logic constraints
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(MAX_REQUESTS_PER_SECOND_AUTH > MAX_REQUESTS_PER_SECOND_UNAUTH);
            assert!(MAX_SUBSCRIPTIONS_PER_CONNECTION > 0);
            assert!(MAX_MESSAGE_SIZE_BYTES > 0);
        }

        // Runtime validation of actual values
        assert_eq!(MAX_REQUESTS_PER_SECOND_AUTH, 20);
        assert_eq!(MAX_REQUESTS_PER_SECOND_UNAUTH, 10);
        assert_eq!(MAX_SUBSCRIPTIONS_PER_CONNECTION, 200);
        assert_eq!(MAX_MESSAGE_SIZE_BYTES, 65536);
    }

    #[test]
    fn test_timeouts() {
        // Validate timeout relationships - these are important for connection stability
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(DEFAULT_CONNECTION_TIMEOUT_MS > 0);
            assert!(DEFAULT_REQUEST_TIMEOUT_MS > DEFAULT_CONNECTION_TIMEOUT_MS);
            assert!(HEARTBEAT_INTERVAL_MS > HEARTBEAT_TIMEOUT_MS);
        }

        // Runtime validation of actual values
        assert_eq!(DEFAULT_CONNECTION_TIMEOUT_MS, 5000);
        assert_eq!(DEFAULT_REQUEST_TIMEOUT_MS, 10000);
        assert_eq!(HEARTBEAT_INTERVAL_MS, 10000);
        assert_eq!(HEARTBEAT_TIMEOUT_MS, 5000);
    }

    #[test]
    fn test_order_limits() {
        // Validate order limit relationships - these are important trading constraints
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(MIN_ORDER_AMOUNT_BTC > 0.0);
            assert!(MIN_ORDER_AMOUNT_ETH > 0.0);
            assert!(MIN_ORDER_AMOUNT_SOL > 0.0);
            assert!(MAX_ORDER_AMOUNT > MIN_ORDER_AMOUNT_BTC);
            assert!(MAX_OPEN_ORDERS_TOTAL > MAX_OPEN_ORDERS_PER_INSTRUMENT);
        }

        // Runtime validation using helper functions
        assert_eq!(get_min_order_amount(CURRENCY_BTC), MIN_ORDER_AMOUNT_BTC);
        assert_eq!(get_min_order_amount(CURRENCY_ETH), MIN_ORDER_AMOUNT_ETH);
        assert_eq!(get_min_order_amount(CURRENCY_SOL), MIN_ORDER_AMOUNT_SOL);
    }

    #[test]
    fn test_jsonrpc_constants() {
        assert_eq!(JSONRPC_VERSION, "2.0");

        // Validate request ID is positive - important for protocol compliance
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(DEFAULT_REQUEST_ID > 0);
        }

        // Runtime validation
        assert_eq!(DEFAULT_REQUEST_ID, 1);
    }

    #[test]
    fn test_fix_constants() {
        assert_eq!(FIX_VERSION, "FIX.4.4");
        assert_eq!(FIX_DELIMITER, '\x01');
        assert_eq!(FIX_DELIMITER_STR, "\x01");

        // Validate heartbeat interval is positive - important for FIX protocol
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(FIX_HEARTBEAT_INTERVAL > 0);
        }

        // Runtime validation
        assert_eq!(FIX_HEARTBEAT_INTERVAL, 30);
    }

    #[test]
    fn test_channel_names() {
        // Validate channel names are not empty - important for WebSocket subscriptions
        #[allow(clippy::const_is_empty)]
        {
            assert!(!CHANNEL_BOOK.is_empty());
            assert!(!CHANNEL_TRADES.is_empty());
            assert!(!CHANNEL_TICKER.is_empty());
            assert!(!CHANNEL_QUOTE.is_empty());
        }

        // Runtime validation of actual channel names
        assert_eq!(CHANNEL_BOOK, "book");
        assert_eq!(CHANNEL_TRADES, "trades");
        assert_eq!(CHANNEL_TICKER, "ticker");
        assert_eq!(CHANNEL_QUOTE, "quote");

        // Validate user channel prefixes
        assert!(CHANNEL_USER_ORDERS.starts_with("user."));
        assert!(CHANNEL_USER_TRADES.starts_with("user."));
        assert!(CHANNEL_USER_PORTFOLIO.starts_with("user."));
    }

    #[test]
    fn test_instrument_types() {
        let types = [
            INSTRUMENT_TYPE_FUTURE,
            INSTRUMENT_TYPE_OPTION,
            INSTRUMENT_TYPE_PERPETUAL,
            INSTRUMENT_TYPE_SPOT,
            INSTRUMENT_TYPE_FUTURE_COMBO,
            INSTRUMENT_TYPE_OPTION_COMBO,
        ];

        for instrument_type in types {
            assert!(!instrument_type.is_empty());
        }
    }

    #[test]
    fn test_authentication_constants() {
        // Validate token expiration relationships - critical for authentication security
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(ACCESS_TOKEN_EXPIRATION_SEC > 0);
            assert!(REFRESH_TOKEN_EXPIRATION_SEC > ACCESS_TOKEN_EXPIRATION_SEC);
            assert!(TOKEN_REFRESH_BUFFER_SEC < ACCESS_TOKEN_EXPIRATION_SEC);
        }

        // Runtime validation of actual values
        assert_eq!(ACCESS_TOKEN_EXPIRATION_SEC, 28800); // 8 hours
        assert_eq!(REFRESH_TOKEN_EXPIRATION_SEC, 2592000); // 30 days
        assert_eq!(TOKEN_REFRESH_BUFFER_SEC, 300); // 5 minutes
    }

    #[test]
    fn test_market_data_constants() {
        // Validate market data limit relationships - important for performance
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(MAX_ORDER_BOOK_DEPTH > DEFAULT_ORDER_BOOK_DEPTH);
            assert!(MAX_RECENT_TRADES > DEFAULT_RECENT_TRADES);
            assert!(DEFAULT_ORDER_BOOK_DEPTH > 0);
            assert!(DEFAULT_RECENT_TRADES > 0);
        }

        // Runtime validation of actual values
        assert_eq!(MAX_ORDER_BOOK_DEPTH, 10000);
        assert_eq!(DEFAULT_ORDER_BOOK_DEPTH, 20);
        assert_eq!(MAX_RECENT_TRADES, 10000);
        assert_eq!(DEFAULT_RECENT_TRADES, 100);
    }

    #[test]
    fn test_error_handling_constants() {
        // Validate retry logic relationships - important for resilience
        #[allow(clippy::assertions_on_constants)]
        {
            assert!(MAX_RETRY_ATTEMPTS > 0);
            assert!(RETRY_BASE_DELAY_MS > 0);
            assert!(RETRY_MAX_DELAY_MS > RETRY_BASE_DELAY_MS);
        }

        // Runtime validation of actual values
        assert_eq!(MAX_RETRY_ATTEMPTS, 3);
        assert_eq!(RETRY_BASE_DELAY_MS, 1000);
        assert_eq!(RETRY_MAX_DELAY_MS, 30000);
    }
}