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(&self, request: &FeeRatesRequest) -> Result<Vec<FeeRate>, Error> {
212        self.client.get(FEE_RATES, request, true).await
213    }
214
215    /// Retrieve account-available instruments.
216    ///
217    /// `GET /api/v5/account/instruments`. Authenticated.
218    ///
219    /// # Errors
220    ///
221    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
222    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
223    pub async fn get_account_instruments(
224        &self,
225        request: &AccountInstrumentsRequest,
226    ) -> Result<Vec<AccountInstrument>, Error> {
227        self.client.get(ACCOUNT_INSTRUMENTS, request, true).await
228    }
229
230    /// Retrieve the maximum loan amount.
231    ///
232    /// `GET /api/v5/account/max-loan`. Authenticated.
233    ///
234    /// # Errors
235    ///
236    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
237    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
238    pub async fn get_max_loan(&self, request: &MaxLoanRequest) -> Result<Vec<MaxLoan>, Error> {
239        self.client.get(MAX_LOAN, request, true).await
240    }
241
242    /// Retrieve interest-accrued records.
243    ///
244    /// `GET /api/v5/account/interest-accrued`. Authenticated.
245    ///
246    /// # Errors
247    ///
248    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
249    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
250    pub async fn get_interest_accrued(
251        &self,
252        request: &InterestAccruedRequest,
253    ) -> Result<Vec<InterestAccrued>, Error> {
254        self.client.get(INTEREST_ACCRUED, request, true).await
255    }
256
257    /// Retrieve interest rates.
258    ///
259    /// `GET /api/v5/account/interest-rate`. Authenticated.
260    ///
261    /// # Errors
262    ///
263    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
264    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
265    pub async fn get_interest_rate(
266        &self,
267        request: BalanceRequest<'_>,
268    ) -> Result<Vec<InterestRate>, Error> {
269        self.client.get(INTEREST_RATE, &request, true).await
270    }
271
272    /// Set the greeks display type.
273    ///
274    /// `POST /api/v5/account/set-greeks`. Authenticated.
275    ///
276    /// # Errors
277    ///
278    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
279    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
280    pub async fn set_greeks(
281        &self,
282        request: &SetGreeksRequest<'_>,
283    ) -> Result<Vec<SetGreeksResult>, Error> {
284        self.client.post(SET_GREEKS, request, true).await
285    }
286
287    /// Set isolated margin transfer mode.
288    ///
289    /// `POST /api/v5/account/set-isolated-mode`. Authenticated.
290    ///
291    /// # Errors
292    ///
293    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
294    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
295    pub async fn set_isolated_mode(
296        &self,
297        request: &SetIsolatedModeRequest<'_>,
298    ) -> Result<Vec<SetIsolatedModeResult>, Error> {
299        self.client.post(SET_ISOLATED_MODE, request, true).await
300    }
301
302    /// Retrieve maximum withdrawal amounts.
303    ///
304    /// `GET /api/v5/account/max-withdrawal`. Authenticated.
305    ///
306    /// # Errors
307    ///
308    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
309    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
310    pub async fn get_max_withdrawal(
311        &self,
312        request: BalanceRequest<'_>,
313    ) -> Result<Vec<MaxWithdrawal>, Error> {
314        self.client.get(MAX_WITHDRAWAL, &request, true).await
315    }
316
317    /// Retrieve borrowing rate and limit information.
318    ///
319    /// `GET /api/v5/account/interest-limits`. Authenticated.
320    ///
321    /// # Errors
322    ///
323    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
324    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
325    pub async fn get_interest_limits(
326        &self,
327        request: &InterestLimitsRequest,
328    ) -> Result<Vec<InterestLimit>, Error> {
329        self.client.get(INTEREST_LIMITS, request, true).await
330    }
331
332    /// Calculate simulated margin information.
333    ///
334    /// `POST /api/v5/account/simulated_margin`. Authenticated.
335    ///
336    /// # Errors
337    ///
338    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
339    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
340    pub async fn get_simulated_margin(
341        &self,
342        request: &SimulatedMarginRequest,
343    ) -> Result<Vec<SimulatedMargin>, Error> {
344        self.client.post(SIMULATED_MARGIN, request, true).await
345    }
346
347    /// Retrieve greeks.
348    ///
349    /// `GET /api/v5/account/greeks`. Authenticated.
350    ///
351    /// # Errors
352    ///
353    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
354    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
355    pub async fn get_greeks(&self, request: BalanceRequest<'_>) -> Result<Vec<Greek>, Error> {
356        self.client.get(GREEKS, &request, true).await
357    }
358
359    /// Retrieve position history.
360    ///
361    /// `GET /api/v5/account/positions-history`. Authenticated.
362    ///
363    /// # Errors
364    ///
365    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
366    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
367    pub async fn get_positions_history(
368        &self,
369        request: &PositionsHistoryRequest,
370    ) -> Result<Vec<PositionHistory>, Error> {
371        self.client.get(POSITIONS_HISTORY, request, true).await
372    }
373
374    /// Retrieve account position tiers.
375    ///
376    /// `GET /api/v5/account/position-tiers`. Authenticated.
377    ///
378    /// # Errors
379    ///
380    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
381    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
382    pub async fn get_account_position_tiers(
383        &self,
384        request: &AccountPositionTiersRequest,
385    ) -> Result<Vec<AccountPositionTier>, Error> {
386        self.client.get(ACCOUNT_POSITION_TIERS, request, true).await
387    }
388
389    /// Retrieve the account risk state.
390    ///
391    /// `GET /api/v5/account/risk-state`. Authenticated.
392    ///
393    /// # Errors
394    ///
395    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
396    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
397    pub async fn get_risk_state(&self) -> Result<Vec<RiskState>, Error> {
398        self.client.get(RISK_STATE, &EmptyRequest {}, true).await
399    }
400
401    /// Set account auto-loan mode.
402    ///
403    /// `POST /api/v5/account/set-auto-loan`. Authenticated.
404    ///
405    /// # Errors
406    ///
407    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
408    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
409    pub async fn set_auto_loan(
410        &self,
411        request: &SetAutoLoanRequest,
412    ) -> Result<Vec<SetAutoLoanResult>, Error> {
413        self.client.post(SET_AUTO_LOAN, request, true).await
414    }
415
416    /// Set the account level.
417    ///
418    /// `POST /api/v5/account/set-account-level`. Authenticated.
419    ///
420    /// # Errors
421    ///
422    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
423    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
424    pub async fn set_account_level(
425        &self,
426        request: &SetAccountLevelRequest<'_>,
427    ) -> Result<Vec<SetAccountLevelResult>, Error> {
428        self.client.post(SET_ACCOUNT_LEVEL, request, true).await
429    }
430
431    /// Activate option trading.
432    ///
433    /// `POST /api/v5/account/activate-option`. Authenticated.
434    ///
435    /// # Errors
436    ///
437    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
438    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
439    pub async fn activate_option(&self) -> Result<Vec<ActivateOptionResult>, Error> {
440        self.client
441            .post(ACTIVATE_OPTION, &EmptyRequest {}, true)
442            .await
443    }
444
445    /// Build simulated positions and equity.
446    ///
447    /// `POST /api/v5/account/position-builder`. Authenticated.
448    ///
449    /// # Errors
450    ///
451    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
452    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
453    pub async fn position_builder(
454        &self,
455        request: &PositionBuilderRequest,
456    ) -> Result<Vec<PositionBuilderResult>, Error> {
457        self.client.post(POSITION_BUILDER, request, true).await
458    }
459
460    /// Manually borrow or repay spot liabilities.
461    ///
462    /// `POST /api/v5/account/spot-manual-borrow-repay`. Authenticated.
463    ///
464    /// # Errors
465    ///
466    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
467    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
468    pub async fn spot_manual_borrow_repay(
469        &self,
470        request: &SpotManualBorrowRepayRequest,
471    ) -> Result<Vec<SpotBorrowRepayResult>, Error> {
472        self.client
473            .post(SPOT_MANUAL_BORROW_REPAY, request, true)
474            .await
475    }
476
477    /// Set automatic repayment for spot borrow/repay.
478    ///
479    /// `POST /api/v5/account/set-auto-repay`. Authenticated.
480    ///
481    /// # Errors
482    ///
483    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
484    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
485    pub async fn set_auto_repay(
486        &self,
487        request: &SetAutoRepayRequest,
488    ) -> Result<Vec<SetAutoRepayResult>, Error> {
489        self.client.post(SET_AUTO_REPAY, request, true).await
490    }
491
492    /// Retrieve spot borrow/repay history.
493    ///
494    /// `GET /api/v5/account/spot-borrow-repay-history`. Authenticated.
495    ///
496    /// # Errors
497    ///
498    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
499    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
500    pub async fn get_spot_borrow_repay_history(
501        &self,
502        request: &SpotBorrowRepayHistoryRequest,
503    ) -> Result<Vec<SpotBorrowRepayHistory>, Error> {
504        self.client
505            .get(SPOT_BORROW_REPAY_HISTORY, request, true)
506            .await
507    }
508
509    /// Set automatic earn for the account.
510    ///
511    /// `POST /api/v5/account/set-auto-earn`. Authenticated.
512    ///
513    /// # Errors
514    ///
515    /// Returns [`RestError::Configuration`](crate::RestError::Configuration) if no credentials are set,
516    /// [`RestError::Okx`](crate::RestError::Okx) on a non-zero OKX code, or transport/decode errors.
517    pub async fn set_auto_earn(
518        &self,
519        request: &SetAutoEarnRequest,
520    ) -> Result<Vec<SetAutoEarnResult>, Error> {
521        self.client.post(SET_AUTO_EARN, request, true).await
522    }
523}