Skip to main content

rust_okx/api/finance/
api.rs

1use crate::api::finance::internal::CancelRedeemBody;
2use crate::client::OkxClient;
3use crate::error::Error;
4use crate::model::ValidateRequest;
5use crate::transport::Transport;
6
7use super::endpoints::*;
8use super::internal::{AmountBody, DaysQuery, NoParams, SetLendingRateBody, optional_ccy};
9use super::requests::*;
10use super::responses::*;
11/// Accessor for OKX finance endpoint groups.
12///
13/// Obtain one via [`OkxClient::finance`](crate::OkxClient::finance).
14pub struct Finance<'a, T> {
15    client: &'a OkxClient<T>,
16}
17
18impl<'a, T: Transport> Finance<'a, T> {
19    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
20        Self { client }
21    }
22
23    /// Access Savings endpoints.
24    pub fn savings(&self) -> Savings<'_, T> {
25        Savings {
26            client: self.client,
27        }
28    }
29
30    /// Access Staking/DeFi endpoints.
31    pub fn staking_defi(&self) -> StakingDefi<'_, T> {
32        StakingDefi {
33            client: self.client,
34        }
35    }
36
37    /// Access ETH staking endpoints.
38    pub fn eth_staking(&self) -> EthStaking<'_, T> {
39        EthStaking {
40            client: self.client,
41        }
42    }
43
44    /// Access SOL staking endpoints.
45    pub fn sol_staking(&self) -> SolStaking<'_, T> {
46        SolStaking {
47            client: self.client,
48        }
49    }
50
51    /// Access Flexible Loan endpoints.
52    pub fn flexible_loan(&self) -> FlexibleLoan<'_, T> {
53        FlexibleLoan {
54            client: self.client,
55        }
56    }
57}
58
59/// Accessor for Savings endpoints.
60pub struct Savings<'a, T> {
61    client: &'a OkxClient<T>,
62}
63
64impl<T: Transport> Savings<'_, T> {
65    /// Retrieve savings balances.
66    ///
67    /// # Errors
68    ///
69    /// Returns authentication, API, transport, or decode errors.
70    pub async fn get_saving_balance(&self, ccy: Option<&str>) -> Result<Vec<SavingBalance>, Error> {
71        let query = optional_ccy(ccy);
72        self.client.get(SAVINGS_BALANCE, &query, true).await
73    }
74
75    /// Purchase or redeem savings.
76    ///
77    /// # Errors
78    ///
79    /// Returns authentication, API, transport, or decode errors.
80    pub async fn purchase_redemption(
81        &self,
82        request: &SavingsPurchaseRedemptionRequest,
83    ) -> Result<Vec<SavingsPurchaseRedemptionResult>, Error> {
84        request.validate()?;
85        self.client
86            .post(SAVINGS_PURCHASE_REDEMPT, request, true)
87            .await
88    }
89
90    /// Set the savings lending rate.
91    ///
92    /// # Errors
93    ///
94    /// Returns authentication, API, transport, or decode errors.
95    pub async fn set_lending_rate(
96        &self,
97        ccy: &str,
98        rate: &str,
99    ) -> Result<Vec<SetLendingRateResult>, Error> {
100        let body = SetLendingRateBody { ccy, rate };
101        self.client
102            .post(SAVINGS_SET_LENDING_RATE, &body, true)
103            .await
104    }
105
106    /// Retrieve lending history.
107    ///
108    /// # Errors
109    ///
110    /// Returns authentication, API, transport, or decode errors.
111    pub async fn get_lending_history(
112        &self,
113        request: &FinanceHistoryRequest,
114    ) -> Result<Vec<LendingHistory>, Error> {
115        request.validate()?;
116        self.client
117            .get(SAVINGS_LENDING_HISTORY, request, true)
118            .await
119    }
120
121    /// Retrieve public borrow history.
122    ///
123    /// # Errors
124    ///
125    /// Returns API, transport, or decode errors.
126    pub async fn get_public_borrow_history(
127        &self,
128        request: &FinanceHistoryRequest,
129    ) -> Result<Vec<PublicBorrowHistory>, Error> {
130        request.validate()?;
131        self.client
132            .get(SAVINGS_PUBLIC_BORROW_HISTORY, request, false)
133            .await
134    }
135
136    /// Retrieve public borrow info.
137    ///
138    /// # Errors
139    ///
140    /// Returns API, transport, or decode errors.
141    pub async fn get_public_borrow_info(
142        &self,
143        ccy: Option<&str>,
144    ) -> Result<Vec<PublicBorrowInfo>, Error> {
145        let query = optional_ccy(ccy);
146        self.client
147            .get(SAVINGS_PUBLIC_BORROW_INFO, &query, false)
148            .await
149    }
150}
151
152/// Accessor for Staking/DeFi endpoints.
153pub struct StakingDefi<'a, T> {
154    client: &'a OkxClient<T>,
155}
156
157impl<T: Transport> StakingDefi<'_, T> {
158    /// Retrieve Staking/DeFi offers.
159    ///
160    /// # Errors
161    ///
162    /// Returns API, transport, or decode errors.
163    pub async fn get_offers(
164        &self,
165        request: &StakingDefiOffersRequest,
166    ) -> Result<Vec<StakingDefiOffer>, Error> {
167        request.validate()?;
168        self.client.get(STAKING_DEFI_OFFERS, request, true).await
169    }
170
171    /// Purchase a Staking/DeFi product.
172    ///
173    /// # Errors
174    ///
175    /// Returns authentication, API, transport, or decode errors.
176    pub async fn purchase(
177        &self,
178        request: &StakingDefiPurchaseRequest,
179    ) -> Result<Vec<StakingDefiOrder>, Error> {
180        request.validate()?;
181        self.client.post(STAKING_DEFI_PURCHASE, request, true).await
182    }
183
184    /// Redeem a Staking/DeFi order.
185    ///
186    /// # Errors
187    ///
188    /// Returns authentication, API, transport, or decode errors.
189    pub async fn redeem(
190        &self,
191        request: &StakingDefiRedeemRequest,
192    ) -> Result<Vec<StakingDefiOrder>, Error> {
193        request.validate()?;
194        self.client.post(STAKING_DEFI_REDEEM, request, true).await
195    }
196
197    /// Cancel a Staking/DeFi order.
198    ///
199    /// # Errors
200    ///
201    /// Returns authentication, API, transport, or decode errors.
202    pub async fn cancel(
203        &self,
204        request: &StakingDefiCancelRequest,
205    ) -> Result<Vec<StakingDefiOrder>, Error> {
206        request.validate()?;
207        self.client.post(STAKING_DEFI_CANCEL, request, true).await
208    }
209
210    /// Retrieve active Staking/DeFi orders.
211    ///
212    /// # Errors
213    ///
214    /// Returns authentication, API, transport, or decode errors.
215    pub async fn get_active_orders(
216        &self,
217        request: &StakingDefiActiveOrdersRequest,
218    ) -> Result<Vec<StakingDefiOrder>, Error> {
219        request.validate()?;
220        self.client
221            .get(STAKING_DEFI_ACTIVE_ORDERS, request, true)
222            .await
223    }
224
225    /// Retrieve Staking/DeFi order history.
226    ///
227    /// # Errors
228    ///
229    /// Returns authentication, API, transport, or decode errors.
230    pub async fn get_orders_history(
231        &self,
232        request: &StakingDefiOrderHistoryRequest,
233    ) -> Result<Vec<StakingDefiOrder>, Error> {
234        request.validate()?;
235        self.client
236            .get(STAKING_DEFI_ORDERS_HISTORY, request, true)
237            .await
238    }
239}
240
241/// Accessor for ETH staking endpoints.
242pub struct EthStaking<'a, T> {
243    client: &'a OkxClient<T>,
244}
245
246impl<T: Transport> EthStaking<'_, T> {
247    /// Retrieve ETH staking product info.
248    ///
249    /// # Errors
250    ///
251    /// Returns API, transport, or decode errors.
252    pub async fn product_info(&self) -> Result<Vec<StakingProductInfo>, Error> {
253        self.client.get(ETH_PRODUCT_INFO, &NoParams {}, true).await
254    }
255
256    /// Purchase ETH staking.
257    ///
258    /// # Errors
259    ///
260    /// Returns authentication, API, transport, or decode errors.
261    pub async fn purchase(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
262        let body = AmountBody { amt };
263        self.client.post(ETH_PURCHASE, &body, true).await
264    }
265
266    /// Redeem ETH staking.
267    ///
268    /// # Errors
269    ///
270    /// Returns authentication, API, transport, or decode errors.
271    pub async fn redeem(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
272        let body = AmountBody { amt };
273        self.client.post(ETH_REDEEM, &body, true).await
274    }
275
276    /// Cancel redeem ETH staking.
277    ///
278    /// # Errors
279    ///
280    /// Returns authentication, API, transport, or decode errors.
281    pub async fn cancel_redeem(&self, ord_id: &str) -> Result<Vec<CancelRedeem>, Error> {
282        let body = CancelRedeemBody { ord_id };
283        self.client.post(ETH_CANCEL_REDEEM, &body, true).await
284    }
285
286    /// Retrieve ETH staking balance.
287    ///
288    /// # Errors
289    ///
290    /// Returns authentication, API, transport, or decode errors.
291    pub async fn balance(&self) -> Result<Vec<StakingBalance>, Error> {
292        self.client.get(ETH_BALANCE, &NoParams {}, true).await
293    }
294
295    /// Retrieve ETH staking purchase/redeem history.
296    ///
297    /// # Errors
298    ///
299    /// Returns authentication, API, transport, or decode errors.
300    pub async fn purchase_redeem_history(
301        &self,
302        request: &FinanceHistoryRequest,
303    ) -> Result<Vec<StakingHistory>, Error> {
304        request.validate()?;
305        self.client.get(ETH_HISTORY, request, true).await
306    }
307
308    /// Retrieve ETH staking APY history.
309    ///
310    /// # Errors
311    ///
312    /// Returns API, transport, or decode errors.
313    pub async fn apy_history(&self, days: &str) -> Result<Vec<StakingApyHistory>, Error> {
314        let query = DaysQuery { days };
315        self.client.get(ETH_APY_HISTORY, &query, false).await
316    }
317}
318
319/// Accessor for SOL staking endpoints.
320pub struct SolStaking<'a, T> {
321    client: &'a OkxClient<T>,
322}
323
324impl<T: Transport> SolStaking<'_, T> {
325    /// Retrieve SOL staking product info.
326    ///
327    /// # Errors
328    ///
329    /// Returns API, transport, or decode errors.
330    pub async fn product_info(&self) -> Result<StakingProductInfo, Error> {
331        self.client.get(SOL_PRODUCT_INFO, &NoParams {}, true).await
332    }
333
334    /// Purchase SOL staking.
335    ///
336    /// # Errors
337    ///
338    /// Returns authentication, API, transport, or decode errors.
339    pub async fn purchase(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
340        let body = AmountBody { amt };
341        self.client.post(SOL_PURCHASE, &body, true).await
342    }
343
344    /// Redeem SOL staking.
345    ///
346    /// # Errors
347    ///
348    /// Returns authentication, API, transport, or decode errors.
349    pub async fn redeem(&self, amt: &str) -> Result<Vec<StakingOrder>, Error> {
350        let body = AmountBody { amt };
351        self.client.post(SOL_REDEEM, &body, true).await
352    }
353
354    /// Retrieve SOL staking balance.
355    ///
356    /// # Errors
357    ///
358    /// Returns authentication, API, transport, or decode errors.
359    pub async fn balance(&self) -> Result<Vec<StakingBalance>, Error> {
360        self.client.get(SOL_BALANCE, &NoParams {}, true).await
361    }
362
363    /// Retrieve SOL staking purchase/redeem history.
364    ///
365    /// # Errors
366    ///
367    /// Returns authentication, API, transport, or decode errors.
368    pub async fn purchase_redeem_history(
369        &self,
370        request: &FinanceHistoryRequest,
371    ) -> Result<Vec<StakingHistory>, Error> {
372        request.validate()?;
373        self.client.get(SOL_HISTORY, request, true).await
374    }
375
376    /// Retrieve SOL staking APY history.
377    ///
378    /// # Errors
379    ///
380    /// Returns API, transport, or decode errors.
381    pub async fn apy_history(&self, days: &str) -> Result<Vec<StakingApyHistory>, Error> {
382        let query = DaysQuery { days };
383        self.client.get(SOL_APY_HISTORY, &query, false).await
384    }
385}
386
387/// Accessor for Flexible Loan endpoints.
388pub struct FlexibleLoan<'a, T> {
389    client: &'a OkxClient<T>,
390}
391
392impl<T: Transport> FlexibleLoan<'_, T> {
393    /// Retrieve borrowable currencies.
394    ///
395    /// # Errors
396    ///
397    /// Returns API, transport, or decode errors.
398    pub async fn borrow_currencies(&self) -> Result<Vec<FlexibleLoanCurrency>, Error> {
399        self.client
400            .get(FLEX_BORROW_CURRENCIES, &NoParams {}, true)
401            .await
402    }
403
404    /// Retrieve collateral assets.
405    ///
406    /// # Errors
407    ///
408    /// Returns API, transport, or decode errors.
409    pub async fn collateral_assets(
410        &self,
411        request: &FlexibleLoanCollateralAssetsRequest,
412    ) -> Result<Vec<FlexibleLoanCollateralAsset>, Error> {
413        request.validate()?;
414        self.client
415            .get(FLEX_COLLATERAL_ASSETS, request, false)
416            .await
417    }
418
419    /// Estimate maximum flexible-loan amount.
420    ///
421    /// # Errors
422    ///
423    /// Returns authentication, API, transport, or decode errors.
424    pub async fn max_loan(
425        &self,
426        request: &FlexibleLoanMaxLoanRequest,
427    ) -> Result<Vec<FlexibleLoanMaxLoan>, Error> {
428        request.validate()?;
429        self.client.post(FLEX_MAX_LOAN, request, true).await
430    }
431
432    /// Retrieve maximum collateral redeem amount.
433    ///
434    /// # Errors
435    ///
436    /// Returns authentication, API, transport, or decode errors.
437    pub async fn max_collateral_redeem_amount(
438        &self,
439        request: &FlexibleLoanMaxRedeemRequest,
440    ) -> Result<Vec<FlexibleLoanMaxRedeem>, Error> {
441        request.validate()?;
442        self.client.get(FLEX_MAX_REDEEM, request, true).await
443    }
444
445    /// Adjust flexible-loan collateral.
446    ///
447    /// # Errors
448    ///
449    /// Returns authentication, API, transport, or decode errors.
450    pub async fn adjust_collateral(
451        &self,
452        request: &FlexibleLoanAdjustCollateralRequest,
453    ) -> Result<Vec<FlexibleLoanOrder>, Error> {
454        request.validate()?;
455        self.client
456            .post(FLEX_ADJUST_COLLATERAL, request, true)
457            .await
458    }
459
460    /// Retrieve flexible-loan info.
461    ///
462    /// # Errors
463    ///
464    /// Returns authentication, API, transport, or decode errors.
465    pub async fn loan_info(
466        &self,
467        request: &FlexibleLoanInfoRequest,
468    ) -> Result<Vec<FlexibleLoanInfo>, Error> {
469        request.validate()?;
470        self.client.get(FLEX_LOAN_INFO, request, true).await
471    }
472
473    /// Retrieve flexible-loan history.
474    ///
475    /// # Errors
476    ///
477    /// Returns authentication, API, transport, or decode errors.
478    pub async fn loan_history(
479        &self,
480        request: &FlexibleLoanHistoryRequest,
481    ) -> Result<Vec<FlexibleLoanHistory>, Error> {
482        request.validate()?;
483        self.client.get(FLEX_LOAN_HISTORY, request, true).await
484    }
485
486    /// Retrieve flexible-loan accrued interest.
487    ///
488    /// # Errors
489    ///
490    /// Returns authentication, API, transport, or decode errors.
491    pub async fn interest_accrued(
492        &self,
493        request: &FlexibleLoanInterestAccruedRequest,
494    ) -> Result<Vec<FlexibleLoanInterest>, Error> {
495        request.validate()?;
496        self.client.get(FLEX_INTEREST_ACCRUED, request, true).await
497    }
498}