Skip to main content

rust_okx/api/account/requests/
history.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::model::{InstType, TradeMode};
6
7/// Query parameters for account bills.
8#[derive(Debug, Clone, Default, Serialize)]
9pub struct BillsRequest<'a> {
10    #[serde(rename = "instType", skip_serializing_if = "Option::is_none")]
11    inst_type: Option<InstType>,
12    #[serde(skip_serializing_if = "Option::is_none")]
13    ccy: Option<Cow<'a, str>>,
14    #[serde(rename = "mgnMode", skip_serializing_if = "Option::is_none")]
15    mgn_mode: Option<TradeMode>,
16    #[serde(rename = "ctType", skip_serializing_if = "Option::is_none")]
17    ct_type: Option<Cow<'a, str>>,
18    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
19    bill_type: Option<Cow<'a, str>>,
20    #[serde(rename = "subType", skip_serializing_if = "Option::is_none")]
21    sub_type: Option<Cow<'a, str>>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    after: Option<Cow<'a, str>>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    before: Option<Cow<'a, str>>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    begin: Option<Cow<'a, str>>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    end: Option<Cow<'a, str>>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    limit: Option<u32>,
32}
33
34impl<'a> BillsRequest<'a> {
35    /// Create an empty account-bills query.
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// Set the instrument type filter.
41    pub fn inst_type(mut self, inst_type: InstType) -> Self {
42        self.inst_type = Some(inst_type);
43        self
44    }
45
46    /// Set the currency filter.
47    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
48        self.ccy = Some(ccy.into());
49        self
50    }
51
52    /// Set the margin mode filter.
53    pub fn margin_mode(mut self, mgn_mode: TradeMode) -> Self {
54        self.mgn_mode = Some(mgn_mode);
55        self
56    }
57
58    /// Set the contract type filter.
59    pub fn contract_type(mut self, ct_type: impl Into<Cow<'a, str>>) -> Self {
60        self.ct_type = Some(ct_type.into());
61        self
62    }
63
64    /// Set the bill type filter.
65    pub fn bill_type(mut self, bill_type: impl Into<Cow<'a, str>>) -> Self {
66        self.bill_type = Some(bill_type.into());
67        self
68    }
69
70    /// Set the bill subtype filter.
71    pub fn sub_type(mut self, sub_type: impl Into<Cow<'a, str>>) -> Self {
72        self.sub_type = Some(sub_type.into());
73        self
74    }
75
76    /// Return records after this pagination cursor.
77    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
78        self.after = Some(after.into());
79        self
80    }
81
82    /// Return records before this pagination cursor.
83    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
84        self.before = Some(before.into());
85        self
86    }
87
88    /// Set the begin timestamp filter.
89    pub fn begin(mut self, begin: impl Into<Cow<'a, str>>) -> Self {
90        self.begin = Some(begin.into());
91        self
92    }
93
94    /// Set the end timestamp filter.
95    pub fn end(mut self, end: impl Into<Cow<'a, str>>) -> Self {
96        self.end = Some(end.into());
97        self
98    }
99
100    /// Set the maximum number of rows to return.
101    pub fn limit(mut self, limit: u32) -> Self {
102        self.limit = Some(limit);
103        self
104    }
105}
106
107/// Query parameters for archived account bills.
108#[derive(Debug, Clone, Default, Serialize)]
109pub struct BillsArchiveRequest<'a> {
110    #[serde(flatten)]
111    base: BillsRequest<'a>,
112    #[serde(skip_serializing_if = "Option::is_none")]
113    begin: Option<Cow<'a, str>>,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    end: Option<Cow<'a, str>>,
116}
117
118impl<'a> BillsArchiveRequest<'a> {
119    /// Create an empty archived-bills query.
120    pub fn new() -> Self {
121        Self::default()
122    }
123
124    /// Set the common bills filters.
125    pub fn filters(mut self, base: BillsRequest<'a>) -> Self {
126        self.base = base;
127        self
128    }
129
130    /// Set the begin timestamp.
131    pub fn begin(mut self, begin: impl Into<Cow<'a, str>>) -> Self {
132        self.begin = Some(begin.into());
133        self
134    }
135
136    /// Set the end timestamp.
137    pub fn end(mut self, end: impl Into<Cow<'a, str>>) -> Self {
138        self.end = Some(end.into());
139        self
140    }
141}
142
143/// Query parameters for account bill type/subtype mapping.
144#[derive(Debug, Clone, Default, Serialize)]
145pub struct BillSubtypesRequest<'a> {
146    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
147    bill_type: Option<Cow<'a, str>>,
148}
149
150impl<'a> BillSubtypesRequest<'a> {
151    /// Create an empty bill-subtypes query.
152    pub fn new() -> Self {
153        Self::default()
154    }
155
156    /// Filter by bill type. Multiple values can be separated by commas.
157    pub fn bill_type(mut self, bill_type: impl Into<Cow<'a, str>>) -> Self {
158        self.bill_type = Some(bill_type.into());
159        self
160    }
161}
162
163/// Quarter selector for historical account-bills archive generation.
164#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
165#[non_exhaustive]
166pub enum BillsHistoryArchiveQuarter {
167    /// First quarter.
168    #[serde(rename = "Q1")]
169    Q1,
170    /// Second quarter.
171    #[serde(rename = "Q2")]
172    Q2,
173    /// Third quarter.
174    #[serde(rename = "Q3")]
175    Q3,
176    /// Fourth quarter.
177    #[serde(rename = "Q4")]
178    Q4,
179}
180
181/// Parameters for historical account-bills archive generation and download-link lookup.
182#[derive(Debug, Clone, Serialize)]
183pub struct BillsHistoryArchiveRequest<'a> {
184    year: Cow<'a, str>,
185    quarter: BillsHistoryArchiveQuarter,
186    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
187    bill_type: Option<Cow<'a, str>>,
188}
189
190impl<'a> BillsHistoryArchiveRequest<'a> {
191    /// Create a request for a year and quarter.
192    pub fn new(year: impl Into<Cow<'a, str>>, quarter: BillsHistoryArchiveQuarter) -> Self {
193        Self {
194            year: year.into(),
195            quarter,
196            bill_type: None,
197        }
198    }
199
200    /// Filter by bill type. Multiple values can be separated by commas.
201    pub fn bill_type(mut self, bill_type: impl Into<Cow<'a, str>>) -> Self {
202        self.bill_type = Some(bill_type.into());
203        self
204    }
205}
206
207/// Request body for applying historical account-bills archive generation.
208pub type ApplyBillsHistoryArchiveRequest<'a> = BillsHistoryArchiveRequest<'a>;
209
210/// Query parameters for position history.
211#[derive(Debug, Clone, Default, Serialize)]
212pub struct PositionsHistoryRequest<'a> {
213    #[serde(rename = "instType", skip_serializing_if = "Option::is_none")]
214    inst_type: Option<InstType>,
215    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
216    inst_id: Option<Cow<'a, str>>,
217    #[serde(rename = "mgnMode", skip_serializing_if = "Option::is_none")]
218    mgn_mode: Option<TradeMode>,
219    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
220    close_type: Option<Cow<'a, str>>,
221    #[serde(rename = "posId", skip_serializing_if = "Option::is_none")]
222    pos_id: Option<Cow<'a, str>>,
223    #[serde(skip_serializing_if = "Option::is_none")]
224    after: Option<Cow<'a, str>>,
225    #[serde(skip_serializing_if = "Option::is_none")]
226    before: Option<Cow<'a, str>>,
227    #[serde(skip_serializing_if = "Option::is_none")]
228    limit: Option<u32>,
229}
230
231impl<'a> PositionsHistoryRequest<'a> {
232    /// Create an empty position-history query.
233    pub fn new() -> Self {
234        Self::default()
235    }
236
237    /// Set the instrument type filter.
238    pub fn inst_type(mut self, inst_type: InstType) -> Self {
239        self.inst_type = Some(inst_type);
240        self
241    }
242
243    /// Set the instrument ID filter.
244    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
245        self.inst_id = Some(inst_id.into());
246        self
247    }
248
249    /// Set the margin mode filter.
250    pub fn margin_mode(mut self, mgn_mode: TradeMode) -> Self {
251        self.mgn_mode = Some(mgn_mode);
252        self
253    }
254
255    /// Set the OKX close type filter.
256    pub fn close_type(mut self, close_type: impl Into<Cow<'a, str>>) -> Self {
257        self.close_type = Some(close_type.into());
258        self
259    }
260
261    /// Set the position ID filter.
262    pub fn position_id(mut self, pos_id: impl Into<Cow<'a, str>>) -> Self {
263        self.pos_id = Some(pos_id.into());
264        self
265    }
266
267    /// Return records after this pagination cursor.
268    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
269        self.after = Some(after.into());
270        self
271    }
272
273    /// Return records before this pagination cursor.
274    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
275        self.before = Some(before.into());
276        self
277    }
278
279    /// Set the maximum number of rows to return.
280    pub fn limit(mut self, limit: u32) -> Self {
281        self.limit = Some(limit);
282        self
283    }
284}