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
/******************************************************************************
Author: Joaquín Béjar García
Email: jb@taunais.com
Date: 15/9/25
******************************************************************************/
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
/// Transfer state enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum TransferState {
/// Transfer is prepared but not yet confirmed
#[default]
Prepared,
/// Transfer has been confirmed
Confirmed,
/// Transfer has been cancelled
Cancelled,
/// Transfer is waiting for admin approval
WaitingForAdmin,
/// Transfer failed due to insufficient funds
InsufficientFunds,
/// Transfer failed due to withdrawal limit
WithdrawalLimit,
}
/// Transfer information
#[skip_serializing_none]
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, Deserialize)]
pub struct Transfer {
/// Transfer ID
pub id: i64,
/// Currency being transferred
pub currency: String,
/// Transfer amount
pub amount: f64,
/// Transfer fee
pub fee: f64,
/// Destination address
pub address: String,
/// Blockchain transaction ID
pub transaction_id: Option<String>,
/// Current transfer state
pub state: TransferState,
/// Creation timestamp (milliseconds since Unix epoch)
pub created_timestamp: i64,
/// Last update timestamp (milliseconds since Unix epoch)
pub updated_timestamp: i64,
/// Confirmation timestamp (milliseconds since Unix epoch)
pub confirmed_timestamp: Option<i64>,
/// Transfer type description
pub transfer_type: Option<String>,
}
impl Transfer {
/// Create a new transfer
pub fn new(
id: i64,
currency: String,
amount: f64,
fee: f64,
address: String,
created_timestamp: i64,
) -> Self {
Self {
id,
currency,
amount,
fee,
address,
transaction_id: None,
state: TransferState::Prepared,
created_timestamp,
updated_timestamp: created_timestamp,
confirmed_timestamp: None,
transfer_type: None,
}
}
/// Set transaction ID
pub fn with_transaction_id(mut self, tx_id: String) -> Self {
self.transaction_id = Some(tx_id);
self
}
/// Set transfer state
pub fn with_state(mut self, state: TransferState) -> Self {
self.state = state;
self
}
/// Set transfer type
pub fn with_type(mut self, transfer_type: String) -> Self {
self.transfer_type = Some(transfer_type);
self
}
/// Confirm the transfer
pub fn confirm(&mut self, timestamp: i64) {
self.state = TransferState::Confirmed;
self.confirmed_timestamp = Some(timestamp);
self.updated_timestamp = timestamp;
}
/// Cancel the transfer
pub fn cancel(&mut self, timestamp: i64) {
self.state = TransferState::Cancelled;
self.updated_timestamp = timestamp;
}
/// Check if transfer is confirmed
pub fn is_confirmed(&self) -> bool {
matches!(self.state, TransferState::Confirmed)
}
/// Check if transfer is cancelled
pub fn is_cancelled(&self) -> bool {
matches!(self.state, TransferState::Cancelled)
}
/// Check if transfer is pending
pub fn is_pending(&self) -> bool {
matches!(
self.state,
TransferState::Prepared | TransferState::WaitingForAdmin
)
}
/// Get net amount (amount - fee)
pub fn net_amount(&self) -> f64 {
self.amount - self.fee
}
}
/// Collection of transfers
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, Deserialize)]
pub struct Transfers {
/// List of transfers
pub transfers: Vec<Transfer>,
}
impl Transfers {
/// Create a new transfers collection
pub fn new() -> Self {
Self {
transfers: Vec::new(),
}
}
/// Add a transfer
pub fn add(&mut self, transfer: Transfer) {
self.transfers.push(transfer);
}
/// Get transfers by currency
pub fn by_currency(&self, currency: String) -> Vec<&Transfer> {
self.transfers
.iter()
.filter(|t| t.currency == currency)
.collect()
}
/// Get transfers by state
pub fn by_state(&self, state: TransferState) -> Vec<&Transfer> {
self.transfers.iter().filter(|t| t.state == state).collect()
}
/// Get pending transfers
pub fn pending(&self) -> Vec<&Transfer> {
self.transfers.iter().filter(|t| t.is_pending()).collect()
}
/// Get confirmed transfers
pub fn confirmed(&self) -> Vec<&Transfer> {
self.transfers.iter().filter(|t| t.is_confirmed()).collect()
}
/// Calculate total amount by currency
pub fn total_amount(&self, currency: String) -> f64 {
self.transfers
.iter()
.filter(|t| t.currency == currency)
.map(|t| t.amount)
.sum()
}
/// Calculate total fees by currency
pub fn total_fees(&self, currency: String) -> f64 {
self.transfers
.iter()
.filter(|t| t.currency == currency)
.map(|t| t.fee)
.sum()
}
}
impl Default for Transfers {
fn default() -> Self {
Self::new()
}
}
/// Subaccount transfer information
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, Deserialize)]
pub struct SubaccountTransfer {
/// Transfer amount
pub amount: f64,
/// Currency being transferred
pub currency: String,
/// Destination subaccount ID
pub destination: i64,
/// Transfer ID
pub id: i64,
/// Source subaccount ID
pub source: i64,
/// Transfer state
pub state: TransferState,
/// Transfer timestamp (milliseconds since Unix epoch)
pub timestamp: i64,
/// Type of transfer
pub transfer_type: String,
}
impl SubaccountTransfer {
/// Create a new subaccount transfer
pub fn new(
id: i64,
amount: f64,
currency: String,
source: i64,
destination: i64,
timestamp: i64,
) -> Self {
Self {
amount,
currency,
destination,
id,
source,
state: TransferState::Prepared,
timestamp,
transfer_type: "subaccount".to_string(),
}
}
/// Set transfer state
pub fn with_state(mut self, state: TransferState) -> Self {
self.state = state;
self
}
/// Set transfer type
pub fn with_type(mut self, transfer_type: String) -> Self {
self.transfer_type = transfer_type;
self
}
/// Check if transfer is between main account and subaccount
pub fn is_main_subaccount_transfer(&self) -> bool {
self.source == 0 || self.destination == 0
}
/// Check if transfer is between subaccounts
pub fn is_subaccount_to_subaccount(&self) -> bool {
self.source != 0 && self.destination != 0
}
}