Skip to main content

rust_okx/api/public_data/requests/
edge.rs

1use std::borrow::Cow;
2
3use serde::Serialize;
4
5/// Query parameters for `GET /api/v5/public/opt-summary`.
6#[derive(Debug, Clone, Serialize)]
7pub struct OptionSummaryRequest<'a> {
8    #[serde(rename = "instFamily")]
9    inst_family: Cow<'a, str>,
10    #[serde(rename = "expTime", skip_serializing_if = "Option::is_none")]
11    exp_time: Option<Cow<'a, str>>,
12}
13
14impl<'a> OptionSummaryRequest<'a> {
15    /// Create a query for an option instrument family, such as `BTC-USD`.
16    pub fn new(inst_family: impl Into<Cow<'a, str>>) -> Self {
17        Self {
18            inst_family: inst_family.into(),
19            exp_time: None,
20        }
21    }
22
23    /// Restrict the result to one expiration timestamp in milliseconds.
24    pub fn expiration(mut self, value: impl Into<Cow<'a, str>>) -> Self {
25        self.exp_time = Some(value.into());
26        self
27    }
28}
29
30/// Empty query for `GET /api/v5/public/interest-rate-loan-quota`.
31#[derive(Debug, Clone, Default, Serialize)]
32pub struct InterestRateLoanQuotaRequest {}
33
34impl InterestRateLoanQuotaRequest {
35    /// Create the parameterless query.
36    pub fn new() -> Self {
37        Self::default()
38    }
39}
40
41/// Query parameters for `GET /api/v5/public/instrument-tick-bands`.
42#[derive(Debug, Clone, Serialize)]
43pub struct InstrumentTickBandsRequest<'a> {
44    #[serde(rename = "instType")]
45    inst_type: Cow<'a, str>,
46}
47
48impl<'a> InstrumentTickBandsRequest<'a> {
49    /// Create a tick-band query for `FUTURES` or `OPTION` instruments.
50    pub fn new(inst_type: impl Into<Cow<'a, str>>) -> Self {
51        Self {
52            inst_type: inst_type.into(),
53        }
54    }
55}
56
57/// Query parameters for `GET /api/v5/public/underlying`.
58#[derive(Debug, Clone, Serialize)]
59pub struct UnderlyingRequest<'a> {
60    #[serde(rename = "instType")]
61    inst_type: Cow<'a, str>,
62}
63
64impl<'a> UnderlyingRequest<'a> {
65    /// Create a query for `SWAP`, `FUTURES`, or `OPTION` instruments.
66    pub fn new(inst_type: impl Into<Cow<'a, str>>) -> Self {
67        Self {
68            inst_type: inst_type.into(),
69        }
70    }
71}
72
73/// Query parameters for `GET /api/v5/public/option-trades`.
74#[derive(Debug, Clone, Default, Serialize)]
75pub struct PublicOptionTradesRequest<'a> {
76    #[serde(rename = "instId", skip_serializing_if = "Option::is_none")]
77    inst_id: Option<Cow<'a, str>>,
78    #[serde(rename = "instFamily", skip_serializing_if = "Option::is_none")]
79    inst_family: Option<Cow<'a, str>>,
80    #[serde(rename = "optType", skip_serializing_if = "Option::is_none")]
81    option_type: Option<Cow<'a, str>>,
82}
83
84impl<'a> PublicOptionTradesRequest<'a> {
85    /// Create an unfiltered option-trades query.
86    pub fn new() -> Self {
87        Self::default()
88    }
89
90    /// Restrict results to one option instrument.
91    pub fn inst_id(mut self, value: impl Into<Cow<'a, str>>) -> Self {
92        self.inst_id = Some(value.into());
93        self
94    }
95
96    /// Restrict results to one option instrument family.
97    pub fn inst_family(mut self, value: impl Into<Cow<'a, str>>) -> Self {
98        self.inst_family = Some(value.into());
99        self
100    }
101
102    /// Restrict results to calls (`C`) or puts (`P`).
103    pub fn option_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
104        self.option_type = Some(value.into());
105        self
106    }
107}
108
109/// Query parameters for `GET /api/v5/public/market-data-history`.
110#[derive(Debug, Clone, Default, Serialize)]
111pub struct MarketDataHistoryRequest<'a> {
112    #[serde(skip_serializing_if = "Option::is_none")]
113    module: Option<Cow<'a, str>>,
114    #[serde(rename = "instType", skip_serializing_if = "Option::is_none")]
115    inst_type: Option<Cow<'a, str>>,
116    #[serde(rename = "instIdList", skip_serializing_if = "Option::is_none")]
117    inst_id_list: Option<Cow<'a, str>>,
118    #[serde(rename = "dateAggrType", skip_serializing_if = "Option::is_none")]
119    date_aggr_type: Option<Cow<'a, str>>,
120    #[serde(skip_serializing_if = "Option::is_none")]
121    begin: Option<Cow<'a, str>>,
122    #[serde(skip_serializing_if = "Option::is_none")]
123    end: Option<Cow<'a, str>>,
124}
125
126impl<'a> MarketDataHistoryRequest<'a> {
127    /// Create an empty market-data-history query.
128    pub fn new() -> Self {
129        Self::default()
130    }
131
132    /// Set the OKX market-data module identifier.
133    pub fn module(mut self, value: impl Into<Cow<'a, str>>) -> Self {
134        self.module = Some(value.into());
135        self
136    }
137
138    /// Set the instrument type filter.
139    pub fn inst_type(mut self, value: impl Into<Cow<'a, str>>) -> Self {
140        self.inst_type = Some(value.into());
141        self
142    }
143
144    /// Set a comma-separated instrument-ID list.
145    pub fn inst_id_list(mut self, value: impl Into<Cow<'a, str>>) -> Self {
146        self.inst_id_list = Some(value.into());
147        self
148    }
149
150    /// Set the documented date aggregation type.
151    pub fn date_aggregation(mut self, value: impl Into<Cow<'a, str>>) -> Self {
152        self.date_aggr_type = Some(value.into());
153        self
154    }
155
156    /// Set the inclusive begin timestamp in milliseconds.
157    pub fn begin(mut self, value: impl Into<Cow<'a, str>>) -> Self {
158        self.begin = Some(value.into());
159        self
160    }
161
162    /// Set the inclusive end timestamp in milliseconds.
163    pub fn end(mut self, value: impl Into<Cow<'a, str>>) -> Self {
164        self.end = Some(value.into());
165        self
166    }
167}
168
169#[cfg(test)]
170mod tests {
171    use super::*;
172
173    #[test]
174    fn market_history_uses_okx_field_names() {
175        let request = MarketDataHistoryRequest::new()
176            .module("2")
177            .inst_type("SPOT")
178            .inst_id_list("BTC-USDT")
179            .date_aggregation("daily");
180        let value = serde_json::to_value(request).unwrap();
181        assert_eq!(value["instType"], "SPOT");
182        assert_eq!(value["instIdList"], "BTC-USDT");
183        assert_eq!(value["dateAggrType"], "daily");
184    }
185}