Skip to main content

rust_okx/api/finance/requests/
common.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5/// Request for [`get_saving_balance`](crate::api::finance::Savings::get_saving_balance).
6#[derive(Debug, Clone, Default, Serialize)]
7pub struct CurrencyRequest<'a> {
8    #[serde(skip_serializing_if = "Option::is_none")]
9    ccy: Option<Cow<'a, str>>,
10}
11
12impl<'a> CurrencyRequest<'a> {
13    /// Create an unfiltered currency query.
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    /// Set the currency filter.
19    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
20        self.ccy = Some(ccy.into());
21        self
22    }
23}
24
25/// Request for [`set_lending_rate`](crate::api::finance::Savings::set_lending_rate).
26#[derive(Debug, Clone, Serialize)]
27pub struct SetLendingRateRequest<'a> {
28    ccy: Cow<'a, str>,
29    rate: Cow<'a, str>,
30}
31
32impl<'a> SetLendingRateRequest<'a> {
33    /// Create a lending-rate update.
34    pub fn new(ccy: impl Into<Cow<'a, str>>, rate: impl Into<Cow<'a, str>>) -> Self {
35        Self {
36            ccy: ccy.into(),
37            rate: rate.into(),
38        }
39    }
40}
41
42/// Request for purchase/redeem operations on staking products.
43#[derive(Debug, Clone, Serialize)]
44pub struct AmountRequest<'a> {
45    amt: Cow<'a, str>,
46}
47
48impl<'a> AmountRequest<'a> {
49    /// Create an amount request.
50    pub fn new(amt: impl Into<Cow<'a, str>>) -> Self {
51        Self { amt: amt.into() }
52    }
53}
54
55/// Request for `cancel_redeem` on staking products.
56#[derive(Debug, Clone, Serialize)]
57#[serde(rename_all = "camelCase")]
58pub struct CancelRedeemRequest<'a> {
59    ord_id: Cow<'a, str>,
60}
61
62impl<'a> CancelRedeemRequest<'a> {
63    /// Create a cancel-redeem request.
64    pub fn new(ord_id: impl Into<Cow<'a, str>>) -> Self {
65        Self {
66            ord_id: ord_id.into(),
67        }
68    }
69}
70
71/// Request for `apy_history` on staking products.
72#[derive(Debug, Clone, Serialize)]
73pub struct ApyHistoryRequest<'a> {
74    days: Cow<'a, str>,
75}
76
77impl<'a> ApyHistoryRequest<'a> {
78    /// Create an APY history query for the given number of days.
79    pub fn new(days: impl Into<Cow<'a, str>>) -> Self {
80        Self { days: days.into() }
81    }
82}
83
84/// Common currency and cursor pagination query used by finance history endpoints.
85#[derive(Debug, Clone, Default, Serialize)]
86pub struct FinanceHistoryRequest<'a> {
87    #[serde(skip_serializing_if = "Option::is_none")]
88    ccy: Option<Cow<'a, str>>,
89    #[serde(skip_serializing_if = "Option::is_none")]
90    after: Option<Cow<'a, str>>,
91    #[serde(skip_serializing_if = "Option::is_none")]
92    before: Option<Cow<'a, str>>,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    limit: Option<u32>,
95}
96
97impl<'a> FinanceHistoryRequest<'a> {
98    /// Create an unfiltered history query.
99    pub fn new() -> Self {
100        Self::default()
101    }
102
103    /// Restrict the result to one currency.
104    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
105        self.ccy = Some(ccy.into());
106        self
107    }
108
109    /// Set the endpoint-specific `after` cursor.
110    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
111        self.after = Some(after.into());
112        self
113    }
114
115    /// Set the endpoint-specific `before` cursor.
116    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
117        self.before = Some(before.into());
118        self
119    }
120
121    /// Set the number of records to return, from 1 through 100.
122    pub fn limit(mut self, limit: u32) -> Self {
123        self.limit = Some(limit);
124        self
125    }
126}