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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 21/7/25
******************************************************************************/
use crate::model::order::OrderInfo;
use crate::model::order_management::QuoteResult;
use crate::model::trade::{LastTrade, TradeExecution};
use pretty_simple_display::{DebugPretty, DisplaySimple};

use serde::{Deserialize, Serialize};

/// Generic JSON-RPC 2.0 response wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse<T> {
    /// JSON-RPC version
    pub jsonrpc: String,
    /// Request ID
    pub id: Option<serde_json::Value>,
    /// Result data (present on success)
    pub result: Option<T>,
    /// Error information (present on error)
    pub error: Option<JsonRpcError>,
    /// Test net flag
    pub testnet: Option<bool>,
    /// Use server time
    #[serde(rename = "usIn")]
    pub us_in: Option<i64>,
    /// Use out time
    #[serde(rename = "usOut")]
    pub us_out: Option<i64>,
    /// Use diff time
    #[serde(rename = "usDiff")]
    pub us_diff: Option<i64>,
}

impl<T> JsonRpcResponse<T> {
    /// Create a successful response
    pub fn success(id: Option<serde_json::Value>, result: T) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            result: Some(result),
            error: None,
            testnet: None,
            us_in: None,
            us_out: None,
            us_diff: None,
        }
    }

    /// Create an error response
    pub fn error(id: Option<serde_json::Value>, error: JsonRpcError) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            id,
            result: None,
            error: Some(error),
            testnet: None,
            us_in: None,
            us_out: None,
            us_diff: None,
        }
    }

    /// Check if the response is successful
    pub fn is_success(&self) -> bool {
        self.error.is_none() && self.result.is_some()
    }

    /// Check if the response is an error
    pub fn is_error(&self) -> bool {
        self.error.is_some()
    }

    /// Get the result, consuming the response
    pub fn into_result(self) -> Result<T, JsonRpcError> {
        match (self.result, self.error) {
            (Some(result), None) => Ok(result),
            (None, Some(error)) => Err(error),
            (Some(_), Some(error)) => Err(error), // Error takes precedence
            (None, None) => Err(JsonRpcError {
                code: -32603,
                message: "Internal error: neither result nor error present".to_string(),
                data: None,
            }),
        }
    }
}

/// JSON-RPC error information
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct JsonRpcError {
    /// Error code
    pub code: i32,
    /// Error message
    pub message: String,
    /// Additional error data
    pub data: Option<serde_json::Value>,
}

impl JsonRpcError {
    /// Create a new JSON-RPC error
    pub fn new(code: i32, message: String) -> Self {
        Self {
            code,
            message,
            data: None,
        }
    }

    /// Create an error with additional data
    pub fn with_data(code: i32, message: String, data: serde_json::Value) -> Self {
        Self {
            code,
            message,
            data: Some(data),
        }
    }

    /// Parse error (-32700)
    pub fn parse_error() -> Self {
        Self::new(-32700, "Parse error".to_string())
    }

    /// Invalid request (-32600)
    pub fn invalid_request() -> Self {
        Self::new(-32600, "Invalid Request".to_string())
    }

    /// Method not found (-32601)
    pub fn method_not_found() -> Self {
        Self::new(-32601, "Method not found".to_string())
    }

    /// Invalid params (-32602)
    pub fn invalid_params() -> Self {
        Self::new(-32602, "Invalid params".to_string())
    }

    /// Internal error (-32603)
    pub fn internal_error() -> Self {
        Self::new(-32603, "Internal error".to_string())
    }

    /// Check if this is a server error (code between -32099 and -32000)
    pub fn is_server_error(&self) -> bool {
        self.code <= -32000 && self.code >= -32099
    }

    /// Check if this is an application error (code > -32000)
    pub fn is_application_error(&self) -> bool {
        self.code > -32000
    }
}

/// Authentication response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AuthResponse {
    /// Access token
    pub access_token: String,
    /// Token type (usually "bearer")
    pub token_type: String,
    /// Expires in seconds
    pub expires_in: i64,
    /// Refresh token
    pub refresh_token: String,
    /// Scope
    pub scope: String,
}

/// Pagination information
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, PartialEq)]
pub struct Pagination {
    /// Current page
    pub page: Option<u32>,
    /// Items per page
    pub per_page: Option<u32>,
    /// Total items
    pub total: Option<u64>,
    /// Total pages
    pub pages: Option<u32>,
    /// Has more pages
    pub has_more: Option<bool>,
}

/// Generic paginated response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginatedResponse<T> {
    /// Data items
    pub data: Vec<T>,
    /// Pagination information
    pub pagination: Option<Pagination>,
}

impl<T> PaginatedResponse<T> {
    /// Create a new paginated response
    pub fn new(data: Vec<T>) -> Self {
        Self {
            data,
            pagination: None,
        }
    }

    /// Create a paginated response with pagination info
    pub fn with_pagination(data: Vec<T>, pagination: Pagination) -> Self {
        Self {
            data,
            pagination: Some(pagination),
        }
    }

    /// Check if there are more pages
    pub fn has_more(&self) -> bool {
        self.pagination
            .as_ref()
            .and_then(|p| p.has_more)
            .unwrap_or(false)
    }

    /// Get the number of items
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Check if the response is empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
}

/// WebSocket notification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Notification<T> {
    /// JSON-RPC version
    pub jsonrpc: String,
    /// Method name
    pub method: String,
    /// Parameters/data
    pub params: T,
}

impl<T> Notification<T> {
    /// Create a new notification
    pub fn new(method: String, params: T) -> Self {
        Self {
            jsonrpc: "2.0".to_string(),
            method,
            params,
        }
    }
}

/// Subscription response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct SubscriptionResponse {
    /// Subscription ID
    pub subscription: String,
    /// Channel name
    pub channel: String,
}

/// Heartbeat response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct HeartbeatResponse {
    /// Type (always "heartbeat")
    #[serde(rename = "type")]
    pub type_: String,
}

/// Test response for connectivity checks
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct TestResponse {
    /// Version information
    pub version: String,
}

/// Server time response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ServerTimeResponse {
    /// Current server timestamp in milliseconds
    pub timestamp: i64,
}

/// Settlements response structure
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct SettlementsResponse {
    /// Continuation token for pagination
    pub continuation: Option<String>,
    /// List of settlement events
    pub settlements: Vec<crate::model::settlement::Settlement>,
}

impl SettlementsResponse {
    /// Create a new settlements response
    pub fn new(settlements: Vec<crate::model::settlement::Settlement>) -> Self {
        Self {
            continuation: None,
            settlements,
        }
    }

    /// Create settlements response with continuation token
    pub fn with_continuation(
        settlements: Vec<crate::model::settlement::Settlement>,
        continuation: String,
    ) -> Self {
        Self {
            continuation: Some(continuation),
            settlements,
        }
    }

    /// Check if there are more results
    pub fn has_more(&self) -> bool {
        self.continuation.is_some()
    }
}

/// Contract size response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ContractSizeResponse {
    /// Contract size value
    pub contract_size: f64,
}

/// Status response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct StatusResponse {
    /// Whether the system is locked (optional)
    pub locked: Option<bool>,
    /// Status message (optional)
    pub message: Option<String>,
    /// List of locked indices (optional)
    pub locked_indices: Option<Vec<String>>,
    /// Additional fields that might be present in the API response
    #[serde(flatten)]
    pub additional_fields: std::collections::HashMap<String, serde_json::Value>,
}

/// APR history response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AprHistoryResponse {
    /// List of APR data points
    pub data: Vec<AprDataPoint>,
    /// Continuation token for pagination
    pub continuation: Option<String>,
}
/// APR data point
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AprDataPoint {
    /// Annual percentage rate
    pub apr: f64,
    /// Timestamp of the data point (optional)
    pub timestamp: Option<u64>,
    /// Day of the data point
    pub day: i32,
}

/// Hello response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct HelloResponse {
    /// Version string
    pub version: String,
}

/// Delivery prices response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct DeliveryPricesResponse {
    /// List of delivery price data
    pub data: Vec<DeliveryPriceData>,
    /// Total number of records
    pub records_total: u32,
}

/// Delivery price data
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct DeliveryPriceData {
    /// Date of the delivery price
    pub date: String,
    /// Delivery price value
    pub delivery_price: f64,
}

/// Currency-specific expirations
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct CurrencyExpirations {
    /// Future instrument expirations
    pub future: Option<Vec<String>>,
    /// Option instrument expirations
    pub option: Option<Vec<String>>,
}

/// Expirations response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct ExpirationsResponse {
    /// Direct future expirations (when currency="any")
    pub future: Option<Vec<String>>,
    /// Direct option expirations (when currency="any")
    pub option: Option<Vec<String>>,
    /// Map of currency to their expirations (when specific currency)
    #[serde(flatten)]
    pub currencies: std::collections::HashMap<String, CurrencyExpirations>,
}

/// Last trades response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct LastTradesResponse {
    /// Whether there are more trades available
    pub has_more: bool,
    /// List of recent trades
    pub trades: Vec<LastTrade>,
}

/// Order response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct OrderResponse {
    /// Order information
    pub order: OrderInfo,
    /// List of trade executions for the order
    pub trades: Vec<TradeExecution>,
}

/// Mass quote response
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct MassQuoteResponse {
    /// List of quote results
    pub quotes: Vec<QuoteResult>,
}

impl std::error::Error for JsonRpcError {}

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

    #[test]
    fn test_json_rpc_response_success() {
        let response = JsonRpcResponse::success(Some(json!(1)), "test_result".to_string());
        assert_eq!(response.jsonrpc, "2.0");
        assert_eq!(response.id, Some(json!(1)));
        assert_eq!(response.result, Some("test_result".to_string()));
        assert_eq!(response.error, None);
        assert!(response.is_success());
        assert!(!response.is_error());
    }

    #[test]
    fn test_json_rpc_response_error() {
        let error = JsonRpcError::new(-32600, "Invalid Request".to_string());
        let response: JsonRpcResponse<String> =
            JsonRpcResponse::error(Some(json!(1)), error.clone());

        assert_eq!(response.jsonrpc, "2.0");
        assert_eq!(response.id, Some(json!(1)));
        assert_eq!(response.result, None);
        assert!(response.error.is_some());
        assert!(!response.is_success());
        assert!(response.is_error());
    }

    #[test]
    fn test_json_rpc_response_into_result_success() {
        let response = JsonRpcResponse::success(Some(json!(1)), "test_result".to_string());
        let result = response.into_result().unwrap();
        assert_eq!(result, "test_result");
    }

    #[test]
    fn test_json_rpc_response_into_result_error() {
        let error = JsonRpcError::new(-32600, "Invalid Request".to_string());
        let response: JsonRpcResponse<String> =
            JsonRpcResponse::error(Some(json!(1)), error.clone());
        let result = response.into_result();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code, -32600);
        assert_eq!(err.message, "Invalid Request");
    }

    #[test]
    fn test_json_rpc_response_into_result_neither() {
        let response: JsonRpcResponse<String> = JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id: Some(json!(1)),
            result: None,
            error: None,
            testnet: None,
            us_in: None,
            us_out: None,
            us_diff: None,
        };
        let result = response.into_result();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.code, -32603);
        assert!(err.message.contains("Internal error"));
    }

    #[test]
    fn test_json_rpc_error_new() {
        let error = JsonRpcError::new(-32600, "Invalid Request".to_string());
        assert_eq!(error.code, -32600);
        assert_eq!(error.message, "Invalid Request");
        assert_eq!(error.data, None);
    }

    #[test]
    fn test_json_rpc_error_with_data() {
        let data = json!({"details": "Additional error information"});
        let error = JsonRpcError::with_data(-32602, "Invalid params".to_string(), data.clone());
        assert_eq!(error.code, -32602);
        assert_eq!(error.message, "Invalid params");
        assert_eq!(error.data, Some(data));
    }

    #[test]
    fn test_json_rpc_error_standard_errors() {
        let parse_error = JsonRpcError::parse_error();
        assert_eq!(parse_error.code, -32700);
        assert_eq!(parse_error.message, "Parse error");

        let invalid_request = JsonRpcError::invalid_request();
        assert_eq!(invalid_request.code, -32600);
        assert_eq!(invalid_request.message, "Invalid Request");

        let method_not_found = JsonRpcError::method_not_found();
        assert_eq!(method_not_found.code, -32601);
        assert_eq!(method_not_found.message, "Method not found");

        let invalid_params = JsonRpcError::invalid_params();
        assert_eq!(invalid_params.code, -32602);
        assert_eq!(invalid_params.message, "Invalid params");

        let internal_error = JsonRpcError::internal_error();
        assert_eq!(internal_error.code, -32603);
        assert_eq!(internal_error.message, "Internal error");
    }

    #[test]
    fn test_json_rpc_error_is_server_error() {
        let server_error = JsonRpcError::new(-32001, "Server error".to_string());
        assert!(server_error.is_server_error());
        assert!(!server_error.is_application_error());

        let app_error = JsonRpcError::new(-31999, "Application error".to_string());
        assert!(!app_error.is_server_error());
        assert!(app_error.is_application_error());
    }

    #[test]
    fn test_json_rpc_error_display() {
        let error = JsonRpcError::new(-32600, "Invalid Request".to_string());
        let display_str = format!("{}", error);
        // The display implementation uses JSON formatting from the macro
        assert!(display_str.contains("-32600"));
        assert!(display_str.contains("Invalid Request"));
    }

    #[test]
    fn test_auth_response() {
        let auth_response = AuthResponse {
            access_token: "access_token_123".to_string(),
            token_type: "bearer".to_string(),
            expires_in: 3600,
            refresh_token: "refresh_token_456".to_string(),
            scope: "read write".to_string(),
        };

        assert_eq!(auth_response.access_token, "access_token_123");
        assert_eq!(auth_response.token_type, "bearer");
        assert_eq!(auth_response.expires_in, 3600);
        assert_eq!(auth_response.refresh_token, "refresh_token_456");
        assert_eq!(auth_response.scope, "read write");
    }

    #[test]
    fn test_pagination() {
        let pagination = Pagination {
            page: Some(1),
            per_page: Some(50),
            total: Some(1000),
            pages: Some(20),
            has_more: Some(true),
        };

        assert_eq!(pagination.page, Some(1));
        assert_eq!(pagination.per_page, Some(50));
        assert_eq!(pagination.total, Some(1000));
        assert_eq!(pagination.pages, Some(20));
        assert_eq!(pagination.has_more, Some(true));
    }

    #[test]
    fn test_paginated_response_new() {
        let data = vec!["item1".to_string(), "item2".to_string()];
        let response = PaginatedResponse::new(data.clone());

        assert_eq!(response.data, data);
        assert_eq!(response.pagination, None);
        assert_eq!(response.len(), 2);
        assert!(!response.is_empty());
        assert!(!response.has_more());
    }

    #[test]
    fn test_paginated_response_with_pagination() {
        let data = vec!["item1".to_string(), "item2".to_string()];
        let pagination = Pagination {
            page: Some(1),
            per_page: Some(2),
            total: Some(10),
            pages: Some(5),
            has_more: Some(true),
        };
        let response = PaginatedResponse::with_pagination(data.clone(), pagination);

        assert_eq!(response.data, data);
        assert!(response.pagination.is_some());
        assert_eq!(response.len(), 2);
        assert!(!response.is_empty());
        assert!(response.has_more());
    }

    #[test]
    fn test_paginated_response_empty() {
        let response: PaginatedResponse<String> = PaginatedResponse::new(vec![]);
        assert_eq!(response.len(), 0);
        assert!(response.is_empty());
        assert!(!response.has_more());
    }

    #[test]
    fn test_notification() {
        let notification = Notification::new("ticker".to_string(), "BTC-PERPETUAL".to_string());
        assert_eq!(notification.jsonrpc, "2.0");
        assert_eq!(notification.method, "ticker");
        assert_eq!(notification.params, "BTC-PERPETUAL");
    }

    #[test]
    fn test_subscription_response() {
        let subscription = SubscriptionResponse {
            subscription: "sub_123".to_string(),
            channel: "ticker.BTC-PERPETUAL".to_string(),
        };
        assert_eq!(subscription.subscription, "sub_123");
        assert_eq!(subscription.channel, "ticker.BTC-PERPETUAL");
    }

    #[test]
    fn test_heartbeat_response() {
        let heartbeat = HeartbeatResponse {
            type_: "heartbeat".to_string(),
        };
        assert_eq!(heartbeat.type_, "heartbeat");
    }

    #[test]
    fn test_test_response() {
        let test_response = TestResponse {
            version: "1.2.3".to_string(),
        };
        assert_eq!(test_response.version, "1.2.3");
    }

    #[test]
    fn test_server_time_response() {
        let server_time = ServerTimeResponse {
            timestamp: 1640995200000,
        };
        assert_eq!(server_time.timestamp, 1640995200000);
    }

    #[test]
    fn test_settlements_response_new() {
        let settlements = vec![];
        let response = SettlementsResponse::new(settlements);
        assert_eq!(response.continuation, None);
        assert!(response.settlements.is_empty());
        assert!(!response.has_more());
    }

    #[test]
    fn test_settlements_response_with_continuation() {
        let settlements = vec![];
        let response = SettlementsResponse::with_continuation(settlements, "token_123".to_string());
        assert_eq!(response.continuation, Some("token_123".to_string()));
        assert!(response.has_more());
    }

    #[test]
    fn test_contract_size_response() {
        let contract_size = ContractSizeResponse { contract_size: 1.0 };
        assert_eq!(contract_size.contract_size, 1.0);
    }

    #[test]
    fn test_status_response() {
        let mut additional_fields = std::collections::HashMap::new();
        additional_fields.insert("custom_field".to_string(), json!("custom_value"));

        let status = StatusResponse {
            locked: Some(false),
            message: Some("System operational".to_string()),
            locked_indices: Some(vec!["BTC".to_string(), "ETH".to_string()]),
            additional_fields,
        };

        assert_eq!(status.locked, Some(false));
        assert_eq!(status.message, Some("System operational".to_string()));
        assert_eq!(
            status.locked_indices,
            Some(vec!["BTC".to_string(), "ETH".to_string()])
        );
        assert_eq!(
            status.additional_fields.get("custom_field"),
            Some(&json!("custom_value"))
        );
    }

    #[test]
    fn test_apr_data_point() {
        let apr_point = AprDataPoint {
            apr: 5.25,
            timestamp: Some(1640995200000),
            day: 365,
        };
        assert_eq!(apr_point.apr, 5.25);
        assert_eq!(apr_point.timestamp, Some(1640995200000));
        assert_eq!(apr_point.day, 365);
    }

    #[test]
    fn test_apr_history_response() {
        let data_points = vec![AprDataPoint {
            apr: 5.25,
            timestamp: Some(1640995200000),
            day: 365,
        }];
        let apr_history = AprHistoryResponse {
            data: data_points,
            continuation: Some("token_456".to_string()),
        };
        assert_eq!(apr_history.data.len(), 1);
        assert_eq!(apr_history.continuation, Some("token_456".to_string()));
    }

    #[test]
    fn test_hello_response() {
        let hello = HelloResponse {
            version: "2.1.1".to_string(),
        };
        assert_eq!(hello.version, "2.1.1");
    }

    #[test]
    fn test_delivery_price_data() {
        let delivery_price = DeliveryPriceData {
            date: "2024-01-01".to_string(),
            delivery_price: 50000.0,
        };
        assert_eq!(delivery_price.date, "2024-01-01");
        assert_eq!(delivery_price.delivery_price, 50000.0);
    }

    #[test]
    fn test_delivery_prices_response() {
        let data = vec![DeliveryPriceData {
            date: "2024-01-01".to_string(),
            delivery_price: 50000.0,
        }];
        let delivery_prices = DeliveryPricesResponse {
            data,
            records_total: 1,
        };
        assert_eq!(delivery_prices.data.len(), 1);
        assert_eq!(delivery_prices.records_total, 1);
    }

    #[test]
    fn test_currency_expirations() {
        let expirations = CurrencyExpirations {
            future: Some(vec!["2024-03-29".to_string()]),
            option: Some(vec!["2024-01-26".to_string(), "2024-02-23".to_string()]),
        };
        assert_eq!(expirations.future, Some(vec!["2024-03-29".to_string()]));
        assert_eq!(
            expirations.option,
            Some(vec!["2024-01-26".to_string(), "2024-02-23".to_string()])
        );
    }

    #[test]
    fn test_expirations_response() {
        let mut currencies = std::collections::HashMap::new();
        currencies.insert(
            "BTC".to_string(),
            CurrencyExpirations {
                future: Some(vec!["2024-03-29".to_string()]),
                option: Some(vec!["2024-01-26".to_string()]),
            },
        );

        let expirations = ExpirationsResponse {
            future: Some(vec!["2024-03-29".to_string()]),
            option: Some(vec!["2024-01-26".to_string()]),
            currencies,
        };

        assert_eq!(expirations.future, Some(vec!["2024-03-29".to_string()]));
        assert_eq!(expirations.option, Some(vec!["2024-01-26".to_string()]));
        assert!(expirations.currencies.contains_key("BTC"));
    }

    #[test]
    fn test_last_trades_response() {
        let last_trades = LastTradesResponse {
            has_more: true,
            trades: vec![],
        };
        assert!(last_trades.has_more);
        assert!(last_trades.trades.is_empty());
    }

    #[test]
    fn test_serialization_roundtrip() {
        let response = JsonRpcResponse::success(Some(json!(1)), "test_result".to_string());
        let json = serde_json::to_string(&response).unwrap();
        let deserialized: JsonRpcResponse<String> = serde_json::from_str(&json).unwrap();

        assert_eq!(response.jsonrpc, deserialized.jsonrpc);
        assert_eq!(response.id, deserialized.id);
        assert_eq!(response.result, deserialized.result);
        assert_eq!(response.error.is_none(), deserialized.error.is_none());
    }

    #[test]
    fn test_debug_and_display_implementations() {
        let auth_response = AuthResponse {
            access_token: "token".to_string(),
            token_type: "bearer".to_string(),
            expires_in: 3600,
            refresh_token: "refresh".to_string(),
            scope: "read".to_string(),
        };

        let debug_str = format!("{:?}", auth_response);
        let display_str = format!("{}", auth_response);

        assert!(debug_str.contains("token"));
        assert!(display_str.contains("token"));
    }
}