deribit-http 0.7.0

HTTP REST API client for Deribit trading platform
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
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 15/9/25
******************************************************************************/
use crate::prelude::*;
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

/// Trading limit structure
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Default, Serialize, Deserialize)]
pub struct TradingLimit {
    /// Total rate limit for trading operations
    #[serde(default)]
    pub total: RateLimit,
}

/// Account limits structure
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Default, Serialize, Deserialize)]
pub struct AccountLimits {
    /// Whether limits are applied per currency
    #[serde(default)]
    pub limits_per_currency: bool,
    /// Rate limits for non-matching engine operations
    #[serde(default)]
    pub non_matching_engine: RateLimit,
    /// Rate limits for matching engine operations
    #[serde(default)]
    pub matching_engine: MatchingEngineLimit,
}

/// Rate limit structure
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Default, Serialize, Deserialize)]
pub struct RateLimit {
    /// Maximum burst capacity for rate limiting
    #[serde(default)]
    pub burst: u32,
    /// Rate limit per time unit
    #[serde(default)]
    pub rate: u32,
}

/// Matching engine limits
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Default, Serialize, Deserialize)]
pub struct MatchingEngineLimit {
    /// Trading limits configuration
    #[serde(default)]
    pub trading: TradingLimit,
    /// Spot trading rate limits
    #[serde(default)]
    pub spot: RateLimit,
    /// Quote request rate limits
    #[serde(default)]
    pub quotes: RateLimit,
    /// Maximum quotes rate limits
    #[serde(default)]
    pub max_quotes: RateLimit,
    /// Guaranteed quotes rate limits
    #[serde(default)]
    pub guaranteed_quotes: RateLimit,
    /// Cancel all orders rate limits
    #[serde(default)]
    pub cancel_all: RateLimit,
}

/// Response type for user trades, containing a vector of user trade data
pub type UserTradeResponse = Vec<UserTrade>;

/// Response type for user trades with pagination info (used by instrument-specific endpoints)
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct UserTradeWithPaginationResponse {
    /// List of user trades
    pub trades: Vec<UserTrade>,
    /// Whether there are more trades available
    pub has_more: bool,
}

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

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

/// Status response
#[skip_serializing_none]
#[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
#[skip_serializing_none]
#[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>,
}

/// 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,
}

/// APR data point
#[skip_serializing_none]
#[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,
}

/// Expirations response
#[skip_serializing_none]
#[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>,
}

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

impl SettlementsResponse {
    /// Create a new settlements response
    pub fn new(settlements: Vec<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()
    }
}

/// Paginated transaction log response
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize, Default)]
pub struct TransactionLogResponse {
    /// Continuation token for pagination. NULL when no continuation.
    pub continuation: Option<u64>,
    /// List of transaction log entries
    pub logs: Vec<TransactionLogEntry>,
}

/// Transfer result for order-related transfers (e.g., fee rebates)
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct TransferResultResponse {
    /// Transfer identifier
    pub id: String,
    /// Transfer status
    pub status: String,
}

/// Shared account-level fields returned by both singular and plural account summary endpoints.
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountInfo {
    /// Account id
    pub id: u64,
    /// User email
    pub email: String,
    /// System generated user nickname
    pub system_name: Option<String>,
    /// Account name (given by user)
    pub username: Option<String>,
    /// When Block RFQ Self Match Prevention is enabled
    pub block_rfq_self_match_prevention: Option<bool>,
    /// Time at which the account was created (milliseconds since the Unix epoch)
    pub creation_timestamp: Option<u64>,
    /// Account type
    #[serde(rename = "type")]
    pub account_type: Option<String>,
    /// Optional identifier of the referrer
    pub referrer_id: Option<String>,
    /// Whether account is loginable using email and password
    pub login_enabled: Option<bool>,
    /// Whether Security Key authentication is enabled
    pub security_keys_enabled: Option<bool>,
    /// Whether MMP is enabled
    pub mmp_enabled: Option<bool>,
    /// true when the inter-user transfers are enabled for user
    pub interuser_transfers_enabled: Option<bool>,
    /// Self trading rejection behavior - reject_taker or cancel_maker
    pub self_trading_reject_mode: Option<String>,
    /// true if self trading rejection behavior is applied to trades between subaccounts
    pub self_trading_extended_to_subaccounts: Option<bool>,
}

/// Account summary response containing user account information
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AccountSummaryResponse {
    /// Account id
    #[serde(default)]
    pub id: u64,
    /// User email
    #[serde(default)]
    pub email: String,
    /// System generated user nickname
    #[serde(default)]
    pub system_name: String,
    /// Account name (given by user)
    #[serde(default)]
    pub username: String,
    /// When Block RFQ Self Match Prevention is enabled
    #[serde(default)]
    pub block_rfq_self_match_prevention: bool,
    /// Time at which the account was created (milliseconds since the Unix epoch)
    #[serde(default)]
    pub creation_timestamp: u64,
    /// Account type
    #[serde(rename = "type", default)]
    pub account_type: String,
    /// Optional identifier of the referrer
    pub referrer_id: Option<String>,
    /// Whether account is loginable using email and password
    #[serde(default)]
    pub login_enabled: bool,
    /// Whether Security Key authentication is enabled
    #[serde(default)]
    pub security_keys_enabled: bool,
    /// Whether MMP is enabled
    #[serde(default)]
    pub mmp_enabled: bool,
    /// true when the inter-user transfers are enabled for user
    #[serde(default)]
    pub interuser_transfers_enabled: bool,
    /// Self trading rejection behavior - reject_taker or cancel_maker
    #[serde(default)]
    pub self_trading_reject_mode: String,
    /// true if self trading rejection behavior is applied to trades between subaccounts
    #[serde(default)]
    pub self_trading_extended_to_subaccounts: bool,
    /// Aggregated list of per-currency account summaries
    #[serde(default)]
    pub summaries: Vec<AccountResult>,
}

/// Response from `get_account_summaries` (plural, all currencies).
///
/// Returns account-level fields with a `summaries` array containing
/// per-currency financial data.
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AccountSummariesResponse {
    /// Account-level fields (id, email, type, etc.)
    #[serde(flatten)]
    pub account: AccountInfo,
    /// Per-currency account summaries
    pub summaries: Vec<AccountResult>,
}

/// Mark price history data point
///
/// Represents a single data point in mark price history.
/// The API returns data as `[timestamp_ms, mark_price]` arrays.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(from = "(u64, f64)", into = "(u64, f64)")]
pub struct MarkPriceHistoryPoint {
    /// Timestamp in milliseconds since Unix epoch
    pub timestamp: u64,
    /// Mark price value
    pub mark_price: f64,
}

impl From<(u64, f64)> for MarkPriceHistoryPoint {
    fn from((timestamp, mark_price): (u64, f64)) -> Self {
        Self {
            timestamp,
            mark_price,
        }
    }
}

impl From<MarkPriceHistoryPoint> for (u64, f64) {
    fn from(point: MarkPriceHistoryPoint) -> Self {
        (point.timestamp, point.mark_price)
    }
}

/// Index name information with extended details
///
/// Represents an index with optional combo trading availability flags.
/// Returned by `get_supported_index_names` when `extended=true`.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexNameInfo {
    /// Index name identifier (e.g., "btc_eth", "btc_usdc")
    pub name: String,
    /// Whether future combo creation is enabled for this index
    pub future_combo_enabled: Option<bool>,
    /// Whether option combo creation is enabled for this index
    pub option_combo_enabled: Option<bool>,
}

/// Aggregated trade volume by currency
///
/// Contains 24-hour trade volumes for different instrument types.
/// When `extended=true`, also includes 7-day and 30-day volumes.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TradeVolume {
    /// Currency (e.g., "BTC", "ETH", "USDC")
    pub currency: String,
    /// Total 24h trade volume for call options
    pub calls_volume: f64,
    /// Total 24h trade volume for put options
    pub puts_volume: f64,
    /// Total 24h trade volume for futures
    pub futures_volume: f64,
    /// Total 24h trade volume for spot
    pub spot_volume: f64,
    /// Total 7d trade volume for call options (extended only)
    pub calls_volume_7d: Option<f64>,
    /// Total 7d trade volume for put options (extended only)
    pub puts_volume_7d: Option<f64>,
    /// Total 7d trade volume for futures (extended only)
    pub futures_volume_7d: Option<f64>,
    /// Total 7d trade volume for spot (extended only)
    pub spot_volume_7d: Option<f64>,
    /// Total 30d trade volume for call options (extended only)
    pub calls_volume_30d: Option<f64>,
    /// Total 30d trade volume for put options (extended only)
    pub puts_volume_30d: Option<f64>,
    /// Total 30d trade volume for futures (extended only)
    pub futures_volume_30d: Option<f64>,
    /// Total 30d trade volume for spot (extended only)
    pub spot_volume_30d: Option<f64>,
}

/// A single volatility index candle
///
/// Represents OHLC data for a volatility index at a specific timestamp.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VolatilityIndexCandle {
    /// Timestamp in milliseconds since UNIX epoch
    pub timestamp: u64,
    /// Open value
    pub open: f64,
    /// High value
    pub high: f64,
    /// Low value
    pub low: f64,
    /// Close value
    pub close: f64,
}

/// Response from get_volatility_index_data
///
/// Contains volatility index candles and optional continuation token.
#[skip_serializing_none]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VolatilityIndexData {
    /// Candles as OHLC data
    #[serde(deserialize_with = "deserialize_candles")]
    pub data: Vec<VolatilityIndexCandle>,
    /// Continuation token for pagination (use as end_timestamp for next request)
    pub continuation: Option<u64>,
}

/// Deserialize candles from array of arrays format
fn deserialize_candles<'de, D>(deserializer: D) -> Result<Vec<VolatilityIndexCandle>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde::de::Error;
    let raw: Vec<Vec<serde_json::Value>> = Vec::deserialize(deserializer)?;
    let mut candles = Vec::with_capacity(raw.len());

    for arr in raw {
        if arr.len() != 5 {
            return Err(D::Error::custom(format!(
                "expected 5 elements in candle array, got {}",
                arr.len()
            )));
        }
        let timestamp = arr[0]
            .as_u64()
            .ok_or_else(|| D::Error::custom("invalid timestamp"))?;
        let open = arr[1]
            .as_f64()
            .ok_or_else(|| D::Error::custom("invalid open"))?;
        let high = arr[2]
            .as_f64()
            .ok_or_else(|| D::Error::custom("invalid high"))?;
        let low = arr[3]
            .as_f64()
            .ok_or_else(|| D::Error::custom("invalid low"))?;
        let close = arr[4]
            .as_f64()
            .ok_or_else(|| D::Error::custom("invalid close"))?;

        candles.push(VolatilityIndexCandle {
            timestamp,
            open,
            high,
            low,
            close,
        });
    }

    Ok(candles)
}

/// Account summary information
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
pub struct AccountResult {
    /// Currency of the summary
    #[serde(default)]
    pub currency: String,
    /// The account's balance
    pub balance: f64,
    /// The account's current equity
    pub equity: f64,
    /// The account's available funds
    pub available_funds: f64,
    /// The account's margin balance
    pub margin_balance: f64,
    /// Profit and loss
    pub total_pl: Option<f64>,
    /// Session realized profit and loss
    pub session_rpl: Option<f64>,
    /// Session unrealized profit and loss
    pub session_upl: Option<f64>,
    /// The maintenance margin
    pub maintenance_margin: f64,
    /// The account's initial margin
    pub initial_margin: f64,
    /// The account's available to withdrawal funds
    pub available_withdrawal_funds: Option<f64>,
    /// When true cross collateral is enabled for user
    pub cross_collateral_enabled: Option<bool>,
    /// The sum of position deltas
    pub delta_total: Option<f64>,
    /// Futures profit and Loss
    pub futures_pl: Option<f64>,
    /// Futures session realized profit and Loss
    pub futures_session_rpl: Option<f64>,
    /// Futures session unrealized profit and Loss
    pub futures_session_upl: Option<f64>,
    /// Options summary delta
    pub options_delta: Option<f64>,
    /// Options summary gamma
    pub options_gamma: Option<f64>,
    /// Options profit and Loss
    pub options_pl: Option<f64>,
    /// Options session realized profit and Loss
    pub options_session_rpl: Option<f64>,
    /// Options session unrealized profit and Loss
    pub options_session_upl: Option<f64>,
    /// Options summary theta
    pub options_theta: Option<f64>,
    /// Options summary vega
    pub options_vega: Option<f64>,
    /// true when portfolio margining is enabled for user
    pub portfolio_margining_enabled: Option<bool>,
    /// The sum of position deltas without positions that will expire during closest expiration
    pub projected_delta_total: Option<f64>,
    /// Projected initial margin
    pub projected_initial_margin: Option<f64>,
    /// Projected maintenance margin
    pub projected_maintenance_margin: Option<f64>,
    /// Delta total map (currency -> delta)
    pub delta_total_map: Option<std::collections::HashMap<String, f64>>,
    /// The deposit address for the account (if available)
    pub deposit_address: Option<String>,
    /// List of fee objects for all currency pairs and instrument types
    pub fees: Option<Vec<FeeStructure>>,
    /// Account limits structure
    pub limits: Option<AccountLimits>,
    /// Name of user's currently enabled margin model
    pub margin_model: Option<String>,
    /// Map of options' gammas per index
    pub options_gamma_map: Option<std::collections::HashMap<String, f64>>,
    /// Map of options' thetas per index
    pub options_theta_map: Option<std::collections::HashMap<String, f64>>,
    /// Map of options' vegas per index
    pub options_vega_map: Option<std::collections::HashMap<String, f64>>,
    /// Options value
    pub options_value: Option<f64>,
    /// The account's balance reserved in active spot orders
    pub spot_reserve: Option<f64>,
    /// Estimated Liquidation Ratio
    pub estimated_liquidation_ratio: Option<f64>,
    /// Estimated liquidation ratio map
    pub estimated_liquidation_ratio_map: Option<std::collections::HashMap<String, f64>>,
    /// The account's fee balance (it can be used to pay for fees)
    pub fee_balance: Option<f64>,
    /// The account's balance reserved in other orders
    pub additional_reserve: Option<f64>,

    // Additional fields for cross-collateral users
    /// Optional field returned with value true when user has non block chain equity
    pub has_non_block_chain_equity: Option<bool>,
    /// The account's total margin balance in all cross collateral currencies, expressed in USD
    pub total_margin_balance_usd: Option<f64>,
    /// The account's total delta total in all cross collateral currencies, expressed in USD
    pub total_delta_total_usd: Option<f64>,
    /// The account's total initial margin in all cross collateral currencies, expressed in USD
    pub total_initial_margin_usd: Option<f64>,
    /// The account's total maintenance margin in all cross collateral currencies, expressed in USD
    pub total_maintenance_margin_usd: Option<f64>,
    /// The account's total equity in all cross collateral currencies, expressed in USD
    pub total_equity_usd: Option<f64>,
    /// System name for the account
    pub system_name: Option<String>,
    /// Account type
    pub account_type: Option<String>,
}