deribit_base/model/
config.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 21/7/25
5******************************************************************************/
6
7use serde::{Deserialize, Serialize};
8
9/// Deribit API configuration
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct DeribitConfig {
12    /// Client ID for API authentication
13    pub client_id: String,
14    /// Client secret for API authentication
15    pub client_secret: String,
16    /// Whether to use testnet
17    pub test_net: bool,
18    /// Request timeout in seconds
19    pub timeout_seconds: u64,
20    /// Maximum number of retries
21    pub max_retries: u32,
22    /// Rate limit per second
23    pub rate_limit: Option<u32>,
24    /// User agent string
25    pub user_agent: Option<String>,
26}
27
28impl DeribitConfig {
29    /// Create a new configuration for production
30    pub fn new(client_id: String, client_secret: String) -> Self {
31        Self {
32            client_id,
33            client_secret,
34            test_net: false,
35            timeout_seconds: 30,
36            max_retries: 3,
37            rate_limit: None,
38            user_agent: None,
39        }
40    }
41
42    /// Create a new configuration for testnet
43    pub fn testnet(client_id: String, client_secret: String) -> Self {
44        Self {
45            client_id,
46            client_secret,
47            test_net: true,
48            timeout_seconds: 30,
49            max_retries: 3,
50            rate_limit: None,
51            user_agent: None,
52        }
53    }
54
55    /// Set timeout in seconds
56    pub fn with_timeout(mut self, timeout_seconds: u64) -> Self {
57        self.timeout_seconds = timeout_seconds;
58        self
59    }
60
61    /// Set maximum retries
62    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
63        self.max_retries = max_retries;
64        self
65    }
66
67    /// Set rate limit
68    pub fn with_rate_limit(mut self, rate_limit: u32) -> Self {
69        self.rate_limit = Some(rate_limit);
70        self
71    }
72
73    /// Set user agent
74    pub fn with_user_agent(mut self, user_agent: String) -> Self {
75        self.user_agent = Some(user_agent);
76        self
77    }
78
79    /// Get the base URL for HTTP API
80    pub fn base_url(&self) -> &'static str {
81        if self.test_net {
82            DeribitUrls::TEST_BASE_URL
83        } else {
84            DeribitUrls::PROD_BASE_URL
85        }
86    }
87
88    /// Get the WebSocket URL
89    pub fn ws_url(&self) -> &'static str {
90        if self.test_net {
91            DeribitUrls::TEST_WS_URL
92        } else {
93            DeribitUrls::PROD_WS_URL
94        }
95    }
96
97    /// Get the API URL for HTTP requests
98    pub fn api_url(&self) -> String {
99        format!("{}/api/v2", self.base_url())
100    }
101}
102
103impl Default for DeribitConfig {
104    fn default() -> Self {
105        Self {
106            client_id: String::new(),
107            client_secret: String::new(),
108            test_net: true, // Default to testnet for safety
109            timeout_seconds: 30,
110            max_retries: 3,
111            rate_limit: None,
112            user_agent: Some("deribit-rust-client/1.0".to_string()),
113        }
114    }
115}
116
117/// Deribit API URLs
118pub struct DeribitUrls;
119
120impl DeribitUrls {
121    /// Production base URL
122    pub const PROD_BASE_URL: &'static str = "https://www.deribit.com";
123    /// Test base URL
124    pub const TEST_BASE_URL: &'static str = "https://test.deribit.com";
125    /// Production WebSocket URL
126    pub const PROD_WS_URL: &'static str = "wss://www.deribit.com/ws/api/v2";
127    /// Test WebSocket URL
128    pub const TEST_WS_URL: &'static str = "wss://test.deribit.com/ws/api/v2";
129}
130
131/// Connection configuration for WebSocket
132#[derive(Debug, Clone, Serialize, Deserialize)]
133pub struct WebSocketConfig {
134    /// Base configuration
135    pub base: DeribitConfig,
136    /// Ping interval in seconds
137    pub ping_interval: u64,
138    /// Pong timeout in seconds
139    pub pong_timeout: u64,
140    /// Reconnect attempts
141    pub reconnect_attempts: u32,
142    /// Reconnect delay in seconds
143    pub reconnect_delay: u64,
144    /// Maximum message size
145    pub max_message_size: usize,
146    /// Enable compression
147    pub compression: bool,
148}
149
150impl WebSocketConfig {
151    /// Create new WebSocket configuration
152    pub fn new(base: DeribitConfig) -> Self {
153        Self {
154            base,
155            ping_interval: 30,
156            pong_timeout: 10,
157            reconnect_attempts: 5,
158            reconnect_delay: 5,
159            max_message_size: 1024 * 1024, // 1MB
160            compression: true,
161        }
162    }
163
164    /// Set ping interval
165    pub fn with_ping_interval(mut self, ping_interval: u64) -> Self {
166        self.ping_interval = ping_interval;
167        self
168    }
169
170    /// Set pong timeout
171    pub fn with_pong_timeout(mut self, pong_timeout: u64) -> Self {
172        self.pong_timeout = pong_timeout;
173        self
174    }
175
176    /// Set reconnect attempts
177    pub fn with_reconnect_attempts(mut self, reconnect_attempts: u32) -> Self {
178        self.reconnect_attempts = reconnect_attempts;
179        self
180    }
181
182    /// Set reconnect delay
183    pub fn with_reconnect_delay(mut self, reconnect_delay: u64) -> Self {
184        self.reconnect_delay = reconnect_delay;
185        self
186    }
187
188    /// Enable/disable compression
189    pub fn with_compression(mut self, compression: bool) -> Self {
190        self.compression = compression;
191        self
192    }
193}
194
195/// HTTP client configuration
196#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct HttpConfig {
198    /// Base configuration
199    pub base: DeribitConfig,
200    /// Connection pool size
201    pub pool_size: Option<usize>,
202    /// Keep alive timeout
203    pub keep_alive: Option<u64>,
204    /// Enable HTTP/2
205    pub http2: bool,
206    /// Enable gzip compression
207    pub gzip: bool,
208}
209
210impl HttpConfig {
211    /// Create new HTTP configuration
212    pub fn new(base: DeribitConfig) -> Self {
213        Self {
214            base,
215            pool_size: None,
216            keep_alive: Some(30),
217            http2: true,
218            gzip: true,
219        }
220    }
221
222    /// Set connection pool size
223    pub fn with_pool_size(mut self, pool_size: usize) -> Self {
224        self.pool_size = Some(pool_size);
225        self
226    }
227
228    /// Set keep alive timeout
229    pub fn with_keep_alive(mut self, keep_alive: u64) -> Self {
230        self.keep_alive = Some(keep_alive);
231        self
232    }
233
234    /// Enable/disable HTTP/2
235    pub fn with_http2(mut self, http2: bool) -> Self {
236        self.http2 = http2;
237        self
238    }
239
240    /// Enable/disable gzip compression
241    pub fn with_gzip(mut self, gzip: bool) -> Self {
242        self.gzip = gzip;
243        self
244    }
245}