Skip to main content

rust_okx/api/public_data/
requests.rs

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