Skip to main content

rust_okx/api/public_data/
api.rs

1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::model::InstType;
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(&self, inst_type: InstType) -> Result<Vec<String>, Error> {
162        let query = UnderlyingQuery {
163            inst_type: &inst_type,
164        };
165        self.client.get(UNDERLYING, &query, 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        self.client.get(OPTION_SUMMARY, request, false).await
208    }
209
210    /// Retrieve the estimated delivery/exercise price for an instrument.
211    ///
212    /// `GET /api/v5/public/estimated-price`. Public.
213    ///
214    /// # Errors
215    ///
216    /// See [`get_system_time`](Self::get_system_time).
217    pub async fn get_estimated_price(&self, inst_id: &str) -> Result<Vec<EstimatedPrice>, Error> {
218        let query = InstIdQuery { inst_id };
219        self.client.get(ESTIMATED_PRICE, &query, false).await
220    }
221
222    /// Retrieve discount-rate and interest-free quota data.
223    ///
224    /// `GET /api/v5/public/discount-rate-interest-free-quota`. Public.
225    ///
226    /// # Errors
227    ///
228    /// See [`get_system_time`](Self::get_system_time).
229    pub async fn get_discount_rate_interest_free_quota(
230        &self,
231        ccy: Option<&str>,
232    ) -> Result<Vec<DiscountRateInterestFreeQuota>, Error> {
233        let query = CurrencyQuery { ccy };
234        self.client
235            .get(DISCOUNT_RATE_INTEREST_FREE_QUOTA, &query, false)
236            .await
237    }
238
239    /// Retrieve interest-rate loan quota data.
240    ///
241    /// `GET /api/v5/public/interest-rate-loan-quota`. Public.
242    ///
243    /// # Errors
244    ///
245    /// See [`get_system_time`](Self::get_system_time).
246    pub async fn get_interest_rate_loan_quota(
247        &self,
248        request: &InterestRateLoanQuotaRequest,
249    ) -> Result<Vec<InterestRateLoanQuota>, Error> {
250        self.client
251            .get(INTEREST_RATE_LOAN_QUOTA, request, false)
252            .await
253    }
254
255    /// Retrieve VIP interest-rate loan quota data.
256    ///
257    /// `GET /api/v5/public/vip-interest-rate-loan-quota`. Public.
258    ///
259    /// # Errors
260    ///
261    /// See [`get_system_time`](Self::get_system_time).
262    pub async fn get_vip_interest_rate_loan_quota(
263        &self,
264        request: &VipInterestRateLoanQuotaRequest,
265    ) -> Result<Vec<VipInterestRateLoanQuota>, Error> {
266        self.client
267            .get(VIP_INTEREST_RATE_LOAN_QUOTA, request, false)
268            .await
269    }
270
271    /// Retrieve option tick bands.
272    ///
273    /// `GET /api/v5/public/instrument-tick-bands`. Public.
274    ///
275    /// # Errors
276    ///
277    /// See [`get_system_time`](Self::get_system_time).
278    pub async fn get_instrument_tick_bands(
279        &self,
280        request: &InstrumentTickBandsRequest,
281    ) -> Result<Vec<InstrumentTickBand>, Error> {
282        self.client.get(INSTRUMENT_TICK_BANDS, request, false).await
283    }
284
285    /// Retrieve public option trade data.
286    ///
287    /// `GET /api/v5/public/option-trades`. Public.
288    ///
289    /// # Errors
290    ///
291    /// See [`get_system_time`](Self::get_system_time).
292    pub async fn get_option_trades(
293        &self,
294        request: &PublicOptionTradesRequest,
295    ) -> Result<Vec<PublicOptionTrade>, Error> {
296        self.client.get(OPTION_TRADES, request, false).await
297    }
298
299    /// Retrieve public market-data history.
300    ///
301    /// `GET /api/v5/public/market-data-history`. Public.
302    ///
303    /// # Errors
304    ///
305    /// See [`get_system_time`](Self::get_system_time).
306    pub async fn get_market_data_history(
307        &self,
308        request: &MarketDataHistoryRequest,
309    ) -> Result<Vec<MarketDataHistory>, Error> {
310        self.client.get(MARKET_DATA_HISTORY, request, false).await
311    }
312}