deribit_http/model/
types.rs

1//! HTTP-specific types and models
2
3use pretty_simple_display::{DebugPretty, DisplaySimple};
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6use std::collections::HashMap;
7
8/// API error structure
9#[skip_serializing_none]
10#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
11pub struct ApiError {
12    /// Error code number
13    pub code: i32,
14    /// Human-readable error message
15    pub message: String,
16    /// Additional error data
17    pub data: Option<serde_json::Value>,
18}
19
20/// Authentication token structure
21#[skip_serializing_none]
22#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
23pub struct AuthToken {
24    /// OAuth2 access token
25    pub access_token: String,
26    /// Token type (typically "Bearer")
27    pub token_type: String,
28    /// Token expiration time in seconds
29    pub expires_in: u64,
30    /// Optional refresh token for renewing access
31    pub refresh_token: Option<String>,
32    /// Token scope permissions
33    pub scope: String,
34}
35
36/// Request parameters
37#[derive(DebugPretty, DisplaySimple, Clone, Default, Serialize, Deserialize)]
38pub struct RequestParams {
39    params: HashMap<String, serde_json::Value>,
40}
41
42impl RequestParams {
43    /// Create new empty parameters
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Add a parameter
49    pub fn add<T: Serialize>(mut self, key: &str, value: T) -> Self {
50        if let Ok(json_value) = serde_json::to_value(value) {
51            self.params.insert(key.to_string(), json_value);
52        }
53        self
54    }
55
56    /// Convert to JSON value
57    pub fn to_json(&self) -> serde_json::Value {
58        serde_json::to_value(&self.params).unwrap_or(serde_json::Value::Null)
59    }
60
61    /// Check if empty
62    pub fn is_empty(&self) -> bool {
63        self.params.is_empty()
64    }
65}
66
67/// Time in force enumeration
68#[derive(DebugPretty, DisplaySimple, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69pub enum TimeInForce {
70    /// Order remains active until explicitly cancelled
71    #[serde(rename = "good_til_cancelled")]
72    GoodTilCancelled,
73    /// Order expires at the end of the trading day
74    #[serde(rename = "good_til_day")]
75    GoodTilDay,
76    /// Order must be filled immediately and completely or cancelled
77    #[serde(rename = "fill_or_kill")]
78    FillOrKill,
79    /// Order must be filled immediately, partial fills allowed, remaining cancelled
80    #[serde(rename = "immediate_or_cancel")]
81    ImmediateOrCancel,
82}
83
84impl TimeInForce {
85    /// Returns the string representation of the time in force value
86    pub fn as_str(&self) -> &'static str {
87        match self {
88            TimeInForce::GoodTilCancelled => "good_til_cancelled",
89            TimeInForce::GoodTilDay => "good_til_day",
90            TimeInForce::FillOrKill => "fill_or_kill",
91            TimeInForce::ImmediateOrCancel => "immediate_or_cancel",
92        }
93    }
94}
95
96/// Withdrawal information
97#[skip_serializing_none]
98#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
99pub struct Withdrawal {
100    /// Withdrawal address
101    pub address: String,
102    /// Withdrawal amount
103    pub amount: f64,
104    /// Currency of the withdrawal
105    pub currency: String,
106    /// Withdrawal fee
107    pub fee: f64,
108    /// Unique withdrawal identifier
109    pub id: u64,
110    /// Withdrawal priority level
111    pub priority: String,
112    /// Current state of the withdrawal
113    pub state: String,
114    /// Timestamp when withdrawal was created
115    pub created_timestamp: u64,
116    /// Timestamp when withdrawal was last updated
117    pub updated_timestamp: Option<u64>,
118    /// Transaction ID on the blockchain
119    pub transaction_id: Option<String>,
120}
121
122/// Position direction enumeration
123#[derive(Debug, Clone, Serialize, Deserialize)]
124#[serde(rename_all = "lowercase")]
125pub enum Direction {
126    /// Buy direction
127    Buy,
128    /// Sell direction
129    Sell,
130}