longbridge/asset/requests/
get_account_records.rs1use serde::{Serialize, Serializer};
2use time::OffsetDateTime;
3
4use crate::asset::{DepositState, RewardType, StockTransferInState, StockTransferOutState};
5
6fn unix_seconds_opt<S>(value: &Option<OffsetDateTime>, serializer: S) -> Result<S::Ok, S::Error>
7where
8 S: Serializer,
9{
10 match value {
11 Some(value) => serializer.serialize_i64(value.unix_timestamp()),
12 None => serializer.serialize_none(),
13 }
14}
15
16#[derive(Debug, Default, Serialize, Clone)]
24pub struct GetStockTransfersOptions<S>
25where
26 S: Serialize,
27{
28 #[serde(skip_serializing_if = "Option::is_none")]
29 market: Option<String>,
30 #[serde(skip_serializing_if = "Option::is_none")]
31 state: Option<S>,
32 #[serde(
33 skip_serializing_if = "Option::is_none",
34 serialize_with = "unix_seconds_opt"
35 )]
36 start: Option<OffsetDateTime>,
37 #[serde(
38 skip_serializing_if = "Option::is_none",
39 serialize_with = "unix_seconds_opt"
40 )]
41 end: Option<OffsetDateTime>,
42 #[serde(skip_serializing_if = "Option::is_none")]
43 page: Option<i32>,
44 #[serde(skip_serializing_if = "Option::is_none")]
45 size: Option<i32>,
46}
47
48mod sealed {
49 pub trait Sealed {}
50
51 impl Sealed for crate::asset::StockTransferInState {}
52 impl Sealed for crate::asset::StockTransferOutState {}
53}
54
55pub trait StockTransferStateFilter:
58 Serialize + Default + PartialEq + Copy + sealed::Sealed
59{
60}
61
62impl StockTransferStateFilter for StockTransferInState {}
63impl StockTransferStateFilter for StockTransferOutState {}
64
65pub type GetStockTransfersInOptions = GetStockTransfersOptions<StockTransferInState>;
67
68pub type GetStockTransfersOutOptions = GetStockTransfersOptions<StockTransferOutState>;
70
71impl<S> GetStockTransfersOptions<S>
72where
73 S: StockTransferStateFilter,
74{
75 #[inline]
77 pub fn new() -> Self {
78 Self::default()
79 }
80
81 #[inline]
83 #[must_use]
84 pub fn market(self, market: impl Into<String>) -> Self {
85 Self {
86 market: Some(market.into().trim().to_uppercase()),
87 ..self
88 }
89 }
90
91 #[inline]
94 #[must_use]
95 pub fn state(self, state: S) -> Self {
96 Self {
97 state: (state != S::default()).then_some(state),
98 ..self
99 }
100 }
101
102 #[inline]
104 #[must_use]
105 pub fn start(self, start: OffsetDateTime) -> Self {
106 Self {
107 start: Some(start),
108 ..self
109 }
110 }
111
112 #[inline]
114 #[must_use]
115 pub fn end(self, end: OffsetDateTime) -> Self {
116 Self {
117 end: Some(end),
118 ..self
119 }
120 }
121
122 #[inline]
124 #[must_use]
125 pub fn page(self, page: i32) -> Self {
126 Self {
127 page: Some(page),
128 ..self
129 }
130 }
131
132 #[inline]
134 #[must_use]
135 pub fn size(self, size: i32) -> Self {
136 Self {
137 size: Some(size),
138 ..self
139 }
140 }
141}
142
143#[derive(Debug, Default, Serialize, Clone)]
145pub struct GetRewardRecordsOptions {
146 #[serde(rename = "current", skip_serializing_if = "Option::is_none")]
147 page: Option<i32>,
148 #[serde(skip_serializing_if = "Option::is_none")]
149 page_size: Option<i32>,
150 #[serde(skip_serializing_if = "Option::is_none")]
151 reward_type: Option<RewardType>,
152}
153
154impl GetRewardRecordsOptions {
155 #[inline]
157 pub fn new() -> Self {
158 Self::default()
159 }
160
161 #[inline]
163 #[must_use]
164 pub fn page(self, page: i32) -> Self {
165 Self {
166 page: Some(page),
167 ..self
168 }
169 }
170
171 #[inline]
173 #[must_use]
174 pub fn page_size(self, page_size: i32) -> Self {
175 Self {
176 page_size: Some(page_size),
177 ..self
178 }
179 }
180
181 #[inline]
184 #[must_use]
185 pub fn reward_type(self, reward_type: RewardType) -> Self {
186 Self {
187 reward_type: (reward_type != RewardType::Unknown).then_some(reward_type),
188 ..self
189 }
190 }
191}
192
193#[derive(Debug, Default, Serialize, Clone)]
195pub struct GetCouponsOptions {
196 #[serde(rename = "current", skip_serializing_if = "Option::is_none")]
197 page: Option<i32>,
198 #[serde(skip_serializing_if = "Option::is_none")]
199 page_size: Option<i32>,
200 #[serde(serialize_with = "serialize_card_status")]
201 card_status: bool,
202 #[serde(skip_serializing_if = "Option::is_none")]
203 card_type: Option<String>,
204}
205
206fn serialize_card_status<S>(expired: &bool, serializer: S) -> Result<S::Ok, S::Error>
209where
210 S: Serializer,
211{
212 serializer.serialize_i32(if *expired { 2 } else { -1 })
213}
214
215impl GetCouponsOptions {
216 #[inline]
219 pub fn new() -> Self {
220 Self::default()
221 }
222
223 #[inline]
225 #[must_use]
226 pub fn page(self, page: i32) -> Self {
227 Self {
228 page: Some(page),
229 ..self
230 }
231 }
232
233 #[inline]
235 #[must_use]
236 pub fn page_size(self, page_size: i32) -> Self {
237 Self {
238 page_size: Some(page_size),
239 ..self
240 }
241 }
242
243 #[inline]
245 #[must_use]
246 pub fn expired(self, expired: bool) -> Self {
247 Self {
248 card_status: expired,
249 ..self
250 }
251 }
252
253 #[inline]
256 #[must_use]
257 pub fn card_type(self, card_type: impl Into<String>) -> Self {
258 Self {
259 card_type: Some(card_type.into()),
260 ..self
261 }
262 }
263}
264
265#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
267pub enum CouponOrderType {
268 Locked,
270 Settled,
272}
273
274impl CouponOrderType {
275 pub(crate) fn as_str(self) -> &'static str {
276 match self {
277 CouponOrderType::Locked => "locked",
278 CouponOrderType::Settled => "settled",
279 }
280 }
281}
282
283#[derive(Debug, Serialize)]
284pub(crate) struct GetCouponOrdersQuery {
285 pub(crate) card_id: String,
286 pub(crate) order_type: &'static str,
287}
288
289#[derive(Debug, Serialize)]
290pub(crate) struct GetEddaApplicationsQuery {
291 pub(crate) card_id: String,
292 pub(crate) currency: String,
293}
294
295pub(crate) const MAX_DEPOSITS_PAGE_SIZE: i32 = 20;
297
298#[derive(Debug, Default, Serialize, Clone)]
300pub struct GetDepositsOptions {
301 #[serde(skip_serializing_if = "Option::is_none")]
302 page: Option<i32>,
303 #[serde(skip_serializing_if = "Option::is_none")]
304 size: Option<i32>,
305 #[serde(skip_serializing_if = "Option::is_none")]
306 states: Option<String>,
307 #[serde(skip_serializing_if = "Option::is_none")]
308 currencies: Option<String>,
309 #[serde(
310 skip_serializing_if = "Option::is_none",
311 serialize_with = "unix_seconds_opt"
312 )]
313 from_date: Option<OffsetDateTime>,
314 #[serde(
315 skip_serializing_if = "Option::is_none",
316 serialize_with = "unix_seconds_opt"
317 )]
318 to_date: Option<OffsetDateTime>,
319}
320
321impl GetDepositsOptions {
322 #[inline]
324 pub fn new() -> Self {
325 Self::default()
326 }
327
328 #[inline]
330 #[must_use]
331 pub fn page(self, page: i32) -> Self {
332 Self {
333 page: Some(page),
334 ..self
335 }
336 }
337
338 #[inline]
345 #[must_use]
346 pub fn size(self, size: i32) -> Self {
347 let size = if size <= 0 {
348 MAX_DEPOSITS_PAGE_SIZE
349 } else {
350 size.min(MAX_DEPOSITS_PAGE_SIZE)
351 };
352 Self {
353 size: Some(size),
354 ..self
355 }
356 }
357
358 pub(crate) fn page_size(&self) -> i32 {
360 self.size.unwrap_or(MAX_DEPOSITS_PAGE_SIZE)
361 }
362
363 #[must_use]
366 pub fn states(self, states: impl IntoIterator<Item = DepositState>) -> Self {
367 let codes = states
368 .into_iter()
369 .filter(|s| *s != DepositState::Unknown)
370 .map(|s| i32::from(s).to_string())
371 .collect::<Vec<_>>();
372 Self {
373 states: (!codes.is_empty()).then(|| codes.join(",")),
374 ..self
375 }
376 }
377
378 #[must_use]
380 pub fn currencies<I, T>(self, currencies: I) -> Self
381 where
382 I: IntoIterator<Item = T>,
383 T: Into<String>,
384 {
385 let currencies = currencies
386 .into_iter()
387 .map(|c| c.into().trim().to_uppercase())
388 .filter(|c| !c.is_empty())
389 .collect::<Vec<_>>();
390 Self {
391 currencies: (!currencies.is_empty()).then(|| currencies.join(",")),
392 ..self
393 }
394 }
395
396 #[inline]
398 #[must_use]
399 pub fn start(self, start: OffsetDateTime) -> Self {
400 Self {
401 from_date: Some(start),
402 ..self
403 }
404 }
405
406 #[inline]
408 #[must_use]
409 pub fn end(self, end: OffsetDateTime) -> Self {
410 Self {
411 to_date: Some(end),
412 ..self
413 }
414 }
415}
416
417const DEFAULT_WITHDRAWALS_PAGE_SIZE: i32 = 20;
419
420const MAX_WITHDRAWALS_PAGE_SIZE: i32 = 50;
422
423#[derive(Debug, Default, Serialize, Clone)]
425pub struct GetWithdrawalsOptions {
426 #[serde(skip_serializing_if = "Option::is_none")]
427 page: Option<i32>,
428 #[serde(skip_serializing_if = "Option::is_none")]
429 size: Option<i32>,
430 #[serde(
431 skip_serializing_if = "Option::is_none",
432 serialize_with = "unix_seconds_opt"
433 )]
434 begin: Option<OffsetDateTime>,
435 #[serde(
436 skip_serializing_if = "Option::is_none",
437 serialize_with = "unix_seconds_opt"
438 )]
439 end: Option<OffsetDateTime>,
440}
441
442impl GetWithdrawalsOptions {
443 #[inline]
445 pub fn new() -> Self {
446 Self::default()
447 }
448
449 #[inline]
451 #[must_use]
452 pub fn page(self, page: i32) -> Self {
453 Self {
454 page: Some(page),
455 ..self
456 }
457 }
458
459 #[inline]
462 #[must_use]
463 pub fn size(self, size: i32) -> Self {
464 let size = if size <= 0 {
465 DEFAULT_WITHDRAWALS_PAGE_SIZE
466 } else {
467 size.min(MAX_WITHDRAWALS_PAGE_SIZE)
468 };
469 Self {
470 size: Some(size),
471 ..self
472 }
473 }
474
475 pub(crate) fn page_size(&self) -> i32 {
477 self.size.unwrap_or(DEFAULT_WITHDRAWALS_PAGE_SIZE)
478 }
479
480 #[inline]
482 #[must_use]
483 pub fn start(self, start: OffsetDateTime) -> Self {
484 Self {
485 begin: Some(start),
486 ..self
487 }
488 }
489
490 #[inline]
492 #[must_use]
493 pub fn end(self, end: OffsetDateTime) -> Self {
494 Self {
495 end: Some(end),
496 ..self
497 }
498 }
499}