Skip to main content

longbridge/asset/requests/
get_account_records.rs

1use 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/// Options for the stock transfer-in and transfer-out lists
17///
18/// `S` is the state enum of the list: use [`GetStockTransfersInOptions`] for
19/// [`crate::AssetContext::stock_transfers_in`] and
20/// [`GetStockTransfersOutOptions`] for
21/// [`crate::AssetContext::stock_transfers_out`]. The two lists use different
22/// state codes, so a state of one list cannot be passed to the other.
23#[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
55/// The state enum of a stock transfer list: [`StockTransferInState`] or
56/// [`StockTransferOutState`] (sealed)
57pub trait StockTransferStateFilter:
58    Serialize + Default + PartialEq + Copy + sealed::Sealed
59{
60}
61
62impl StockTransferStateFilter for StockTransferInState {}
63impl StockTransferStateFilter for StockTransferOutState {}
64
65/// Options for [`crate::AssetContext::stock_transfers_in`]
66pub type GetStockTransfersInOptions = GetStockTransfersOptions<StockTransferInState>;
67
68/// Options for [`crate::AssetContext::stock_transfers_out`]
69pub type GetStockTransfersOutOptions = GetStockTransfersOptions<StockTransferOutState>;
70
71impl<S> GetStockTransfersOptions<S>
72where
73    S: StockTransferStateFilter,
74{
75    /// Create new options (no filters)
76    #[inline]
77    pub fn new() -> Self {
78        Self::default()
79    }
80
81    /// Filter by market (`HK`, `US`, `CN`, `SG`)
82    #[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    /// Filter by state; it is sent as the state's server code. The `Unknown`
92    /// state is not a server code, so it clears the filter instead.
93    #[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    /// Only applications created at or after this time
103    #[inline]
104    #[must_use]
105    pub fn start(self, start: OffsetDateTime) -> Self {
106        Self {
107            start: Some(start),
108            ..self
109        }
110    }
111
112    /// Only applications created at or before this time
113    #[inline]
114    #[must_use]
115    pub fn end(self, end: OffsetDateTime) -> Self {
116        Self {
117            end: Some(end),
118            ..self
119        }
120    }
121
122    /// Set the page number (default 1)
123    #[inline]
124    #[must_use]
125    pub fn page(self, page: i32) -> Self {
126        Self {
127            page: Some(page),
128            ..self
129        }
130    }
131
132    /// Set the page size (default 20, max 50)
133    #[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/// Options for the reward record list
144#[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    /// Create a new `GetRewardRecordsOptions`
156    #[inline]
157    pub fn new() -> Self {
158        Self::default()
159    }
160
161    /// Set the page number (default 1)
162    #[inline]
163    #[must_use]
164    pub fn page(self, page: i32) -> Self {
165        Self {
166            page: Some(page),
167            ..self
168        }
169    }
170
171    /// Set the page size (max 50)
172    #[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    /// Filter by reward type (default: all types). [`RewardType::Unknown`] is
182    /// not a server code, so it clears the filter instead.
183    #[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/// Options for the coupon list
194#[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
206/// `card_status` filter: `-1` lists coupons that have not expired, `2` the
207/// expired ones.
208fn 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    /// Create a new `GetCouponsOptions` (lists coupons that have not
217    /// expired)
218    #[inline]
219    pub fn new() -> Self {
220        Self::default()
221    }
222
223    /// Set the page number (default 1)
224    #[inline]
225    #[must_use]
226    pub fn page(self, page: i32) -> Self {
227        Self {
228            page: Some(page),
229            ..self
230        }
231    }
232
233    /// Set the page size (max 50)
234    #[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    /// List expired coupons instead of the ones that have not expired
244    #[inline]
245    #[must_use]
246    pub fn expired(self, expired: bool) -> Self {
247        Self {
248            card_status: expired,
249            ..self
250        }
251    }
252
253    /// Filter by coupon type (e.g. `commission`, `platform_fee_deduct`,
254    /// `stock`, `quote`, `cash`; default: all types)
255    #[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/// Which orders of a coupon to list
266#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
267pub enum CouponOrderType {
268    /// Orders the coupon is locked to (in progress)
269    Locked,
270    /// Orders whose rebate has been settled
271    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
295/// Largest page size the deposit list honours, and the default.
296pub(crate) const MAX_DEPOSITS_PAGE_SIZE: i32 = 20;
297
298/// Options for the deposit list
299#[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    /// Create a new `GetDepositsOptions`
323    #[inline]
324    pub fn new() -> Self {
325        Self::default()
326    }
327
328    /// Set the page number (default 1)
329    #[inline]
330    #[must_use]
331    pub fn page(self, page: i32) -> Self {
332        Self {
333            page: Some(page),
334            ..self
335        }
336    }
337
338    /// Set the page size (default 20, max 20; larger values are sent as 20,
339    /// and 0 or a negative value as the default 20)
340    ///
341    /// The deposit list honours a page size of at most 20: a larger one
342    /// silently returns a 10-record page, which would read as the last page
343    /// and hide records, so it is clamped here.
344    #[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    /// The page size sent: the one set, or the default 20.
359    pub(crate) fn page_size(&self) -> i32 {
360        self.size.unwrap_or(MAX_DEPOSITS_PAGE_SIZE)
361    }
362
363    /// Filter by states (sent as their numeric codes;
364    /// [`DepositState::Unknown`] is ignored)
365    #[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    /// Filter by currencies (e.g. `HKD`, `USD`)
379    #[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    /// Only deposits applied at or after this time
397    #[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    /// Only deposits applied at or before this time
407    #[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
417/// Default page size of the withdrawal list.
418const DEFAULT_WITHDRAWALS_PAGE_SIZE: i32 = 20;
419
420/// Largest page size of the withdrawal list.
421const MAX_WITHDRAWALS_PAGE_SIZE: i32 = 50;
422
423/// Options for the withdrawal list
424#[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    /// Create a new `GetWithdrawalsOptions`
444    #[inline]
445    pub fn new() -> Self {
446        Self::default()
447    }
448
449    /// Set the page number (default 1)
450    #[inline]
451    #[must_use]
452    pub fn page(self, page: i32) -> Self {
453        Self {
454            page: Some(page),
455            ..self
456        }
457    }
458
459    /// Set the page size (default 20, max 50; larger values are sent as 50,
460    /// and 0 or a negative value as the default 20)
461    #[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    /// The page size sent: the one set, or the default 20.
476    pub(crate) fn page_size(&self) -> i32 {
477        self.size.unwrap_or(DEFAULT_WITHDRAWALS_PAGE_SIZE)
478    }
479
480    /// Only withdrawals applied at or after this time
481    #[inline]
482    #[must_use]
483    pub fn start(self, start: OffsetDateTime) -> Self {
484        Self {
485            begin: Some(start),
486            ..self
487        }
488    }
489
490    /// Only withdrawals applied at or before this time
491    #[inline]
492    #[must_use]
493    pub fn end(self, end: OffsetDateTime) -> Self {
494        Self {
495            end: Some(end),
496            ..self
497        }
498    }
499}