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()