Skip to main content

rust_okx/api/account/requests/
borrowing.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::model::TradeMode;
6
7/// Query parameters for maximum loan.
8#[derive(Debug, Clone, Serialize)]
9pub struct MaxLoanRequest<'a> {
10    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
11    inst_id: Option<Cow<'a, str>>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    ccy: Option<Cow<'a, str>>,
14    #[serde(rename = "mgnMode")]
15    mgn_mode: TradeMode,
16    #[serde(rename = "mgnCcy", skip_serializing_if = "Option::is_none")]
17    mgn_ccy: Option<Cow<'a, str>>,
18    #[serde(rename = "tradeQuoteCcy", skip_serializing_if = "Option::is_none")]
19    trade_quote_ccy: Option<Cow<'a, str>>,
20}
21
22impl<'a> MaxLoanRequest<'a> {
23    /// Create an instrument-based maximum-loan query.
24    ///
25    /// `inst_id` may contain one to five comma-separated instrument IDs, as
26    /// documented by OKX. Use [`Self::by_currency`] for Spot-mode manual-borrow
27    /// quota queries.
28    pub fn new(inst_id: impl Into<Cow<'a, str>>, mgn_mode: TradeMode) -> Self {
29        Self::by_instrument(inst_id, mgn_mode)
30    }
31
32    /// Create an instrument-based maximum-loan query.
33    pub fn by_instrument(inst_id: impl Into<Cow<'a, str>>, mgn_mode: TradeMode) -> Self {
34        Self {
35            mgn_mode,
36            inst_id: Some(inst_id.into()),
37            ccy: None,
38            mgn_ccy: None,
39            trade_quote_ccy: None,
40        }
41    }
42
43    /// Create a currency-based Spot-mode manual-borrow quota query.
44    pub fn by_currency(ccy: impl Into<Cow<'a, str>>) -> Self {
45        Self {
46            mgn_mode: TradeMode::Cross,
47            inst_id: None,
48            ccy: Some(ccy.into()),
49            mgn_ccy: None,
50            trade_quote_ccy: None,
51        }
52    }
53
54    /// Replace the selector with a currency-based Spot-mode query.
55    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
56        self.inst_id = None;
57        self.ccy = Some(ccy.into());
58        self
59    }
60
61    /// Set the margin currency.
62    pub fn margin_currency(mut self, mgn_ccy: impl Into<Cow<'a, str>>) -> Self {
63        self.mgn_ccy = Some(mgn_ccy.into());
64        self
65    }
66
67    /// Set the trade quote currency.
68    pub fn trade_quote_currency(mut self, trade_quote_ccy: impl Into<Cow<'a, str>>) -> Self {
69        self.trade_quote_ccy = Some(trade_quote_ccy.into());
70        self
71    }
72}
73
74/// Query parameters for interest-accrued records.
75#[derive(Debug, Clone, Default, Serialize)]
76pub struct InterestAccruedRequest<'a> {
77    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
78    inst_id: Option<Cow<'a, str>>,
79    #[serde(skip_serializing_if = "Option::is_none")]
80    ccy: Option<Cow<'a, str>>,
81    #[serde(rename = "mgnMode", skip_serializing_if = "Option::is_none")]
82    mgn_mode: Option<TradeMode>,
83    #[serde(skip_serializing_if = "Option::is_none")]
84    after: Option<Cow<'a, str>>,
85    #[serde(skip_serializing_if = "Option::is_none")]
86    before: Option<Cow<'a, str>>,
87    #[serde(skip_serializing_if = "Option::is_none")]
88    limit: Option<u32>,
89}
90
91impl<'a> InterestAccruedRequest<'a> {
92    /// Create an empty interest-accrued query.
93    pub fn new() -> Self {
94        Self::default()
95    }
96
97    /// Set the instrument ID filter.
98    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
99        self.inst_id = Some(inst_id.into());
100        self
101    }
102
103    /// Set the currency filter.
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 margin mode filter.
110    pub fn margin_mode(mut self, mgn_mode: TradeMode) -> Self {
111        self.mgn_mode = Some(mgn_mode);
112        self
113    }
114
115    /// Return records after this pagination cursor.
116    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
117        self.after = Some(after.into());
118        self
119    }
120
121    /// Return records before this pagination cursor.
122    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
123        self.before = Some(before.into());
124        self
125    }
126
127    /// Set the maximum number of rows to return.
128    pub fn limit(mut self, limit: u32) -> Self {
129        self.limit = Some(limit);
130        self
131    }
132}
133
134/// Request body for borrow/repay.
135#[derive(Debug, Clone, Serialize)]
136pub struct BorrowRepayRequest<'a> {
137    ccy: Cow<'a, str>,
138    side: Cow<'a, str>,
139    amt: Cow<'a, str>,
140    #[serde(rename = "ordId", skip_serializing_if = "Option::is_none")]
141    ord_id: Option<Cow<'a, str>>,
142}
143
144impl<'a> BorrowRepayRequest<'a> {
145    /// Create a borrow/repay request.
146    pub fn new(
147        ccy: impl Into<Cow<'a, str>>,
148        side: impl Into<Cow<'a, str>>,
149        amt: impl Into<Cow<'a, str>>,
150    ) -> Self {
151        Self {
152            ccy: ccy.into(),
153            side: side.into(),
154            amt: amt.into(),
155            ord_id: None,
156        }
157    }
158
159    /// Set the related order ID.
160    pub fn order_id(mut self, ord_id: impl Into<Cow<'a, str>>) -> Self {
161        self.ord_id = Some(ord_id.into());
162        self
163    }
164}
165
166/// Query parameters for borrow/repay history.
167#[derive(Debug, Clone, Default, Serialize)]
168pub struct BorrowRepayHistoryRequest<'a> {
169    #[serde(skip_serializing_if = "Option::is_none")]
170    ccy: Option<Cow<'a, str>>,
171    #[serde(skip_serializing_if = "Option::is_none")]
172    after: Option<Cow<'a, str>>,
173    #[serde(skip_serializing_if = "Option::is_none")]
174    before: Option<Cow<'a, str>>,
175    #[serde(skip_serializing_if = "Option::is_none")]
176    limit: Option<u32>,
177}
178
179impl<'a> BorrowRepayHistoryRequest<'a> {
180    /// Create an empty borrow/repay-history query.
181    pub fn new() -> Self {
182        Self::default()
183    }
184
185    /// Set the currency filter.
186    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
187        self.ccy = Some(ccy.into());
188        self
189    }
190
191    /// Return records after this pagination cursor.
192    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
193        self.after = Some(after.into());
194        self
195    }
196
197    /// Return records before this pagination cursor.
198    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
199        self.before = Some(before.into());
200        self
201    }
202
203    /// Set the maximum number of rows to return.
204    pub fn limit(mut self, limit: u32) -> Self {
205        self.limit = Some(limit);
206        self
207    }
208}
209
210/// Query parameters for interest limits.
211#[derive(Debug, Clone, Default, Serialize)]
212pub struct InterestLimitsRequest<'a> {
213    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
214    limit_type: Option<Cow<'a, str>>,
215    #[serde(skip_serializing_if = "Option::is_none")]
216    ccy: Option<Cow<'a, str>>,
217}
218
219impl<'a> InterestLimitsRequest<'a> {
220    /// Create an empty interest-limits query.
221    pub fn new() -> Self {
222        Self::default()
223    }
224
225    /// Set the OKX interest-limit type.
226    pub fn limit_type(mut self, limit_type: impl Into<Cow<'a, str>>) -> Self {
227        self.limit_type = Some(limit_type.into());
228        self
229    }
230
231    /// Set the currency filter.
232    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
233        self.ccy = Some(ccy.into());
234        self
235    }
236}