Skip to content

Commit

Permalink
feat: refactor checksum cache for speed (#796)
Browse files Browse the repository at this point in the history
* feat: refactor checksum cache for speed

* feat: quicker cache loader

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Nov 26, 2024
1 parent 93d0c7b commit 535832b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
6 changes: 2 additions & 4 deletions y/_db/utils/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,9 @@ def _wrap_query_with_addresses(self, generator) -> Query:
if not (addresses := self.addresses):
return generator
elif isinstance(addresses, str):
address = _remove_0x_prefix(convert.to_address(addresses))
address = convert.to_address(addresses)[2:]
return (log for log in generator if log.address.hash == address)
addresses = [
_remove_0x_prefix(convert.to_address(address)) for address in addresses
]
addresses = tuple(convert.to_address(address)[2:] for address in addresses)
return (log for log in generator if log.address.hash in addresses)

def _wrap_query_with_topic(self, generator, topic_id: str) -> Query:
Expand Down
24 changes: 21 additions & 3 deletions y/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@

@lru_cache(maxsize=ENVS.CHECKSUM_CACHE_MAXSIZE)
def checksum(address: AnyAddressType) -> ChecksumAddress:
return str(EthAddress(address))
try:
return to_checksum_address(address)
except ValueError:
raise ValueError(f"'{address}' is not a valid ETH address") from None


def to_address(address: AnyAddressType) -> ChecksumAddress:
address = _int_to_address(address) if type(address) == int else str(address)
if isinstance(address, bytes):
address = (
address.hex()
if type(address).__name__ == "HexBytes"
else HexBytes(address).hex()
)
elif type(address) == int:
address = _int_to_address(address)
else:
address = str(address)

if address in _is_checksummed:
return address
elif address in _is_not_checksummed:
return checksum(address)
checksummed = str(EthAddress(address))

try:
checksummed = to_checksum_address(address)
except ValueError:
raise ValueError(f"'{address}' is not a valid ETH address") from None

if address == checksummed:
_is_checksummed.add(address)
else:
Expand Down
4 changes: 3 additions & 1 deletion y/prices/dex/uniswap/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ def _log_factory_helper_failure(
)


_all_pools_semaphore = a_sync.Semaphore(10, name=f"{__name__}.UniswapRouterV2.all_pools_for")
_all_pools_semaphore = a_sync.Semaphore(
10, name=f"{__name__}.UniswapRouterV2.all_pools_for"
)


class UniswapRouterV2(ContractBase):
Expand Down

0 comments on commit 535832b

Please sign in to comment.