deribit_base/model/
transaction.rs

1/****************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 21/7/25
5******************************************************************************/
6
7use crate::{impl_json_debug_pretty, impl_json_display};
8use serde::{Deserialize, Serialize};
9
10/// Transaction type enumeration
11#[derive(Clone, Serialize, Deserialize, Default)]
12pub enum TransactionType {
13    /// Deposit transaction
14    Deposit,
15    /// Withdrawal transaction
16    Withdrawal,
17    /// Trade transaction (default)
18    #[default]
19    Trade,
20    /// Transfer transaction
21    Transfer,
22    /// Fee transaction
23    Fee,
24    /// Funding transaction
25    Funding,
26    /// Bonus transaction
27    Bonus,
28    /// Dividend transaction
29    Dividend,
30    /// Liquidation transaction
31    Liquidation,
32    /// Insurance transaction
33    Insurance,
34}
35
36/// Generic transaction log entry
37#[derive(Clone, Serialize, Deserialize)]
38pub struct TransactionLogEntry {
39    /// Unique transaction identifier
40    pub id: u64,
41    /// Currency of the transaction
42    pub currency: String,
43    /// Transaction amount
44    pub amount: f64,
45    /// Account balance after transaction
46    pub balance: f64,
47    /// Transaction timestamp
48    pub timestamp: u64,
49    /// Type of transaction
50    pub transaction_type: TransactionType,
51    /// Additional transaction information
52    pub info: Option<String>,
53}
54
55impl Default for TransactionLogEntry {
56    fn default() -> Self {
57        Self {
58            id: 0,
59            currency: String::new(),
60            amount: 0.0,
61            balance: 0.0,
62            timestamp: 0,
63            transaction_type: TransactionType::default(),
64            info: None,
65        }
66    }
67}
68
69impl_json_display!(TransactionLogEntry);
70impl_json_debug_pretty!(TransactionLogEntry);
71
72/// Paginated transaction log response
73#[derive(Clone, Serialize, Deserialize, Default)]
74pub struct TransactionLog {
75    /// Continuation token for pagination
76    pub continuation: Option<String>,
77    /// List of transaction log entries
78    pub logs: Vec<TransactionLogEntry>,
79}
80
81impl_json_display!(TransactionLog);
82impl_json_debug_pretty!(TransactionLog);
83
84/// Deposit information
85#[derive(Clone, Serialize, Deserialize)]
86pub struct Deposit {
87    /// Deposit address
88    pub address: String,
89    /// Deposit amount
90    pub amount: f64,
91    /// Currency of the deposit
92    pub currency: String,
93    /// Current state of the deposit
94    pub state: String,
95    /// Timestamp when deposit was received
96    pub received_timestamp: u64,
97    /// Transaction ID on the blockchain
98    pub transaction_id: Option<String>,
99    /// Timestamp when deposit was last updated
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub updated_timestamp: Option<u64>,
102}
103
104impl_json_display!(Deposit);
105impl_json_debug_pretty!(Deposit);
106
107/// Deposits response wrapper
108#[derive(Clone, Serialize, Deserialize)]
109pub struct DepositsResponse {
110    /// Total count of deposits
111    pub count: u32,
112    /// List of deposit entries
113    pub data: Vec<Deposit>,
114}
115
116impl_json_display!(DepositsResponse);
117impl_json_debug_pretty!(DepositsResponse);
118
119/// Withdrawal information
120#[derive(Clone, Serialize, Deserialize)]
121pub struct Withdrawal {
122    /// Withdrawal address
123    pub address: String,
124    /// Withdrawal amount
125    pub amount: f64,
126    /// Currency of the withdrawal
127    pub currency: String,
128    /// Withdrawal fee
129    pub fee: f64,
130    /// Unique withdrawal identifier
131    pub id: u64,
132    /// Withdrawal priority level
133    pub priority: String,
134    /// Current state of the withdrawal
135    pub state: String,
136    /// Timestamp when withdrawal was created
137    pub created_timestamp: u64,
138    /// Timestamp when withdrawal was last updated
139    pub updated_timestamp: Option<u64>,
140    /// Transaction ID on the blockchain
141    pub transaction_id: Option<String>,
142}
143
144impl_json_display!(Withdrawal);
145impl_json_debug_pretty!(Withdrawal);
146
147/// Withdrawals response wrapper
148#[derive(Clone, Serialize, Deserialize)]
149pub struct WithdrawalsResponse {
150    /// Total count of withdrawals
151    pub count: u32,
152    /// List of withdrawal entries
153    pub data: Vec<Withdrawal>,
154}
155
156impl_json_display!(WithdrawalsResponse);
157impl_json_debug_pretty!(WithdrawalsResponse);
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162
163    #[test]
164    fn test_default_transaction_log_entry() {
165        let tx = TransactionLogEntry::default();
166        assert_eq!(tx.id, 0);
167        assert_eq!(tx.amount, 0.0);
168    }
169}