From 5ac671820f20f2e4fa1eddb0eb66a0eb76d62c4a Mon Sep 17 00:00:00 2001 From: Uxio Fuentefria <6909403+Uxio0@users.noreply.github.com> Date: Mon, 11 Nov 2024 14:26:06 +0100 Subject: [PATCH] Optimize load of ERC20 addresses - Closes #2301 --- safe_transaction_service/history/models.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/safe_transaction_service/history/models.py b/safe_transaction_service/history/models.py index fb07f345f..091e1dc32 100644 --- a/safe_transaction_service/history/models.py +++ b/safe_transaction_service/history/models.py @@ -1764,6 +1764,22 @@ class SafeContractManager(models.Manager): def get_banned_safes(self) -> QuerySet[ChecksumAddress]: return self.filter(banned=True).values_list("address", flat=True) + def get_safes_as_bytes( + self, newer_than: Optional[datetime.datetime] = None + ) -> list[bytes]: + """ + Calculating checksum for a lot of addresses can take a lot of time. With this method we can get + them as they are stored in the database, as bytes + + :return: List of Safes as bytes + """ + query = "SELECT address FROM history_safecontract" + if newer_than: + query += f" created >= {newer_than.isoformat()}" + with connection.cursor() as cursor: + cursor.execute(query) + return [address.tobytes() for (address,) in cursor.fetchall()] + class SafeContract(models.Model): objects = SafeContractManager()