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