Skip to content

Commit

Permalink
[Fix] Allow starknet contract addresses starting with '0x0' (#1181)
Browse files Browse the repository at this point in the history
Fixes #1141
  • Loading branch information
baitcode authored Jan 9, 2025
1 parent 2026ab9 commit 36c760a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/dipdup/config/starknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/configs/demo_starknet_events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 36c760a

Please sign in to comment.