Skip to main content

rust_okx/api/account/
api.rs

1use crate::client::OkxClient;
2use crate::error::Error;
3use crate::transport::Transport;
4
5use super::endpoints::*;
6use super::requests::*;
7use super::responses::*;
8use crate::model::EmptyRequest;
9
10/// Accessor for the authenticated account endpoints.
11///
12/// Obtain one via [`OkxClient::account`](crate::OkxClient::account). All methods
13/// require credentials; calling them without credentials returns
14/// [`RestError::Configuration`](crate::RestError::Configuration).
15pub struct Account<'a, T> {
16    client: &'a OkxClient<T>,
17}
18
19impl<'a, T: Transport> Account<'a, T> {
20    pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
21        Self { client }
22    }
23
24    /// Retrieve the trading-account balance.
25    ///
26    /// `GET /api/v5/account/balance`. Authenticated.
27    ///
28    /// # Errors
29    ///
30    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
31    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
32    pub async fn get_balance(
33        &self,
34        request: BalanceRequest<'_>,
35    ) -> Result<Vec<AccountBalance>, Error> {
36        self.client.get(BALANCE, &request, true).await
37    }
38
39    /// Retrieve open positions.
40    ///
41    /// `GET /api/v5/account/positions`. Authenticated.
42    ///
43    /// # Errors
44    ///
45    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
46    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
47    pub async fn get_positions(
48        &self,
49        request: &PositionsRequest<'_>,
50    ) -> Result<Vec<Position>, Error> {
51        self.client.get(POSITIONS, request, true).await
52    }
53
54    /// Retrieve account position risk.
55    ///
56    /// `GET /api/v5/account/account-position-risk`. Authenticated.
57    ///
58    /// # Errors
59    ///
60    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
61    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
62    pub async fn get_position_risk(
63        &self,
64        request: &PositionRiskRequest,
65    ) -> Result<Vec<PositionRisk>, Error> {
66        self.client.get(POSITION_RISK, request, true).await
67    }
68
69    /// Retrieve account configuration.
70    ///
71    /// `GET /api/v5/account/config`. Authenticated.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
76    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
77    pub async fn get_account_config(&self) -> Result<Vec<AccountConfig>, Error> {
78        self.client
79            .get(ACCOUNT_CONFIG, &EmptyRequest {}, true)
80            .await
81    }
82
83    /// Retrieve recent account bills.
84    ///
85    /// `GET /api/v5/account/bills`. Authenticated.
86    ///
87    /// # Errors
88    ///
89    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
90    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
91    pub async fn get_account_bills(
92        &self,
93        request: &BillsRequest<'_>,
94    ) -> Result<Vec<AccountBill>, Error> {
95        self.client.get(BILLS, request, true).await
96    }
97
98    /// Retrieve archived account bills.
99    ///
100    /// `GET /api/v5/account/bills-archive`. Authenticated.
101    ///
102    /// # Errors
103    ///
104    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
105    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
106    pub async fn get_account_bills_archive(
107        &self,
108        request: &BillsArchiveRequest<'_>,
109    ) -> Result<Vec<AccountBill>, Error> {
110        self.client.get(BILLS_ARCHIVE, request, true).await
111    }
112
113    /// Set the account position mode.
114    ///
115    /// `POST /api/v5/account/set-position-mode`. Authenticated.
116    ///
117    /// # Errors
118    ///
119    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
120    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
121    pub async fn set_position_mode(
122        &self,
123        request: &SetPositionModeRequest<'_>,
124    ) -> Result<Vec<SetPositionModeResult>, Error> {
125        self.client.post(SET_POSITION_MODE, request, true).await
126    }
127
128    /// Set leverage for an instrument or currency.
129    ///
130    /// `POST /api/v5/account/set-leverage`. Authenticated.
131    ///
132    /// # Errors
133    ///
134    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
135    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
136    pub async fn set_leverage(
137        &self,
138        request: &SetLeverageRequest<'_>,
139    ) -> Result<Vec<LeverageInfo>, Error> {
140        self.client.post(SET_LEVERAGE, request, true).await
141    }
142
143    /// Retrieve leverage settings.
144    ///
145    /// `GET /api/v5/account/leverage-info`. Authenticated.
146    ///
147    /// # Errors
148    ///
149    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
150    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
151    pub async fn get_leverage(
152        &self,
153        request: &LeverageRequest<'_>,
154    ) -> Result<Vec<LeverageInfo>, Error> {
155        self.client.get(GET_LEVERAGE, request, true).await
156    }
157
158    /// Retrieve maximum tradable size for an instrument.
159    ///
160    /// `GET /api/v5/account/max-size`. Authenticated.
161    ///
162    /// # Errors
163    ///
164    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
165    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
166    pub async fn get_max_order_size(
167        &self,
168        request: &MaxOrderSizeRequest<'_>,
169    ) -> Result<Vec<MaxOrderSize>, Error> {
170        self.client.get(MAX_ORDER_SIZE, request, true).await
171    }
172
173    /// Retrieve maximum available size for an instrument.
174    ///
175    /// `GET /api/v5/account/max-avail-size`. Authenticated.
176    ///
177    /// # Errors
178    ///
179    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
180    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
181    pub async fn get_max_avail_size(
182        &self,
183        request: &MaxAvailableSizeRequest<'_>,
184    ) -> Result<Vec<MaxAvailableSize>, Error> {
185        self.client.get(MAX_AVAILABLE_SIZE, request, true).await
186    }
187
188    /// Increase or decrease margin for a position.
189    ///
190    /// `POST /api/v5/account/position/margin-balance`. Authenticated.
191    ///
192    /// # Errors
193    ///
194    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
195    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
196    pub async fn adjust_margin(
197        &self,
198        request: &AdjustMarginRequest<'_>,
199    ) -> Result<Vec<AdjustMarginResult>, Error> {
200        self.client.post(ADJUST_MARGIN, request, true).await
201    }
202
203    /// Retrieve trade fee rates.
204    ///
205    /// `GET /api/v5/account/trade-fee`. Authenticated.
206    ///
207    /// # Errors
208    ///
209    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
210    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
211    pub async fn get_fee_rates(
212        &self,
213        request: &FeeRatesRequest<'_>,
214    ) -> Result<Vec<FeeRate>, Error> {
215        self.client.get(FEE_RATES, request, true).await
216    }
217
218    /// Retrieve account-available instruments.
219    ///
220    /// `GET /api/v5/account/instruments`. Authenticated.
221    ///
222    /// # Errors
223    ///
224    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
225    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
226    pub async fn get_account_instruments(
227        &self,
228        request: &AccountInstrumentsRequest<'_>,
229    ) -> Result<Vec<AccountInstrument>, Error> {
230        self.client.get(ACCOUNT_INSTRUMENTS, request, true).await
231    }
232
233    /// Retrieve the maximum loan amount.
234    ///
235    /// `GET /api/v5/account/max-loan`. Authenticated.
236    ///
237    /// # Errors
238    ///
239    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
240    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
241    pub async fn get_max_loan(&self, request: &MaxLoanRequest<'_>) -> Result<Vec<MaxLoan>, Error> {
242        self.client.get(MAX_LOAN, request, true).await
243    }
244
245    /// Retrieve interest-accrued records.
246    ///
247    /// `GET /api/v5/account/interest-accrued`. Authenticated.
248    ///
249    /// # Errors
250    ///
251    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
252    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
253    pub async fn get_interest_accrued(
254        &self,
255        request: &InterestAccruedRequest<'_>,
256    ) -> Result<Vec<InterestAccrued>, Error> {
257        self.client.get(INTEREST_ACCRUED, request, true).await
258    }
259
260    /// Retrieve interest rates.
261    ///
262    /// `GET /api/v5/account/interest-rate`. Authenticated.
263    ///
264    /// # Errors
265    ///
266    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
267    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
268    pub async fn get_interest_rate(
269        &self,
270        request: BalanceRequest<'_>,
271    ) -> Result<Vec<InterestRate>, Error> {
272        self.client.get(INTEREST_RATE, &request, true).await
273    }
274
275    /// Set the greeks display type.
276    ///
277    /// `POST /api/v5/account/set-greeks`. Authenticated.
278    ///
279    /// # Errors
280    ///
281    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
282    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
283    pub async fn set_greeks(
284        &self,
285        request: &SetGreeksRequest<'_>,
286    ) -> Result<Vec<SetGreeksResult>, Error> {
287        self.client.post(SET_GREEKS, request, true).await
288    }
289
290    /// Set isolated margin transfer mode.
291    ///
292    /// `POST /api/v5/account/set-isolated-mode`. Authenticated.
293    ///
294    /// # Errors
295    ///
296    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
297    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
298    pub async fn set_isolated_mode(
299        &self,
300        request: &SetIsolatedModeRequest<'_>,
301    ) -> Result<Vec<SetIsolatedModeResult>, Error> {
302        self.client.post(SET_ISOLATED_MODE, request, true).await
303    }
304
305    /// Retrieve maximum withdrawal amounts.
306    ///
307    /// `GET /api/v5/account/max-withdrawal`. Authenticated.
308    ///
309    /// # Errors
310    ///
311    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
312    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
313    pub async fn get_max_withdrawal(
314        &self,
315        request: BalanceRequest<'_>,
316    ) -> Result<Vec<MaxWithdrawal>, Error> {
317        self.client.get(MAX_WITHDRAWAL, &request, true).await
318    }
319
320    /// Retrieve borrowing rate and limit information.
321    ///
322    /// `GET /api/v5/account/interest-limits`. Authenticated.
323    ///
324    /// # Errors
325    ///
326    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
327    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
328    pub async fn get_interest_limits(
329        &self,
330        request: &InterestLimitsRequest<'_>,
331    ) -> Result<Vec<InterestLimit>, Error> {
332        self.client.get(INTEREST_LIMITS, request, true).await
333    }
334
335    /// Calculate simulated margin information.
336    ///
337    /// `POST /api/v5/account/simulated_margin`. Authenticated.
338    ///
339    /// # Errors
340    ///
341    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
342    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
343    pub async fn get_simulated_margin(
344        &self,
345        request: &SimulatedMarginRequest<'_>,
346    ) -> Result<Vec<SimulatedMargin>, Error> {
347        self.client.post(SIMULATED_MARGIN, request, true).await
348    }
349
350    /// Retrieve greeks.
351    ///
352    /// `GET /api/v5/account/greeks`. Authenticated.
353    ///
354    /// # Errors
355    ///
356    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
357    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
358    pub async fn get_greeks(&self, request: BalanceRequest<'_>) -> Result<Vec<Greek>, Error> {
359        self.client.get(GREEKS, &request, true).await
360    }
361
362    /// Retrieve position history.
363    ///
364    /// `GET /api/v5/account/positions-history`. Authenticated.
365    ///
366    /// # Errors
367    ///
368    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
369    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
370    pub async fn get_positions_history(
371        &self,
372        request: &PositionsHistoryRequest<'_>,
373    ) -> Result<Vec<PositionHistory>, Error> {
374        self.client.get(POSITIONS_HISTORY, request, true).await
375    }
376
377    /// Retrieve account position tiers.
378    ///
379    /// `GET /api/v5/account/position-tiers`. Authenticated.
380    ///
381    /// # Errors
382    ///
383    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
384    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
385    pub async fn get_account_position_tiers(
386        &self,
387        request: &AccountPositionTiersRequest<'_>,
388    ) -> Result<Vec<AccountPositionTier>, Error> {
389        self.client.get(ACCOUNT_POSITION_TIERS, request, true).await
390    }
391
392    /// Retrieve the account risk state.
393    ///
394    /// `GET /api/v5/account/risk-state`. Authenticated.
395    ///
396    /// # Errors
397    ///
398    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
399    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
400    pub async fn get_risk_state(&self) -> Result<Vec<RiskState>, Error> {
401        self.client.get(RISK_STATE, &EmptyRequest {}, true).await
402    }
403
404    /// Set account auto-loan mode.
405    ///
406    /// `POST /api/v5/account/set-auto-loan`. Authenticated.
407    ///
408    /// # Errors
409    ///
410    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
411    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
412    pub async fn set_auto_loan(
413        &self,
414        request: &SetAutoLoanRequest,
415    ) -> Result<Vec<SetAutoLoanResult>, Error> {
416        self.client.post(SET_AUTO_LOAN, request, true).await
417    }
418
419    /// Set the account level.
420    ///
421    /// `POST /api/v5/account/set-account-level`. Authenticated.
422    ///
423    /// # Errors
424    ///
425    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
426    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
427    pub async fn set_account_level(
428        &self,
429        request: &SetAccountLevelRequest<'_>,
430    ) -> Result<Vec<SetAccountLevelResult>, Error> {
431        self.client.post(SET_ACCOUNT_LEVEL, request, true).await
432    }
433
434    /// Activate option trading.
435    ///
436    /// `POST /api/v5/account/activate-option`. Authenticated.
437    ///
438    /// # Errors
439    ///
440    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
441    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
442    pub async fn activate_option(&self) -> Result<Vec<ActivateOptionResult>, Error> {
443        self.client
444            .post(ACTIVATE_OPTION, &EmptyRequest {}, true)
445            .await
446    }
447
448    /// Build simulated positions and equity.
449    ///
450    /// `POST /api/v5/account/position-builder`. Authenticated.
451    ///
452    /// # Errors
453    ///
454    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
455    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
456    pub async fn position_builder(
457        &self,
458        request: &PositionBuilderRequest<'_>,
459    ) -> Result<Vec<PositionBuilderResult>, Error> {
460        self.client.post(POSITION_BUILDER, request, true).await
461    }
462
463    /// Manually borrow or repay spot liabilities.
464    ///
465    /// `POST /api/v5/account/spot-manual-borrow-repay`. Authenticated.
466    ///
467    /// # Errors
468    ///
469    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
470    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
471    pub async fn spot_manual_borrow_repay(
472        &self,
473        request: &SpotManualBorrowRepayRequest<'_>,
474    ) -> Result<Vec<SpotBorrowRepayResult>, Error> {
475        self.client
476            .post(SPOT_MANUAL_BORROW_REPAY, request, true)
477            .await
478    }
479
480    /// Set automatic repayment for spot borrow/repay.
481    ///
482    /// `POST /api/v5/account/set-auto-repay`. Authenticated.
483    ///
484    /// # Errors
485    ///
486    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
487    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
488    pub async fn set_auto_repay(
489        &self,
490        request: &SetAutoRepayRequest,
491    ) -> Result<Vec<SetAutoRepayResult>, Error> {
492        self.client.post(SET_AUTO_REPAY, request, true).await
493    }
494
495    /// Retrieve spot borrow/repay history.
496    ///
497    /// `GET /api/v5/account/spot-borrow-repay-history`. Authenticated.
498    ///
499    /// # Errors
500    ///
501    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
502    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
503    pub async fn get_spot_borrow_repay_history(
504        &self,
505        request: &SpotBorrowRepayHistoryRequest<'_>,
506    ) -> Result<Vec<SpotBorrowRepayHistory>, Error> {
507        self.client
508            .get(SPOT_BORROW_REPAY_HISTORY, request, true)
509            .await
510    }
511
512    /// Set automatic earn for the account.
513    ///
514    /// `POST /api/v5/account/set-auto-earn`. Authenticated.
515    ///
516    /// # Errors
517    ///
518    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
519    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
520    pub async fn set_auto_earn(
521        &self,
522        request: &SetAutoEarnRequest<'_>,
523    ) -> Result<Vec<SetAutoEarnResult>, Error> {
524        self.client.post(SET_AUTO_EARN, request, true).await
525    }
526}