10
10
endpoint_cache : dict [int , str ] | None = None
11
11
def get_endpoints () -> dict [int , str ]:
12
12
global endpoint_cache
13
- if endpoint_cache is not None :
14
- return endpoint_cache
15
- res = requests .get ("https://cdn.subsquid.io/archives/evm.json" )
16
- res .raise_for_status ()
13
+ if endpoint_cache is None :
14
+ res = requests .get ("https://cdn.subsquid.io/archives/evm.json" )
15
+ res .raise_for_status ()
16
+
17
+ endpoints : dict [int , str ] = {}
18
+ for chain in res .json ()["archives" ]:
19
+ endpoints [chain ["chainId" ]] = chain ["providers" ][0 ]["dataSourceUrl" ]
20
+
21
+ endpoint_cache = endpoints
22
+ return endpoint_cache
17
23
18
- endpoints : dict [int , str ] = {}
19
- for chain in res .json ()["archives" ]:
20
- endpoints [chain ["chainId" ]] = chain ["providers" ][0 ]["dataSourceUrl" ]
24
+ latest_block_cache : int | None = None
25
+ def get_latest_subsquid_block (gateway_url : str ) -> int :
26
+ global latest_block_cache
27
+ if latest_block_cache is None :
28
+ latest_block_cache = int (get_text (f"{ gateway_url } /height" ))
21
29
22
- endpoint_cache = endpoints
23
- return endpoints
30
+ return latest_block_cache
24
31
25
32
def get_text (url : str ) -> str :
26
33
res = requests .get (url )
@@ -44,7 +51,7 @@ def get_filter(
44
51
from_block : int = filter_params ['fromBlock' ]
45
52
to_block : int = filter_params ['toBlock' ]
46
53
47
- latest_block = int ( get_text ( f" { gateway_url } /height" ) )
54
+ latest_block = get_latest_subsquid_block ( gateway_url )
48
55
49
56
if from_block > latest_block :
50
57
raise ValueError (f"Subsquid has only indexed till block { latest_block } " )
@@ -76,13 +83,15 @@ def get_filter(
76
83
topics = filter_params ["topics" ]
77
84
assert len (topics ) <= 4
78
85
for i in range (len (topics )):
79
- topic : str | list [str ]
80
- if isinstance (topics [i ], str ):
86
+ topic : list [str ]
87
+ if topics [i ] is None :
88
+ continue
89
+ elif isinstance (topics [i ], str ):
81
90
topic = [topics [i ]]
82
- elif hasattr (topics [i ], "hex " ):
83
- topic = [topics [i ].hex ()]
91
+ elif hasattr (topics [i ], "to_0x_hex " ):
92
+ topic = [topics [i ].to_0x_hex ()]
84
93
else :
85
- topic = [(single_topic .hex () if not isinstance (single_topic , str ) else single_topic ) for single_topic in topics [i ]]
94
+ topic = [(single_topic .to_0x_hex () if not isinstance (single_topic , str ) else single_topic ) for single_topic in topics [i ]]
86
95
query ["logs" ][0 ][f"topic{ i } " ] = topic
87
96
88
97
logs : list [LogReceipt ] = []
0 commit comments