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