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`. 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        self.client.get(UNDERLYING, request, false).await
165    }
166
167    /// Retrieve insurance-fund snapshots.
168    ///
169    /// `GET /api/v5/public/insurance-fund`. Public.
170    ///
171    /// # Errors
172    ///
173    /// See [`get_system_time`](Self::get_system_time).
174    pub async fn get_insurance_fund(
175        &self,
176        request: &InsuranceFundRequest,
177    ) -> Result<Vec<InsuranceFund>, Error> {
178        self.client.get(INSURANCE_FUND, request, false).await
179    }
180
181    /// Convert between contract count and coin amount.
182    ///
183    /// `GET /api/v5/public/convert-contract-coin`. Public.
184    ///
185    /// # Errors
186    ///
187    /// See [`get_system_time`](Self::get_system_time).
188    pub async fn get_convert_contract_coin(
189        &self,
190        request: &ConvertContractCoinRequest,
191    ) -> Result<Vec<ConvertContractCoin>, Error> {
192        self.client.get(CONVERT_CONTRACT_COIN, request, false).await
193    }
194
195    /// Retrieve option summary data.
196    ///
197    /// `GET /api/v5/public/opt-summary`. Public.
198    ///
199    /// # Errors
200    ///
201    /// See [`get_system_time`](Self::get_system_time).
202    pub async fn get_option_summary(
203        &self,
204        request: &OptionSummaryRequest,
205    ) -> Result<Vec<OptionSummary>, Error> {
206        self.client.get(OPTION_SUMMARY, request, false).await
207    }
208
209    /// Retrieve the estimated delivery/exercise price for an instrument.
210    ///
211    /// `GET /api/v5/public/estimated-price`. Public.
212    ///
213    /// # Errors
214    ///
215    /// See [`get_system_time`](Self::get_system_time).
216    pub async fn get_estimated_price(&self, inst_id: &str) -> Result<Vec<EstimatedPrice>, Error> {
217        let query = InstIdQuery { inst_id };
218        self.client.get(ESTIMATED_PRICE, &query, 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        ccy: Option<&str>,
231    ) -> Result<Vec<DiscountRateInterestFreeQuota>, Error> {
232        let query = CurrencyQuery { ccy };
233        self.client
234            .get(DISCOUNT_RATE_INTEREST_FREE_QUOTA, &query, false)
235            .await
236    }
237
238    /// Retrieve interest-rate loan quota data.
239    ///
240    /// `GET /api/v5/public/interest-rate-loan-quota`. Public.
241    ///
242    /// # Errors
243    ///
244    /// See [`get_system_time`](Self::get_system_time).
245    pub async fn get_interest_rate_loan_quota(
246        &self,
247        request: &InterestRateLoanQuotaRequest,
248    ) -> Result<Vec<InterestRateLoanQuota>, Error> {
249        self.client
250            .get(INTEREST_RATE_LOAN_QUOTA, request, false)
251            .await
252    }
253
254    /// Retrieve option tick bands.
255    ///
256    /// `GET /api/v5/public/instrument-tick-bands`. Public.
257    ///
258    /// # Errors
259    ///
260    /// See [`get_system_time`](Self::get_system_time).
261    pub async fn get_instrument_tick_bands(
262        &self,
263        request: &InstrumentTickBandsRequest,
264    ) -> Result<Vec<InstrumentTickBand>, Error> {
265        self.client.get(INSTRUMENT_TICK_BANDS, request, false).await
266    }
267
268    /// Retrieve public option trade data.
269    ///
270    /// `GET /api/v5/public/option-trades`. Public.
271    ///
272    /// # Errors
273    ///
274    /// See [`get_system_time`](Self::get_system_time).
275    pub async fn get_option_trades(
276        &self,
277        request: &PublicOptionTradesRequest,
278    ) -> Result<Vec<PublicOptionTrade>, Error> {
279        self.client.get(OPTION_TRADES, request, false).await
280    }
281
282    /// Retrieve public market-data history.
283    ///
284    /// `GET /api/v5/public/market-data-history`. Public.
285    ///
286    /// # Errors
287    ///
288    /// See [`get_system_time`](Self::get_system_time).
289    pub async fn get_market_data_history(
290        &self,
291        request: &MarketDataHistoryRequest,
292    ) -> Result<Vec<MarketDataHistory>, Error> {
293        self.client.get(MARKET_DATA_HISTORY, request, false).await
294    }
295}