rust_okx/api/public_data/api.rs
1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::model::EmptyRequest;
4use crate::transport::Transport;
5
6use super::endpoints::*;
7use super::requests::*;
8use super::responses::*;
9
10/// Accessor for the public reference-data endpoints.
11///
12/// Obtain one via [`OkxClient::public_data`](crate::OkxClient::public_data).
13pub struct PublicData<'a, T> {
14 client: &'a OkxClient<T>,
15}
16
17impl<'a, T: Transport> PublicData<'a, T> {
18 pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
19 Self { client }
20 }
21
22 /// Retrieve the list of tradable instruments.
23 ///
24 /// `GET /api/v5/public/instruments`. Public.
25 ///
26 /// `inst_family` is required for `FUTURES`, `SWAP`, and `OPTION` and ignored for `SPOT`/`MARGIN`.
27 ///
28 /// # Errors
29 ///
30 /// Returns [`RestError::Okx`](crate::RestError::Okx) if OKX rejects the request, or
31 /// [`RestError::Transport`](crate::RestError::Transport)/[`RestError::Decode`](crate::RestError::Decode) on transport/parsing failure.
32 pub async fn get_instruments(
33 &self,
34 request: &InstrumentsRequest<'_>,
35 ) -> Result<Vec<Instrument>, Error> {
36 self.client.get(INSTRUMENTS, request, false).await
37 }
38
39 /// Retrieve OKX system time.
40 ///
41 /// `GET /api/v5/public/time`. Public.
42 ///
43 /// # Errors
44 ///
45 /// Returns [`RestError::Okx`](crate::RestError::Okx) if OKX rejects the request, or transport/decode
46 /// errors.
47 pub async fn get_system_time(&self) -> Result<Vec<SystemTime>, Error> {
48 self.client.get(SYSTEM_TIME, &EmptyRequest {}, false).await
49 }
50
51 /// Retrieve open interest.
52 ///
53 /// `GET /api/v5/public/open-interest`. Public.
54 ///
55 /// # Errors
56 ///
57 /// See [`get_system_time`](Self::get_system_time).
58 pub async fn get_open_interest(
59 &self,
60 request: &InstrumentFamilyRequest<'_>,
61 ) -> Result<Vec<OpenInterest>, Error> {
62 self.client.get(OPEN_INTEREST, request, false).await
63 }
64
65 /// Retrieve the current funding rate for a derivatives instrument.
66 ///
67 /// `GET /api/v5/public/funding-rate`. Public.
68 ///
69 /// # Errors
70 ///
71 /// See [`get_system_time`](Self::get_system_time).
72 pub async fn get_funding_rate(
73 &self,
74 request: &InstIdRequest<'_>,
75 ) -> Result<Vec<FundingRate>, Error> {
76 self.client.get(FUNDING_RATE, request, false).await
77 }
78
79 /// Retrieve historical funding rates.
80 ///
81 /// `GET /api/v5/public/funding-rate-history`. Public.
82 ///
83 /// # Errors
84 ///
85 /// See [`get_system_time`](Self::get_system_time).
86 pub async fn get_funding_rate_history(
87 &self,
88 request: &FundingRateHistoryRequest<'_>,
89 ) -> Result<Vec<FundingRateHistory>, Error> {
90 self.client.get(FUNDING_RATE_HISTORY, request, false).await
91 }
92
93 /// Retrieve the price limit for an instrument.
94 ///
95 /// `GET /api/v5/public/price-limit`. Public.
96 ///
97 /// # Errors
98 ///
99 /// See [`get_system_time`](Self::get_system_time).
100 pub async fn get_price_limit(
101 &self,
102 request: &InstIdRequest<'_>,
103 ) -> Result<Vec<PriceLimit>, Error> {
104 self.client.get(PRICE_LIMIT, request, false).await
105 }
106
107 /// Retrieve mark prices.
108 ///
109 /// `GET /api/v5/public/mark-price`. Public.
110 ///
111 /// # Errors
112 ///
113 /// See [`get_system_time`](Self::get_system_time).
114 pub async fn get_mark_price(
115 &self,
116 request: &InstrumentFamilyRequest<'_>,
117 ) -> Result<Vec<MarkPrice>, Error> {
118 self.client.get(MARK_PRICE, request, false).await
119 }
120
121 /// Retrieve delivery/exercise history.
122 ///
123 /// `GET /api/v5/public/delivery-exercise-history`. Public.
124 ///
125 /// # Errors
126 ///
127 /// See [`get_system_time`](Self::get_system_time).
128 pub async fn get_delivery_exercise_history(
129 &self,
130 request: &DeliveryExerciseHistoryRequest<'_>,
131 ) -> Result<Vec<DeliveryExercise>, Error> {
132 self.client
133 .get(DELIVERY_EXERCISE_HISTORY, request, false)
134 .await
135 }
136
137 /// Retrieve position tiers.
138 ///
139 /// `GET /api/v5/public/position-tiers`. Public.
140 ///
141 /// # Errors
142 ///
143 /// See [`get_system_time`](Self::get_system_time).
144 pub async fn get_position_tiers(
145 &self,
146 request: &PositionTiersRequest<'_>,
147 ) -> Result<Vec<PositionTier>, Error> {
148 self.client.get(POSITION_TIERS, request, false).await
149 }
150
151 /// Retrieve underlying values for an instrument type.
152 ///
153 /// `GET /api/v5/public/underlying`. Public.
154 ///
155 /// # Errors
156 ///
157 /// See [`get_system_time`](Self::get_system_time).
158 pub async fn get_underlying(
159 &self,
160 request: &UnderlyingRequest<'_>,
161 ) -> Result<Vec<Vec<String>>, Error> {
162 self.client.get(UNDERLYING, request, false).await
163 }
164
165 /// Retrieve insurance-fund snapshots.
166 ///
167 /// `GET /api/v5/public/insurance-fund`. Public.
168 ///
169 /// # Errors
170 ///
171 /// See [`get_system_time`](Self::get_system_time).
172 pub async fn get_insurance_fund(
173 &self,
174 request: &InsuranceFundRequest<'_>,
175 ) -> Result<Vec<InsuranceFund>, Error> {
176 self.client.get(INSURANCE_FUND, request, false).await
177 }
178
179 /// Convert between contract count and coin amount.
180 ///
181 /// `GET /api/v5/public/convert-contract-coin`. Public.
182 ///
183 /// # Errors
184 ///
185 /// See [`get_system_time`](Self::get_system_time).
186 pub async fn get_convert_contract_coin(
187 &self,
188 request: &ConvertContractCoinRequest<'_>,
189 ) -> Result<Vec<ConvertContractCoin>, Error> {
190 self.client.get(CONVERT_CONTRACT_COIN, request, false).await
191 }
192
193 /// Retrieve option summary data.
194 ///
195 /// `GET /api/v5/public/opt-summary`. Public.
196 ///
197 /// # Errors
198 ///
199 /// See [`get_system_time`](Self::get_system_time).
200 pub async fn get_option_summary(
201 &self,
202 request: &OptionSummaryRequest<'_>,
203 ) -> Result<Vec<OptionSummary>, Error> {
204 self.client.get(OPTION_SUMMARY, request, false).await
205 }
206
207 /// Retrieve the estimated delivery/exercise price for an instrument.
208 ///
209 /// `GET /api/v5/public/estimated-price`. Public.
210 ///
211 /// # Errors
212 ///
213 /// See [`get_system_time`](Self::get_system_time).
214 pub async fn get_estimated_price(
215 &self,
216 request: &InstIdRequest<'_>,
217 ) -> Result<Vec<EstimatedPrice>, Error> {
218 self.client.get(ESTIMATED_PRICE, request, false).await
219 }
220
221 /// Retrieve discount-rate and interest-free quota data.
222 ///
223 /// `GET /api/v5/public/discount-rate-interest-free-quota`. Public.
224 ///
225 /// # Errors
226 ///
227 /// See [`get_system_time`](Self::get_system_time).
228 pub async fn get_discount_rate_interest_free_quota(
229 &self,
230 request: &CurrencyRequest<'_>,
231 ) -> Result<Vec<DiscountRateInterestFreeQuota>, Error> {
232 self.client
233 .get(DISCOUNT_RATE_INTEREST_FREE_QUOTA, request, false)
234 .await
235 }
236
237 /// Retrieve interest-rate loan quota data.
238 ///
239 /// `GET /api/v5/public/interest-rate-loan-quota`. Public.
240 ///
241 /// # Errors
242 ///
243 /// See [`get_system_time`](Self::get_system_time).
244 pub async fn get_interest_rate_loan_quota(
245 &self,
246 request: &InterestRateLoanQuotaRequest,
247 ) -> Result<Vec<InterestRateLoanQuota>, Error> {
248 self.client
249 .get(INTEREST_RATE_LOAN_QUOTA, request, false)
250 .await
251 }
252
253 /// Retrieve option tick bands.
254 ///
255 /// `GET /api/v5/public/instrument-tick-bands`. Public.
256 ///
257 /// # Errors
258 ///
259 /// See [`get_system_time`](Self::get_system_time).
260 pub async fn get_instrument_tick_bands(
261 &self,
262 request: &InstrumentTickBandsRequest<'_>,
263 ) -> Result<Vec<InstrumentTickBand>, Error> {
264 self.client.get(INSTRUMENT_TICK_BANDS, request, false).await
265 }
266
267 /// Retrieve public option trade data.
268 ///
269 /// `GET /api/v5/public/option-trades`. Public.
270 ///
271 /// # Errors
272 ///
273 /// See [`get_system_time`](Self::get_system_time).
274 pub async fn get_option_trades(
275 &self,
276 request: &PublicOptionTradesRequest<'_>,
277 ) -> Result<Vec<PublicOptionTrade>, Error> {
278 self.client.get(OPTION_TRADES, request, false).await
279 }
280
281 /// Retrieve public market-data history.
282 ///
283 /// `GET /api/v5/public/market-data-history`. Public.
284 ///
285 /// # Errors
286 ///
287 /// See [`get_system_time`](Self::get_system_time).
288 pub async fn get_market_data_history(
289 &self,
290 request: &MarketDataHistoryRequest<'_>,
291 ) -> Result<Vec<MarketDataHistory>, Error> {
292 self.client.get(MARKET_DATA_HISTORY, request, false).await
293 }
294
295 /// Retrieve MM Program instrument-type classifications.
296 ///
297 /// `GET /api/v5/public/mm-instrument-types`. Public.
298 ///
299 /// # Errors
300 ///
301 /// See [`get_system_time`](Self::get_system_time).
302 pub async fn get_mm_instrument_types(
303 &self,
304 request: &MmInstrumentTypesRequest<'_>,
305 ) -> Result<Vec<MmInstrumentType>, Error> {
306 self.client.get(MM_INSTRUMENT_TYPES, request, false).await
307 }
308
309 /// Retrieve macro-economic calendar data within the last three months.
310 ///
311 /// `GET /api/v5/public/economic-calendar`. Authenticated: this endpoint
312 /// requires signing and is only supported in the production environment.
313 /// Historical data older than three months requires trading fee tier VIP1
314 /// or above.
315 ///
316 /// # Errors
317 ///
318 /// See [`get_system_time`](Self::get_system_time).
319 pub async fn get_economic_calendar(
320 &self,
321 request: &EconomicCalendarRequest<'_>,
322 ) -> Result<Vec<EconomicCalendar>, Error> {
323 self.client.get(ECONOMIC_CALENDAR, request, true).await
324 }
325}