deribit_http/model/
account.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 21/7/25
5******************************************************************************/
6use crate::model::response::other::AccountSummaryResponse;
7use pretty_simple_display::{DebugPretty, DisplaySimple};
8use serde::{Deserialize, Serialize};
9use serde_with::skip_serializing_none;
10
11/// Subaccount information
12#[skip_serializing_none]
13#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
14pub struct Subaccount {
15    /// Subaccount email
16    pub email: String,
17    /// Subaccount ID
18    pub id: u64,
19    /// Whether login is enabled
20    pub login_enabled: bool,
21    /// Portfolio information (optional)
22    pub portfolio: Option<std::collections::HashMap<String, CurrencyPortfolio>>,
23    /// Whether to receive notifications
24    pub receive_notifications: bool,
25    /// System name
26    pub system_name: String,
27    /// Time in force (optional)
28    pub tif: Option<String>,
29    /// Subaccount type
30    #[serde(rename = "type")]
31    pub subaccount_type: String,
32    /// Username
33    pub username: String,
34    /// Margin model
35    pub margin_model: Option<String>,
36    /// Available funds
37    pub available_funds: Option<f64>,
38    /// Disabled trading products
39    pub disabled_trading_products: Option<Vec<String>>,
40    /// Is password
41    pub is_password: Option<bool>,
42    /// Proof ID
43    pub proof_id: Option<String>,
44    /// Proof ID signature
45    pub proof_id_signature: Option<String>,
46    /// Security keys assignments
47    pub security_keys_assignments: Option<Vec<serde_json::Value>>,
48    /// Security keys enabled
49    pub security_keys_enabled: Option<bool>,
50    /// Trading products details
51    pub trading_products_details: Option<Vec<TradingProductDetail>>,
52    /// Referrals count
53    pub referrals_count: Option<u64>,
54}
55
56/// Currency portfolio information
57#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
58pub struct CurrencyPortfolio {
59    /// Available funds
60    pub available_funds: f64,
61    /// Available withdrawal funds
62    pub available_withdrawal_funds: f64,
63    /// Balance
64    pub balance: f64,
65    /// Currency
66    pub currency: String,
67    /// Equity
68    pub equity: f64,
69    /// Initial margin
70    pub initial_margin: f64,
71    /// Locked balance
72    pub locked_balance: f64,
73    /// Maintenance margin
74    pub maintenance_margin: f64,
75    /// Margin balance
76    pub margin_balance: f64,
77    /// Spot reserve
78    pub spot_reserve: f64,
79    /// Additional reserve
80    pub additional_reserve: f64,
81}
82
83/// Trading product detail
84#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
85pub struct TradingProductDetail {
86    /// Whether enabled
87    pub enabled: bool,
88    /// Product name
89    pub product: String,
90    /// Whether overwriteable
91    pub overwriteable: bool,
92}
93
94/// Portfolio information (legacy)
95#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
96pub struct PortfolioInfo {
97    /// Available funds
98    pub available_funds: f64,
99    /// Available withdrawal funds
100    pub available_withdrawal_funds: f64,
101    /// Balance
102    pub balance: f64,
103    /// Currency
104    pub currency: String,
105    /// Delta total
106    pub delta_total: f64,
107    /// Equity
108    pub equity: f64,
109    /// Initial margin
110    pub initial_margin: f64,
111    /// Maintenance margin
112    pub maintenance_margin: f64,
113    /// Margin balance
114    pub margin_balance: f64,
115    /// Session realized P&L
116    pub session_rpl: f64,
117    /// Session unrealized P&L
118    pub session_upl: f64,
119    /// Total P&L
120    pub total_pl: f64,
121}
122
123/// Portfolio information
124#[skip_serializing_none]
125#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
126pub struct Portfolio {
127    /// Currency of the portfolio
128    pub currency: String,
129    /// Account summaries for different currencies
130    pub accounts: Vec<AccountSummaryResponse>,
131    /// Total portfolio value in USD
132    pub total_usd_value: Option<f64>,
133    /// Cross-currency margin enabled
134    pub cross_margin_enabled: bool,
135}
136
137impl Portfolio {
138    /// Create a new empty portfolio
139    pub fn new(currency: String) -> Self {
140        Self {
141            currency,
142            accounts: Vec::new(),
143            total_usd_value: None,
144            cross_margin_enabled: false,
145        }
146    }
147
148    /// Add an account summary to the portfolio
149    pub fn add_account(&mut self, account: AccountSummaryResponse) {
150        self.accounts.push(account);
151    }
152}