From 36c760ade42f7fec0fa3854020d88c5022bdba5c Mon Sep 17 00:00:00 2001 From: Ilia Batii Date: Thu, 9 Jan 2025 22:43:49 +0300 Subject: [PATCH] [Fix] Allow starknet contract addresses starting with '0x0' (#1181) Fixes #1141 --- src/dipdup/config/starknet.py | 14 ++++++++++---- tests/configs/demo_starknet_events.yml | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/dipdup/config/starknet.py b/src/dipdup/config/starknet.py index 67d36ca69..466fa48d7 100644 --- a/src/dipdup/config/starknet.py +++ b/src/dipdup/config/starknet.py @@ -22,6 +22,8 @@ _HEX_ADDRESS_REGEXP = re.compile(r'(0x)?[0-9a-f]{1,64}', re.IGNORECASE | re.ASCII) +# Spec: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json +_TRUNCATED_STARKNET_ADDRESS_REGEXP = re.compile(r'^0x(0|[a-fA-F1-9]{1}[a-fA-F0-9]{0,62})$', re.ASCII) def _validate_starknet_address(v: str) -> str: """ @@ -32,15 +34,19 @@ def _validate_starknet_address(v: str) -> str: return v if _HEX_ADDRESS_REGEXP.fullmatch(v) is None: - raise ValueError(f'{v} is not a valid Starknet contract address') + raise ValueError(f'{v} is not a valid contract address (check if it is a hex string in the form 0x[64 hex chars])') + # Following code is similar to: + # https://github.com/software-mansion/starknet.py/blob/a8d73538d409d9ef7c756921e43d10925f2838bc/starknet_py/net/client_utils.py#L60 + # starknet_py.net.client_utils._to_rpc_felt method + # # Convert hex to decimal and check if it's less than 2**251 numeric_value = int(v, 16) - if not (numeric_value < 2**251): + truncated_value = hex(numeric_value) + if not _TRUNCATED_STARKNET_ADDRESS_REGEXP.fullmatch(truncated_value): raise ValueError(f'{v} is not a valid Starknet contract address') - # TODO: Probably needs to be to normalized as in EVM case - return v + return truncated_value type StarknetAddress = Annotated[Hex, AfterValidator(_validate_starknet_address)] diff --git a/tests/configs/demo_starknet_events.yml b/tests/configs/demo_starknet_events.yml index e4afe7026..837ff84c9 100644 --- a/tests/configs/demo_starknet_events.yml +++ b/tests/configs/demo_starknet_events.yml @@ -7,7 +7,7 @@ datasources: url: https://v2.archive.subsquid.io/network/starknet-mainnet node: kind: starknet.node - url: https://starknet-mainnet.g.alchemy.com/v2 + url: https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_7/ contracts: stark_usdt: