Skip to main content

rust_okx/api/public_data/
requests.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5use crate::model::InstType;
6
7/// Request for [`get_instruments`](crate::api::public_data::PublicData::get_instruments).
8#[derive(Debug, Clone, Serialize)]
9pub struct InstrumentsRequest<'a> {
10    #[serde(rename = "instType")]
11    inst_type: InstType,
12    #[serde(rename = "seriesId", skip_serializing_if = "Option::is_none")]
13    series_id: Option<Cow<'a, str>>,
14    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
15    inst_family: Option<Cow<'a, str>>,
16    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
17    inst_id: Option<Cow<'a, str>>,
18}
19
20impl<'a> InstrumentsRequest<'a> {
21    /// Create an instruments query for an instrument type.
22    pub fn new(inst_type: InstType) -> Self {
23        Self {
24            inst_type,
25            series_id: None,
26            inst_family: None,
27            inst_id: None,
28        }
29    }
30
31    /// Set the series ID filter, e.g. `BTC-ABOVE-DAILY`.
32    ///
33    /// Required when `instType` is `EVENTS`.
34    pub fn series_id(mut self, series_id: impl Into<Cow<'a, str>>) -> Self {
35        self.series_id = Some(series_id.into());
36        self
37    }
38
39    /// Set the instrument family filter.
40    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
41        self.inst_family = Some(inst_family.into());
42        self
43    }
44
45    /// Set the instrument ID filter.
46    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
47        self.inst_id = Some(inst_id.into());
48        self
49    }
50}
51
52/// Request for [`get_funding_rate`](crate::api::public_data::PublicData::get_funding_rate),
53/// [`get_price_limit`](crate::api::public_data::PublicData::get_price_limit), and
54/// [`get_estimated_price`](crate::api::public_data::PublicData::get_estimated_price).
55#[derive(Debug, Clone, Serialize)]
56pub struct InstIdRequest<'a> {
57    #[serde(rename = "instId")]
58    inst_id: Cow<'a, str>,
59}
60
61impl<'a> InstIdRequest<'a> {
62    /// Create a query for one instrument ID.
63    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
64        Self {
65            inst_id: inst_id.into(),
66        }
67    }
68}
69
70/// Request for [`get_discount_rate_interest_free_quota`](crate::api::public_data::PublicData::get_discount_rate_interest_free_quota).
71#[derive(Debug, Clone, Default, Serialize)]
72pub struct CurrencyRequest<'a> {
73    #[serde(skip_serializing_if = "Option::is_none")]
74    ccy: Option<Cow<'a, str>>,
75}
76
77impl<'a> CurrencyRequest<'a> {
78    /// Create an unfiltered currency query.
79    pub fn new() -> Self {
80        Self::default()
81    }
82
83    /// Set the currency filter.
84    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
85        self.ccy = Some(ccy.into());
86        self
87    }
88}
89
90mod edge;
91
92pub use edge::*;
93
94/// Query parameters for public endpoints filtered by instrument family.
95#[derive(Debug, Clone, Serialize)]
96pub struct InstrumentFamilyRequest<'a> {
97    #[serde(rename = "instType")]
98    inst_type: InstType,
99    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
100    underlying: Option<Cow<'a, str>>,
101    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
102    inst_id: Option<Cow<'a, str>>,
103    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
104    inst_family: Option<Cow<'a, str>>,
105}
106
107impl<'a> InstrumentFamilyRequest<'a> {
108    /// Create a query for an instrument type.
109    pub fn new(inst_type: InstType) -> Self {
110        Self {
111            inst_type,
112            underlying: None,
113            inst_id: None,
114            inst_family: None,
115        }
116    }
117
118    /// Set the underlying filter.
119    pub fn underlying(mut self, underlying: impl Into<Cow<'a, str>>) -> Self {
120        self.underlying = Some(underlying.into());
121        self
122    }
123
124    /// Set the instrument ID filter.
125    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
126        self.inst_id = Some(inst_id.into());
127        self
128    }
129
130    /// Set the instrument family filter.
131    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
132        self.inst_family = Some(inst_family.into());
133        self
134    }
135}
136
137/// Query parameters for funding-rate history.
138#[derive(Debug, Clone, Serialize)]
139pub struct FundingRateHistoryRequest<'a> {
140    #[serde(rename = "instId")]
141    inst_id: Cow<'a, str>,
142    #[serde(skip_serializing_if = "Option::is_none")]
143    after: Option<Cow<'a, str>>,
144    #[serde(skip_serializing_if = "Option::is_none")]
145    before: Option<Cow<'a, str>>,
146    #[serde(skip_serializing_if = "Option::is_none")]
147    limit: Option<u32>,
148}
149
150impl<'a> FundingRateHistoryRequest<'a> {
151    /// Create a funding-rate history query.
152    pub fn new(inst_id: impl Into<Cow<'a, str>>) -> Self {
153        Self {
154            inst_id: inst_id.into(),
155            after: None,
156            before: None,
157            limit: None,
158        }
159    }
160
161    /// Return records after this pagination cursor.
162    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
163        self.after = Some(after.into());
164        self
165    }
166
167    /// Return records before this pagination cursor.
168    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
169        self.before = Some(before.into());
170        self
171    }
172
173    /// Set the maximum number of rows to return.
174    pub fn limit(mut self, limit: u32) -> Self {
175        self.limit = Some(limit);
176        self
177    }
178}
179
180/// Query parameters for delivery/exercise history.
181#[derive(Debug, Clone, Serialize)]
182pub struct DeliveryExerciseHistoryRequest<'a> {
183    #[serde(rename = "instType")]
184    inst_type: InstType,
185    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
186    underlying: Option<Cow<'a, str>>,
187    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
188    inst_family: Option<Cow<'a, str>>,
189    #[serde(skip_serializing_if = "Option::is_none")]
190    after: Option<Cow<'a, str>>,
191    #[serde(skip_serializing_if = "Option::is_none")]
192    before: Option<Cow<'a, str>>,
193    #[serde(skip_serializing_if = "Option::is_none")]
194    limit: Option<u32>,
195}
196
197impl<'a> DeliveryExerciseHistoryRequest<'a> {
198    /// Create a delivery/exercise history query.
199    pub fn new(inst_type: InstType) -> Self {
200        Self {
201            inst_type,
202            underlying: None,
203            inst_family: None,
204            after: None,
205            before: None,
206            limit: None,
207        }
208    }
209
210    /// Set the underlying filter.
211    pub fn underlying(mut self, underlying: impl Into<Cow<'a, str>>) -> Self {
212        self.underlying = Some(underlying.into());
213        self
214    }
215
216    /// Set the instrument family filter.
217    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
218        self.inst_family = Some(inst_family.into());
219        self
220    }
221
222    /// Return records after this pagination cursor.
223    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
224        self.after = Some(after.into());
225        self
226    }
227
228    /// Return records before this pagination cursor.
229    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
230        self.before = Some(before.into());
231        self
232    }
233
234    /// Set the maximum number of rows to return.
235    pub fn limit(mut self, limit: u32) -> Self {
236        self.limit = Some(limit);
237        self
238    }
239}
240
241/// Query parameters for public position tiers.
242#[derive(Debug, Clone, Serialize)]
243pub struct PositionTiersRequest<'a> {
244    #[serde(rename = "instType")]
245    inst_type: InstType,
246    #[serde(rename = "tdMode")]
247    td_mode: Cow<'a, str>,
248    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
249    underlying: Option<Cow<'a, str>>,
250    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
251    inst_id: Option<Cow<'a, str>>,
252    #[serde(skip_serializing_if = "Option::is_none")]
253    ccy: Option<Cow<'a, str>>,
254    #[serde(skip_serializing_if = "Option::is_none")]
255    tier: Option<Cow<'a, str>>,
256    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
257    inst_family: Option<Cow<'a, str>>,
258}
259
260impl<'a> PositionTiersRequest<'a> {
261    /// Create a position-tiers query.
262    pub fn new(inst_type: InstType, td_mode: impl Into<Cow<'a, str>>) -> Self {
263        Self {
264            inst_type,
265            td_mode: td_mode.into(),
266            underlying: None,
267            inst_id: None,
268            ccy: None,
269            tier: None,
270            inst_family: None,
271        }
272    }
273
274    /// Set the underlying filter.
275    pub fn underlying(mut self, underlying: impl Into<Cow<'a, str>>) -> Self {
276        self.underlying = Some(underlying.into());
277        self
278    }
279
280    /// Set the instrument ID filter.
281    pub fn inst_id(mut self, inst_id: impl Into<Cow<'a, str>>) -> Self {
282        self.inst_id = Some(inst_id.into());
283        self
284    }
285
286    /// Set the currency filter.
287    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
288        self.ccy = Some(ccy.into());
289        self
290    }
291
292    /// Set the tier filter.
293    pub fn tier(mut self, tier: impl Into<Cow<'a, str>>) -> Self {
294        self.tier = Some(tier.into());
295        self
296    }
297
298    /// Set the instrument family filter.
299    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
300        self.inst_family = Some(inst_family.into());
301        self
302    }
303}
304
305/// Query parameters for insurance fund snapshots.
306#[derive(Debug, Clone, Serialize)]
307pub struct InsuranceFundRequest<'a> {
308    #[serde(rename = "instType")]
309    inst_type: InstType,
310    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
311    fund_type: Option<Cow<'a, str>>,
312    #[serde(rename = "uly", skip_serializing_if = "Option::is_none")]
313    underlying: Option<Cow<'a, str>>,
314    #[serde(skip_serializing_if = "Option::is_none")]
315    ccy: Option<Cow<'a, str>>,
316    #[serde(skip_serializing_if = "Option::is_none")]
317    before: Option<Cow<'a, str>>,
318    #[serde(skip_serializing_if = "Option::is_none")]
319    after: Option<Cow<'a, str>>,
320    #[serde(skip_serializing_if = "Option::is_none")]
321    limit: Option<u32>,
322    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
323    inst_family: Option<Cow<'a, str>>,
324}
325
326impl<'a> InsuranceFundRequest<'a> {
327    /// Create an insurance fund query.
328    pub fn new(inst_type: InstType) -> Self {
329        Self {
330            inst_type,
331            fund_type: None,
332            underlying: None,
333            ccy: None,
334            before: None,
335            after: None,
336            limit: None,
337            inst_family: None,
338        }
339    }
340
341    /// Set the OKX fund type filter.
342    pub fn fund_type(mut self, fund_type: impl Into<Cow<'a, str>>) -> Self {
343        self.fund_type = Some(fund_type.into());
344        self
345    }
346
347    /// Set the underlying filter.
348    pub fn underlying(mut self, underlying: impl Into<Cow<'a, str>>) -> Self {
349        self.underlying = Some(underlying.into());
350        self
351    }
352
353    /// Set the currency filter.
354    pub fn currency(mut self, ccy: impl Into<Cow<'a, str>>) -> Self {
355        self.ccy = Some(ccy.into());
356        self
357    }
358
359    /// Return records before this pagination cursor.
360    pub fn before(mut self, before: impl Into<Cow<'a, str>>) -> Self {
361        self.before = Some(before.into());
362        self
363    }
364
365    /// Return records after this pagination cursor.
366    pub fn after(mut self, after: impl Into<Cow<'a, str>>) -> Self {
367        self.after = Some(after.into());
368        self
369    }
370
371    /// Set the maximum number of rows to return.
372    pub fn limit(mut self, limit: u32) -> Self {
373        self.limit = Some(limit);
374        self
375    }
376
377    /// Set the instrument family filter.
378    pub fn inst_family(mut self, inst_family: impl Into<Cow<'a, str>>) -> Self {
379        self.inst_family = Some(inst_family.into());
380        self
381    }
382}
383
384/// Query parameters for contract/coin conversion.
385#[derive(Debug, Clone, Serialize)]
386pub struct ConvertContractCoinRequest<'a> {
387    #[serde(rename = "type")]
388    conversion_type: Cow<'a, str>,
389    #[serde(rename = "instId")]
390    inst_id: Cow<'a, str>,
391    sz: Cow<'a, str>,
392    #[serde(skip_serializing_if = "Option::is_none")]
393    px: Option<Cow<'a, str>>,
394    #[serde(skip_serializing_if = "Option::is_none")]
395    unit: Option<Cow<'a, str>>,
396}
397
398impl<'a> ConvertContractCoinRequest<'a> {
399    /// Create a contract/coin conversion query.
400    pub fn new(
401        conversion_type: impl Into<Cow<'a, str>>,
402        inst_id: impl Into<Cow<'a, str>>,
403        sz: impl Into<Cow<'a, str>>,
404    ) -> Self {
405        Self {
406            conversion_type: conversion_type.into(),
407            inst_id: inst_id.into(),
408            sz: sz.into(),
409            px: None,
410            unit: None,
411        }
412    }
413
414    /// Set the price used for conversion.
415    pub fn price(mut self, px: impl Into<Cow<'a, str>>) -> Self {
416        self.px = Some(px.into());
417        self
418    }
419
420    /// Set the unit used for conversion.
421    pub fn unit(mut self, unit: impl Into<Cow<'a, str>>) -> Self {
422        self.unit = Some(unit.into());
423        self
424    }
425}