Skip to main content

deribit_http/model/
self_trading.rs

1//! Self-trading configuration models for Deribit API
2//!
3//! This module contains types for self-trading prevention configuration.
4
5use serde::{Deserialize, Serialize};
6
7/// Self-trading prevention mode
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum SelfTradingMode {
11    /// Reject the taker order
12    RejectTaker,
13    /// Cancel the maker order
14    CancelMaker,
15}
16
17impl SelfTradingMode {
18    /// Returns the mode as a string for API requests
19    #[must_use]
20    pub fn as_str(&self) -> &'static str {
21        match self {
22            Self::RejectTaker => "reject_taker",
23            Self::CancelMaker => "cancel_maker",
24        }
25    }
26}
27
28impl std::fmt::Display for SelfTradingMode {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "{}", self.as_str())
31    }
32}
33
34/// Self-trading configuration
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct SelfTradingConfig {
37    /// The self-trading prevention mode
38    pub mode: SelfTradingMode,
39    /// Whether the config extends to subaccounts
40    pub extended_to_subaccounts: bool,
41    /// Whether to block RFQ self-match prevention
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub block_rfq_self_match_prevention: Option<bool>,
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_self_trading_mode_serialization() {
52        let mode = SelfTradingMode::CancelMaker;
53        let json = serde_json::to_string(&mode).expect("Failed to serialize");
54        assert_eq!(json, "\"cancel_maker\"");
55    }
56
57    #[test]
58    fn test_self_trading_mode_deserialization() {
59        let json = "\"reject_taker\"";
60        let mode: SelfTradingMode = serde_json::from_str(json).expect("Failed to parse");
61        assert_eq!(mode, SelfTradingMode::RejectTaker);
62    }
63
64    #[test]
65    fn test_self_trading_config_deserialization() {
66        let json = r#"{
67            "mode": "cancel_maker",
68            "extended_to_subaccounts": true,
69            "block_rfq_self_match_prevention": false
70        }"#;
71
72        let config: SelfTradingConfig = serde_json::from_str(json).expect("Failed to parse");
73        assert_eq!(config.mode, SelfTradingMode::CancelMaker);
74        assert!(config.extended_to_subaccounts);
75        assert_eq!(config.block_rfq_self_match_prevention, Some(false));
76    }
77}