|
25 | 25 | import octobot_trading.exchanges.connectors.ccxt.ccxt_connector as ccxt_connector
|
26 | 26 | import octobot_trading.exchanges.connectors.ccxt.enums as ccxt_enums
|
27 | 27 | import octobot_trading.exchanges.connectors.ccxt.constants as ccxt_constants
|
| 28 | +import octobot_trading.exchanges.connectors.ccxt.ccxt_client_util as ccxt_client_util |
28 | 29 | import octobot_commons.constants as commons_constants
|
29 | 30 | import octobot_trading.constants as constants
|
30 | 31 | import octobot_trading.enums as trading_enums
|
@@ -175,56 +176,54 @@ async def get_account_id(self, **kwargs: dict) -> str:
|
175 | 176 | # It is currently impossible to fetch subaccounts account id, use a constant value to identify it.
|
176 | 177 | # updated: 21/05/2024
|
177 | 178 | try:
|
178 |
| - account_id = None |
179 |
| - subaccount_id = None |
180 |
| - sub_accounts = await self.connector.client.private_get_sub_accounts() |
181 |
| - accounts = sub_accounts.get("data", {}).get("items", {}) |
182 |
| - has_subaccounts = bool(accounts) |
183 |
| - if has_subaccounts: |
184 |
| - if len(accounts) == 1: |
185 |
| - # only 1 account: use its id or name |
186 |
| - account = accounts[0] |
187 |
| - # try using subUserId if available |
188 |
| - # 'ex subUserId: 65d41ea409407d000160cc17 subName: octobot1' |
189 |
| - account_id = account.get("subUserId") or account["subName"] |
190 |
| - else: |
191 |
| - # more than 1 account: consider other accounts |
192 |
| - for account in accounts: |
193 |
| - if account["subUserId"]: |
194 |
| - subaccount_id = account["subName"] |
195 |
| - else: |
196 |
| - # only subaccounts have a subUserId: if this condition is True, we are on the main account |
197 |
| - account_id = account["subName"] |
198 |
| - if account_id and self.exchange_manager.is_future: |
199 |
| - account_id = octobot.community.to_community_exchange_internal_name( |
200 |
| - account_id, commons_constants.CONFIG_EXCHANGE_FUTURE |
| 179 | + with self.connector.error_describer(): |
| 180 | + account_id = None |
| 181 | + subaccount_id = None |
| 182 | + sub_accounts = await self.connector.client.private_get_sub_accounts() |
| 183 | + accounts = sub_accounts.get("data", {}).get("items", {}) |
| 184 | + has_subaccounts = bool(accounts) |
| 185 | + if has_subaccounts: |
| 186 | + if len(accounts) == 1: |
| 187 | + # only 1 account: use its id or name |
| 188 | + account = accounts[0] |
| 189 | + # try using subUserId if available |
| 190 | + # 'ex subUserId: 65d41ea409407d000160cc17 subName: octobot1' |
| 191 | + account_id = account.get("subUserId") or account["subName"] |
| 192 | + else: |
| 193 | + # more than 1 account: consider other accounts |
| 194 | + for account in accounts: |
| 195 | + if account["subUserId"]: |
| 196 | + subaccount_id = account["subName"] |
| 197 | + else: |
| 198 | + # only subaccounts have a subUserId: if this condition is True, we are on the main account |
| 199 | + account_id = account["subName"] |
| 200 | + if account_id and self.exchange_manager.is_future: |
| 201 | + account_id = octobot.community.to_community_exchange_internal_name( |
| 202 | + account_id, commons_constants.CONFIG_EXCHANGE_FUTURE |
| 203 | + ) |
| 204 | + if subaccount_id: |
| 205 | + # there is at least a subaccount: ensure the current account is the main account as there is no way |
| 206 | + # to know the id of the current account (only a list of existing accounts) |
| 207 | + subaccount_api_key_details = await self.connector.client.private_get_sub_api_key( |
| 208 | + {"subName": subaccount_id} |
201 | 209 | )
|
202 |
| - if subaccount_id: |
203 |
| - # there is at least a subaccount: ensure the current account is the main account as there is no way |
204 |
| - # to know the id of the current account (only a list of existing accounts) |
205 |
| - subaccount_api_key_details = await self.connector.client.private_get_sub_api_key( |
206 |
| - {"subName": subaccount_id} |
207 |
| - ) |
208 |
| - if "data" not in subaccount_api_key_details or "msg" in subaccount_api_key_details: |
209 |
| - # subaccounts can't fetch other accounts data, if this is False, we are on a subaccount |
| 210 | + if "data" not in subaccount_api_key_details or "msg" in subaccount_api_key_details: |
| 211 | + # subaccounts can't fetch other accounts data, if this is False, we are on a subaccount |
| 212 | + self.logger.error( |
| 213 | + f"kucoin api changed: it is now possible to call private_get_sub_accounts on subaccounts. " |
| 214 | + f"kucoin get_account_id has to be updated. " |
| 215 | + f"sub_accounts={sub_accounts} subaccount_api_key_details={subaccount_api_key_details}" |
| 216 | + ) |
| 217 | + return constants.DEFAULT_ACCOUNT_ID |
| 218 | + if has_subaccounts and account_id is None: |
210 | 219 | self.logger.error(
|
211 |
| - f"kucoin api changed: it is now possible to call private_get_sub_accounts on subaccounts. " |
212 |
| - f"kucoin get_account_id has to be updated. " |
213 |
| - f"sub_accounts={sub_accounts} subaccount_api_key_details={subaccount_api_key_details}" |
| 220 | + f"kucoin api changed: can't fetch master account account_id. " |
| 221 | + f"kucoin get_account_id has to be updated." |
| 222 | + f"sub_accounts={sub_accounts}" |
214 | 223 | )
|
215 |
| - return constants.DEFAULT_ACCOUNT_ID |
216 |
| - if has_subaccounts and account_id is None: |
217 |
| - self.logger.error( |
218 |
| - f"kucoin api changed: can't fetch master account account_id. " |
219 |
| - f"kucoin get_account_id has to be updated." |
220 |
| - f"sub_accounts={sub_accounts}" |
221 |
| - ) |
222 |
| - account_id = constants.DEFAULT_ACCOUNT_ID |
223 |
| - # we are on the master account |
224 |
| - return account_id or constants.DEFAULT_ACCOUNT_ID |
225 |
| - except ccxt.AuthenticationError: |
226 |
| - # when api key is wrong |
227 |
| - raise |
| 224 | + account_id = constants.DEFAULT_ACCOUNT_ID |
| 225 | + # we are on the master account |
| 226 | + return account_id or constants.DEFAULT_ACCOUNT_ID |
228 | 227 | except ccxt.ExchangeError as err:
|
229 | 228 | # ExchangeError('kucoin This user is not a master user')
|
230 | 229 | if "not a master user" not in str(err):
|
|
0 commit comments