Skip to content

Commit

Permalink
[Community] add auth fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeDSM committed Aug 28, 2024
1 parent 521ebde commit 9adbe0b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
11 changes: 9 additions & 2 deletions octobot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,21 @@ async def _apply_db_bot_config(logger, config, community_auth) -> bool:
try:
# async loop may have changed if community_auth was already used before
await community_auth.ensure_async_loop()
profile_data = await community_auth.supabase_client.fetch_bot_tentacles_data_based_config(constants.COMMUNITY_BOT_ID)
profile_data, auth_data = await community_auth.fetch_bot_tentacles_data_based_config(
constants.COMMUNITY_BOT_ID,
constants.USER_AUTH_KEY,
)
profile = await profiles.import_profile_data_as_profile(
profile_data,
constants.PROFILE_FILE_SCHEMA,
None,
name=profile_data.profile_details.name,
auto_update=False
auto_update=False,
force_simulator=False,
)
for auth_data_element in auth_data:
if auth_data_element.apply_to_exchange_config(config):
logger.info(f"Applying {auth_data_element.internal_name} exchange auth details")
config.load_profiles()
except octobot.community.errors.BotNotFoundError:
raise errors.RemoteConfigError(
Expand Down
8 changes: 8 additions & 0 deletions octobot/community/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ async def _re_create_client(self):
self.logger.debug(f"Refreshing user session")
self.supabase_client.event_loop = asyncio.get_event_loop()
await self.supabase_client.refresh_session()
await self._on_account_updated()

async def ensure_async_loop(self):
# elements should be bound to the current loop
Expand Down Expand Up @@ -548,6 +549,13 @@ def _get_compatible_strategy_categories(self) -> list[str]:
category_types.append("index")
return category_types

async def fetch_bot_tentacles_data_based_config(
self, bot_id: str, auth_key: typing.Optional[str]
) -> (commons_profiles.ProfileData, list[commons_profiles.ExchangeAuthData]):
return await self.supabase_client.fetch_bot_tentacles_data_based_config(
bot_id, self, auth_key
)

async def fetch_private_data(self, reset=False):
try:
mqtt_uuid = None
Expand Down
22 changes: 17 additions & 5 deletions octobot/community/supabase_backend/community_supabase_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ async def update_bot_orders(self, bot_id, formatted_orders) -> dict:
bot_id, bot_update
)

async def fetch_bot_tentacles_data_based_config(self, bot_id: str) -> commons_profiles.ProfileData:
async def fetch_bot_tentacles_data_based_config(
self, bot_id: str, authenticator, auth_key: typing.Optional[str]
) -> (commons_profiles.ProfileData, list[commons_profiles.ExchangeAuthData]):
if not bot_id:
raise errors.BotNotFoundError(f"bot_id is '{bot_id}'")
commons_logging.get_logger(__name__).debug(f"Fetching {bot_id} bot config")
Expand Down Expand Up @@ -383,16 +385,26 @@ async def fetch_bot_tentacles_data_based_config(self, bot_id: str) -> commons_pr
[],
commons_profile_data.TradingData(commons_constants.USD_LIKE_COINS[0])
)
auth_data = []
# apply specific options
self._apply_options_based_config(profile_data, bot_config["bot_config"])
return profile_data
await self._apply_options_based_config(
profile_data, auth_data, bot_config["bot_config"], authenticator, auth_key
)
return profile_data, auth_data


def _apply_options_based_config(self, profile_data: commons_profiles.ProfileData, bot_config: dict):
async def _apply_options_based_config(
self, profile_data: commons_profiles.ProfileData,
auth_data: list[commons_profiles.ExchangeAuthData], bot_config: dict,
authenticator, auth_key: typing.Optional[str]
):
if tentacles_data := [
commons_profile_data.TentaclesData.from_dict(td)
for td in bot_config[enums.BotConfigKeys.OPTIONS.value].get("tentacles", [])
]:
commons_profiles.TentaclesProfileDataTranslator(profile_data).translate(tentacles_data, bot_config)
await commons_profiles.TentaclesProfileDataTranslator(profile_data, auth_data).translate(
tentacles_data, bot_config, authenticator, auth_key
)

async def fetch_bot_profile_data(self, bot_config_id: str) -> commons_profiles.ProfileData:
if not bot_config_id:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Drakkar-Software requirements
OctoBot-Commons==1.9.55
OctoBot-Commons==1.9.56
OctoBot-Trading==2.4.107
OctoBot-Evaluators==1.9.6
OctoBot-Tentacles-Manager==2.9.16
Expand Down

0 comments on commit 9adbe0b

Please sign in to comment.