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