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
//! # Deribit WebSocket Client
//!
//! A high-performance, production-ready WebSocket client for the Deribit cryptocurrency derivatives exchange.
//! This crate provides comprehensive real-time market data streaming, trading operations, and account management
//! through Deribit's WebSocket API v2.
//!
//! ## Features
//!
//! - ๐ **WebSocket Connection Management** - Robust connection handling with automatic reconnection and heartbeat
//! - ๐ก **JSON-RPC Protocol** - Complete JSON-RPC 2.0 implementation for Deribit API
//! - ๐ **Real-time Market Data** - Live ticker, order book, trades, and chart data streaming
//! - ๐ **Advanced Subscriptions** - Chart data aggregation and user position change notifications
//! - ๐ฐ **Mass Quote System** - High-performance mass quoting with MMP (Market Maker Protection) groups
//! - ๐ **Authentication** - Secure API key and signature-based authentication with typed responses
//! - ๐ **Trading Operations** - Full order lifecycle: buy, sell, cancel, edit orders
//! - ๐ผ **Account Management** - Position queries, account summaries, order history
//! - ๐ **Session Management** - Heartbeat control, client identification, cancel-on-disconnect
//! - ๐ก๏ธ **Error Handling** - Comprehensive error types with detailed recovery mechanisms
//! - โก **Async/Await** - Full async support with tokio runtime for high concurrency
//! - ๐ **Callback System** - Flexible message processing with primary and error callbacks
//! - ๐ **Subscription Management** - Intelligent subscription tracking and channel management
//! - ๐งช **Testing Support** - Complete test coverage with working examples
//!
//! ## Supported Subscription Channels
//!
//! ### Market Data Channels
//! - `ticker.{instrument}` - Real-time ticker updates
//! - `book.{instrument}.{group}` - Order book snapshots and updates
//! - `trades.{instrument}` - Live trade executions
//! - `chart.trades.{instrument}.{resolution}` - Aggregated chart data for technical analysis
//!
//! ### User Data Channels (Requires Authentication)
//! - `user.orders` - Order status updates and fills
//! - `user.trades` - User trade executions
//! - `user.changes.{instrument}.{interval}` - Position and portfolio changes
//!
//! ## Protocol Support
//!
//! | Feature | Status | Description |
//! |---------|--------|-------------|
//! | JSON-RPC over WebSocket | โ
Full Support | Complete JSON-RPC 2.0 implementation |
//! | Market Data Subscriptions | โ
Full Support | All public channels supported |
//! | User Data Subscriptions | โ
Full Support | Private channels with authentication |
//! | Chart Data Streaming | โ
Full Support | Real-time OHLCV data aggregation |
//! | Authentication | โ
API Key + Signature | Secure credential-based auth |
//! | Connection Management | โ
Auto-reconnect | Robust connection handling |
//! | Error Recovery | โ
Comprehensive | Detailed error types and handling |
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use deribit_websocket::prelude::*;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Install the rustls crypto provider that matches the active TLS feature.
//! // See the crate-level "TLS backends" section or `Cargo features` in the
//! // README for the available backends.
//! deribit_websocket::install_default_crypto_provider()?;
//!
//! // Create client for testnet
//! let config = WebSocketConfig::default();
//! let mut client = DeribitWebSocketClient::new(&config)?;
//!
//! // Set up message processing
//! client.set_message_handler(
//! |message| {
//! tracing::info!("Received: {}", message);
//! Ok(())
//! },
//! |message, error| {
//! tracing::error!("Error processing {}: {}", message, error);
//! }
//! );
//!
//! // Connect and subscribe
//! client.connect().await?;
//! client.subscribe(vec!["ticker.BTC-PERPETUAL".to_string()]).await?;
//!
//! // Start processing messages
//! client.start_message_processing_loop().await?;
//! Ok(())
//! }
//! ```
//!
//! ## Advanced Usage
//!
//! The client supports advanced subscription patterns for professional trading applications:
//!
//! ### Chart Data Streaming
//! ```rust,no_run
//! # use deribit_websocket::prelude::*;
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let config = WebSocketConfig::default();
//! # let client = DeribitWebSocketClient::new(&config)?;
//! // Subscribe to 1-minute chart data for BTC perpetual
//! client.subscribe(vec!["chart.trades.BTC-PERPETUAL.1".to_string()]).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Position Change Monitoring
//! ```rust,no_run
//! # use deribit_websocket::prelude::*;
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let config = WebSocketConfig::default();
//! # let client = DeribitWebSocketClient::new(&config)?;
//! // Monitor real-time position changes (requires authentication)
//! client.authenticate("client_id", "client_secret").await?;
//! client.subscribe(vec!["user.changes.BTC-PERPETUAL.raw".to_string()]).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Mass Quote System
//! ```rust,no_run
//! # use deribit_websocket::prelude::*;
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # let config = WebSocketConfig::default();
//! # let mut client = DeribitWebSocketClient::new(&config)?;
//! # client.connect().await?;
//! # client.authenticate("client_id", "client_secret").await?;
//! // Set up MMP group for mass quoting
//! let mmp_config = MmpGroupConfig::new(
//! "btc_market_making".to_string(),
//! 10.0, // quantity_limit
//! 5.0, // delta_limit
//! 1000, // interval (ms)
//! 5000, // frozen_time (ms)
//! )?;
//! client.set_mmp_config(mmp_config).await?;
//!
//! // Create and place mass quotes
//! let quotes = vec![
//! Quote::buy("BTC-PERPETUAL".to_string(), 0.1, 45000.0),
//! Quote::sell("BTC-PERPETUAL".to_string(), 0.1, 55000.0),
//! ];
//! let request = MassQuoteRequest::new("btc_market_making".to_string(), quotes);
//! let response = client.mass_quote(request).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Examples
//!
//! The crate includes comprehensive examples demonstrating:
//!
//! ### Core Examples
//! - **`basic_client.rs`** - Basic connection, subscription, and message handling
//! - **`callback_example.rs`** - Advanced callback system with error handling
//! - **`advanced_subscriptions.rs`** - Chart data and position change subscriptions
//!
//! ### Trading & Account Management (v0.2.0)
//! - **`trading_operations.rs`** - Buy, sell, cancel, edit orders
//! - **`account_operations.rs`** - Get positions, account summary, order history
//! - **`position_management.rs`** - Close positions, move positions between subaccounts
//!
//! ### Session Management (v0.2.0)
//! - **`session_management.rs`** - Hello, heartbeat, typed responses (AuthResponse, HelloResponse, TestResponse)
//! - **`cancel_on_disconnect.rs`** - Enable/disable/get cancel-on-disconnect status
//! - **`unsubscribe_all.rs`** - Public and private unsubscribe_all operations
//!
//! ### Market Data Subscriptions
//! - **`new_channels_subscription.rs`** - Grouped order book, incremental ticker, trades by kind
//! - **`perpetual_subscription.rs`** - Perpetual funding rate subscriptions
//! - **`quote_subscription.rs`** - Quote data subscriptions
//! - **`price_index_subscription.rs`** - Price index subscriptions
//!
//! ### Mass Quoting
//! - **`mass_quote_basic.rs`** - Basic mass quoting with MMP group setup
//! - **`mass_quote_advanced.rs`** - Advanced mass quoting with multiple MMP groups
//! - **`mass_quote_options.rs`** - Options-specific mass quoting with delta management
//!
//! ## Timeouts
//!
//! Two deadlines bound the most common sources of indefinite hangs in a
//! network client. Both live on `WebSocketConfig` and can be set via
//! builder methods or the corresponding environment variables:
//!
//! - **`connection_timeout`** (default 10s, env `DERIBIT_CONNECTION_TIMEOUT`) โ
//! upper bound on the WebSocket handshake (TCP + TLS + HTTP upgrade).
//! A peer that accepts the TCP connection but never completes the
//! upgrade makes `DeribitWebSocketClient::connect` / `Dispatcher::connect`
//! fail with `WebSocketError::Timeout` instead of hanging.
//! - **`request_timeout`** (default 30s, env `DERIBIT_REQUEST_TIMEOUT`) โ
//! upper bound on each `send_request` call, covering enqueue, write,
//! and response wait. On the deadline the dispatcher evicts the
//! now-orphaned waiter so the id-map stays small under repeated
//! timeouts.
//!
//! Planned follow-ups: `read_idle_timeout` (maximum gap between frames)
//! and granular per-operation overrides.
//!
//! ## Backpressure
//!
//! The client and dispatcher communicate over two **bounded**
//! `tokio::sync::mpsc` channels, both using **Strategy A (await-send)** โ
//! the producer blocks on a full channel, so frames are not dropped due
//! to backpressure. Frames can still be discarded if the notification
//! receiver has already been closed (for example during shutdown or
//! disconnect).
//!
//! - **`notification_channel_capacity`** (default 1024) โ notifications
//! from the dispatcher to the consumer. When full, the dispatcher
//! stops polling the WebSocket stream and the TCP recv buffer fills,
//! which makes the Deribit server apply flow control. Every
//! full-channel event emits a `tracing::warn!` so slow consumers are
//! visible in logs.
//! - **`dispatcher_command_capacity`** โ outbound commands from the
//! client to the dispatcher (request sends, cancel-request on timeout,
//! shutdown). When full, the caller blocks until the dispatcher drains
//! a slot; `request_timeout` on `send_request` still applies, so the
//! caller surfaces `WebSocketError::Timeout` if the deadline elapses
//! while waiting on the channel.
//!
//! Strategy A was chosen over drop-oldest / drop-newest variants because
//! the notification stream carries private trading events (order
//! updates, trade reports) where silent loss is unacceptable.
//!
//! ## Architecture
//!
//! The client is built with a modular architecture:
//! - **Connection Layer** - Low-level WebSocket connection management
//! - **Session Layer** - Protocol-aware session handling with authentication
//! - **Message Layer** - JSON-RPC request/response and notification handling
//! - **Subscription Layer** - Channel management and subscription tracking
//! - **Callback Layer** - Flexible message processing with error recovery
//!
//! ## TLS backends
//!
//! `deribit-websocket` exposes three mutually-exclusive TLS backends as
//! Cargo features, with a compile-time mutex (see the `tls` module)
//! that rejects any other combination:
//!
//! | Feature | Default | Behaviour |
//! | ---------------- | :-----: | ------------------------------------------------------------------ |
//! | `rustls-aws-lc` | โ
| `rustls` with the `aws-lc-rs` crypto provider + OS root store |
//! | `rustls-ring` | | `rustls` with the `ring` crypto provider + OS root store |
//! | `native-tls` | | OS-native TLS stack (SChannel / SecureTransport / OpenSSL) |
//!
//! Selecting a non-default backend:
//!
//! ```toml
//! # Cargo.toml
//! [dependencies]
//! deribit-websocket = { version = "0.2", default-features = false, features = ["rustls-ring"] }
//! ```
//!
//! or, from the command line:
//!
//! ```sh
//! cargo add deribit-websocket --no-default-features --features native-tls
//! ```
//!
//! Applications must call `install_default_crypto_provider` once at
//! startup โ it picks the right provider for the active feature and is
//! a no-op under `native-tls`.
//!
//! Because both `rustls-*` backends use the OS-native root store via
//! `rustls-native-certs`, minimal container images (Alpine, distroless)
//! must have `ca-certificates` (or equivalent) installed so the trust
//! store is populated.
// Catch dead rustdoc links and missing crate-level docs in CI via
// `RUSTDOCFLAGS="-D warnings" cargo doc`. `missing_crate_level_docs`
// is technically redundant with the substantial `//!` header above
// but it guards against the header ever being deleted in a refactor.
// Regression guard against future `std::sync::Mutex` use across `.await`.
// Tokio's mutex (which this crate uses) is intentionally allowed.
// Ban `.unwrap()`/`.expect()` in library code. `#[cfg(test)]` modules inside
// `src/` keep the default behaviour so existing unit tests continue to work.
/// Prelude module with commonly used types
/// Utility functions and helpers
pub use ;