From e4bc03db3d469149e6595843d2a0e8791d6ff7fe Mon Sep 17 00:00:00 2001 From: cardosofede Date: Mon, 24 Jun 2024 20:39:53 +0200 Subject: [PATCH 1/7] (feat) improve accounts state dump process --- services/accounts_service.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/services/accounts_service.py b/services/accounts_service.py index 9b67303..42d0df8 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -77,24 +77,30 @@ async def dump_account_state_loop(self): :return: """ while True: - now = datetime.now() - next_log_time = (now + timedelta(minutes=self.account_history_dump_interval)).replace(second=0, microsecond=0) - next_log_time = next_log_time - timedelta(minutes=next_log_time.minute % self.account_history_dump_interval) - sleep_duration = (next_log_time - now).total_seconds() - await asyncio.sleep(sleep_duration) try: await self.dump_account_state() except Exception as e: logging.error(f"Error dumping account state: {e}") + finally: + now = datetime.now() + next_log_time = (now + timedelta(minutes=self.account_history_dump_interval)).replace(second=0, + microsecond=0) + next_log_time = next_log_time - timedelta( + minutes=next_log_time.minute % self.account_history_dump_interval) + sleep_duration = (next_log_time - now).total_seconds() + await asyncio.sleep(sleep_duration) async def dump_account_state(self): """ - Dump the current account state to a JSON file. + Dump the current account state to a JSON file. Create it if the file not exists. :return: """ timestamp = datetime.now().isoformat() state_to_dump = {"timestamp": timestamp, "state": self.accounts_state} - file_system.append_to_file("data", self.history_file, json.dumps(state_to_dump) + "\n") + if not file_system.path_exists(path=f"data/{self.history_file}"): + file_system.add_file(directory="data", file_name=self.history_file, content=json.dumps(state_to_dump) + "\n") + else: + file_system.append_to_file(directory="data", file_name=self.history_file, content=json.dumps(state_to_dump) + "\n") def load_account_state_history(self): """ From fca590cbdb406620149b98ef366688cb945c6129 Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 26 Jun 2024 00:26:55 +0200 Subject: [PATCH 2/7] (feat) add account to state --- services/accounts_service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/services/accounts_service.py b/services/accounts_service.py index 42d0df8..3876c9f 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -335,6 +335,7 @@ def add_account(self, account_name: str): for file in files_to_copy: file_system.copy_file(f"credentials/master_account/{file}", f"credentials/{account_name}/{file}") self.accounts[account_name] = {} + self.accounts_state[account_name] = {} def delete_account(self, account_name: str): """ From ee5108262a6ea1264d7b52ead02dc368ebb475a7 Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 26 Jun 2024 01:00:32 +0200 Subject: [PATCH 3/7] (feat) change default dump to 10 mins --- services/accounts_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/accounts_service.py b/services/accounts_service.py index 3876c9f..fd34c75 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -30,7 +30,7 @@ def __init__(self, update_account_state_interval_minutes: int = 1, default_quote: str = "USDT", account_history_file: str = "account_state_history.json", - account_history_dump_interval_minutes: int = 1): + account_history_dump_interval_minutes: int = 10): # TODO: Add database to store the balances of each account each time it is updated. self.secrets_manager = ETHKeyFileSecretManger(CONFIG_PASSWORD) self.accounts = {} From 62e50e099615ba80976a76be669fb4793482c52b Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 26 Jun 2024 17:05:32 +0200 Subject: [PATCH 4/7] (feat) add gitignore --- bots/conf/controllers/.gitignore | 0 bots/conf/scripts/.gitignore | 0 bots/data/.gitignore | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 bots/conf/controllers/.gitignore create mode 100644 bots/conf/scripts/.gitignore create mode 100644 bots/data/.gitignore diff --git a/bots/conf/controllers/.gitignore b/bots/conf/controllers/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/bots/conf/scripts/.gitignore b/bots/conf/scripts/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/bots/data/.gitignore b/bots/data/.gitignore new file mode 100644 index 0000000..e69de29 From d49d6f46789229224d0f27816b4f02dc9db6c93c Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 26 Jun 2024 18:48:01 +0200 Subject: [PATCH 5/7] (feat) fetch balances after adding a new connector --- services/accounts_service.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/accounts_service.py b/services/accounts_service.py index fd34c75..e4d0189 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -262,8 +262,9 @@ async def add_connector_keys(self, account_name: str, connector_name: str, keys: setattr(connector_config, key, value) BackendAPISecurity.update_connector_keys(account_name, connector_config) new_connector = self.get_connector(account_name, connector_name) - self.accounts[account_name][connector_name] = new_connector await new_connector._update_balances() + self.accounts[account_name][connector_name] = new_connector + await self.update_account_state() def get_connector(self, account_name: str, connector_name: str): """ From c2838b2d9f8c5b57ff4bb1ad1a267354ab19908d Mon Sep 17 00:00:00 2001 From: cardosofede Date: Wed, 26 Jun 2024 18:52:46 +0200 Subject: [PATCH 6/7] (feat) dump account state after adding a new key --- services/accounts_service.py | 1 + 1 file changed, 1 insertion(+) diff --git a/services/accounts_service.py b/services/accounts_service.py index e4d0189..d54ed1c 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -265,6 +265,7 @@ async def add_connector_keys(self, account_name: str, connector_name: str, keys: await new_connector._update_balances() self.accounts[account_name][connector_name] = new_connector await self.update_account_state() + await self.dump_account_state() def get_connector(self, account_name: str, connector_name: str): """ From c57f473b1e2b257d9e2d5c4ed4be5e98d1fdd2d7 Mon Sep 17 00:00:00 2001 From: cardosofede Date: Fri, 28 Jun 2024 10:34:51 +0200 Subject: [PATCH 7/7] (feat) add event to dump account with information --- services/accounts_service.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/services/accounts_service.py b/services/accounts_service.py index d54ed1c..82567cb 100644 --- a/services/accounts_service.py +++ b/services/accounts_service.py @@ -30,14 +30,15 @@ def __init__(self, update_account_state_interval_minutes: int = 1, default_quote: str = "USDT", account_history_file: str = "account_state_history.json", - account_history_dump_interval_minutes: int = 10): + account_history_dump_interval_minutes: int = 1): # TODO: Add database to store the balances of each account each time it is updated. self.secrets_manager = ETHKeyFileSecretManger(CONFIG_PASSWORD) self.accounts = {} + self.accounts_state = {} + self.account_state_update_event = asyncio.Event() self.initialize_accounts() self.update_account_state_interval = update_account_state_interval_minutes * 60 self.default_quote = default_quote - self.accounts_state = {} self.history_file = account_history_file self.account_history_dump_interval = account_history_dump_interval_minutes @@ -76,6 +77,7 @@ async def dump_account_state_loop(self): The loop that dumps the current account state to a file at fixed intervals. :return: """ + await self.account_state_update_event.wait() while True: try: await self.dump_account_state() @@ -226,6 +228,7 @@ async def update_account_state(self): "value": float(price * balance["units"]), "available_units": float(connector.get_available_balance(balance["token"])) }) + self.account_state_update_event.set() except Exception as e: logging.error( f"Error updating balances for connector {connector_name} in account {account_name}: {e}")