rust_okx/api/sub_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::*;
8
9/// Accessor for the sub-account management endpoints.
10///
11/// Obtain one via [`OkxClient::sub_account`](crate::OkxClient::sub_account).
12/// All methods require credentials.
13pub struct SubAccount<'a, T> {
14 client: &'a OkxClient<T>,
15}
16
17impl<'a, T: Transport> SubAccount<'a, T> {
18 pub(crate) fn new(client: &'a OkxClient<T>) -> Self {
19 Self { client }
20 }
21
22 /// List sub-accounts of the master account.
23 ///
24 /// `GET /api/v5/users/subaccount/list`. Authenticated.
25 ///
26 /// # Errors
27 ///
28 /// Returns [`Error::InvalidRequest`] if `limit` is outside 1–100,
29 /// [`Error::Configuration`] if no credentials are set,
30 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
31 pub async fn get_subaccount_list(
32 &self,
33 request: &SubAccountListRequest,
34 ) -> Result<Vec<SubAccountEntry>, Error> {
35 self.client.get(SUBACCOUNT_LIST, request, true).await
36 }
37
38 /// Create a new sub-account.
39 ///
40 /// `POST /api/v5/users/subaccount/create-subaccount`. Authenticated.
41 ///
42 /// # Errors
43 ///
44 /// Returns [`Error::Configuration`] if no credentials are set,
45 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
46 pub async fn create_subaccount(
47 &self,
48 request: &CreateSubAccountRequest,
49 ) -> Result<Vec<SubAccountEntry>, Error> {
50 self.client.post(SUBACCOUNT_CREATE, request, true).await
51 }
52
53 /// Create an API key for a sub-account.
54 ///
55 /// `POST /api/v5/users/subaccount/apikey`. Authenticated.
56 ///
57 /// # Errors
58 ///
59 /// Returns [`Error::Configuration`] if no credentials are set,
60 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
61 pub async fn create_subaccount_apikey(
62 &self,
63 request: &CreateSubAccountApiKeyRequest,
64 ) -> Result<Vec<SubAccountApiKey>, Error> {
65 self.client.post(SUBACCOUNT_APIKEY, request, true).await
66 }
67
68 /// List API keys of a sub-account.
69 ///
70 /// `GET /api/v5/users/subaccount/apikey`. Authenticated.
71 ///
72 /// # Errors
73 ///
74 /// Returns [`Error::Configuration`] if no credentials are set,
75 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
76 pub async fn get_subaccount_apikeys(
77 &self,
78 request: &SubAccountApiKeysRequest,
79 ) -> Result<Vec<SubAccountApiKey>, Error> {
80 self.client.get(SUBACCOUNT_APIKEY, request, true).await
81 }
82
83 /// Modify an API key of a sub-account.
84 ///
85 /// `POST /api/v5/users/subaccount/modify-apikey`. Authenticated.
86 ///
87 /// # Errors
88 ///
89 /// Returns [`Error::InvalidRequest`] if none of `label`/`perm`/`ip` is set,
90 /// [`Error::Configuration`] if no credentials are set,
91 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
92 pub async fn modify_subaccount_apikey(
93 &self,
94 request: &ModifySubAccountApiKeyRequest,
95 ) -> Result<Vec<SubAccountApiKey>, Error> {
96 self.client
97 .post(SUBACCOUNT_APIKEY_MODIFY, request, true)
98 .await
99 }
100
101 /// Delete an API key of a sub-account.
102 ///
103 /// `POST /api/v5/users/subaccount/delete-apikey`. Authenticated.
104 ///
105 /// # Errors
106 ///
107 /// Returns [`Error::Configuration`] if no credentials are set,
108 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
109 pub async fn delete_subaccount_apikey(
110 &self,
111 request: &DeleteSubAccountApiKeyRequest,
112 ) -> Result<Vec<SubAccountApiKey>, Error> {
113 self.client
114 .post(SUBACCOUNT_APIKEY_DELETE, request, true)
115 .await
116 }
117
118 /// Retrieve trading-account balances of a sub-account.
119 ///
120 /// `GET /api/v5/account/subaccount/balances`. Authenticated.
121 ///
122 /// # Errors
123 ///
124 /// Returns [`Error::Configuration`] if no credentials are set,
125 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
126 pub async fn get_subaccount_trading_balances(
127 &self,
128 request: &SubAccountTradingBalancesRequest,
129 ) -> Result<Vec<SubAccountTradingBalance>, Error> {
130 self.client
131 .get(SUBACCOUNT_TRADING_BALANCES, request, true)
132 .await
133 }
134
135 /// Retrieve funding-account balances of a sub-account.
136 ///
137 /// `GET /api/v5/asset/subaccount/balances`. Authenticated.
138 ///
139 /// # Errors
140 ///
141 /// Returns [`Error::Configuration`] if no credentials are set,
142 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
143 pub async fn get_subaccount_funding_balances(
144 &self,
145 request: &SubAccountFundingBalancesRequest,
146 ) -> Result<Vec<SubAccountFundingBalance>, Error> {
147 self.client
148 .get(SUBACCOUNT_FUNDING_BALANCES, request, true)
149 .await
150 }
151
152 /// Retrieve the maximum withdrawal amount for a sub-account.
153 ///
154 /// `GET /api/v5/account/subaccount/max-withdrawal`. Authenticated.
155 ///
156 /// # Errors
157 ///
158 /// Returns [`Error::Configuration`] if no credentials are set,
159 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
160 pub async fn get_subaccount_max_withdrawal(
161 &self,
162 request: &SubAccountMaxWithdrawalRequest,
163 ) -> Result<Vec<SubAccountMaxWithdrawal>, Error> {
164 self.client
165 .get(SUBACCOUNT_MAX_WITHDRAWAL, request, true)
166 .await
167 }
168
169 /// Retrieve asset bills for sub-accounts of the master account.
170 ///
171 /// `GET /api/v5/asset/subaccount/bills`. Authenticated.
172 ///
173 /// # Errors
174 ///
175 /// Returns [`Error::InvalidRequest`] if `limit` is outside 1–100,
176 /// [`Error::Configuration`] if no credentials are set,
177 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
178 pub async fn get_subaccount_bills(
179 &self,
180 request: &SubAccountBillsRequest,
181 ) -> Result<Vec<SubAccountBill>, Error> {
182 self.client.get(SUBACCOUNT_BILLS, request, true).await
183 }
184
185 /// Retrieve asset bills for managed sub-accounts.
186 ///
187 /// `GET /api/v5/asset/subaccount/managed-subaccount-bills`. Authenticated.
188 ///
189 /// # Errors
190 ///
191 /// Returns [`Error::InvalidRequest`] if `limit` is outside 1–100,
192 /// [`Error::Configuration`] if no credentials are set,
193 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
194 pub async fn get_subaccount_managed_bills(
195 &self,
196 request: &ManagedSubAccountBillsRequest,
197 ) -> Result<Vec<ManagedSubAccountBill>, Error> {
198 self.client
199 .get(SUBACCOUNT_MANAGED_BILLS, request, true)
200 .await
201 }
202
203 /// Transfer assets between funding or trading accounts of sub-accounts.
204 ///
205 /// `POST /api/v5/asset/subaccount/transfer`. Authenticated. Use
206 /// [`SubAccountType`] to specify the account type on each side.
207 ///
208 /// # Errors
209 ///
210 /// Returns [`Error::Configuration`] if no credentials are set,
211 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
212 pub async fn transfer_between_subaccounts(
213 &self,
214 request: &SubAccountTransferRequest,
215 ) -> Result<Vec<SubAccountTransferResult>, Error> {
216 self.client.post(SUBACCOUNT_TRANSFER, request, true).await
217 }
218
219 /// Enable or disable transfers out for one or more sub-accounts.
220 ///
221 /// `POST /api/v5/users/subaccount/set-transfer-out`. Authenticated.
222 ///
223 /// # Errors
224 ///
225 /// Returns [`Error::Configuration`] if no credentials are set,
226 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
227 pub async fn set_subaccount_transfer_out(
228 &self,
229 request: &SetTransferOutRequest,
230 ) -> Result<Vec<SetTransferOutResult>, Error> {
231 self.client
232 .post(SUBACCOUNT_SET_TRANSFER_OUT, request, true)
233 .await
234 }
235
236 /// List sub-accounts of an entrusted entity.
237 ///
238 /// `GET /api/v5/users/entrust-subaccount-list`. Authenticated.
239 ///
240 /// # Errors
241 ///
242 /// Returns [`Error::Configuration`] if no credentials are set,
243 /// [`Error::Api`] on a non-zero OKX code, or transport/decode errors.
244 pub async fn get_entrust_subaccount_list(&self) -> Result<Vec<EntrustSubAccount>, Error> {
245 self.client.get(ENTRUST_SUBACCOUNT_LIST, &(), true).await
246 }
247}