Skip to content

Commit 295cd38

Browse files
committed
v0.1.31 added NO_SUBSQUID_LOGS env to disable use of SubSquid when getting logs. Version compatibility fixes and caching in SubSquid
1 parent af503e3 commit 295cd38

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from time import sleep
23
from typing import Optional, TypedDict, Sequence
34

@@ -138,7 +139,7 @@ def get_logs(
138139
show_progress_bar: bool = False,
139140
p_bar=None,
140141
no_retry: bool = False,
141-
use_subsquid: bool = True,
142+
use_subsquid: bool = os.getenv("NO_SUBSQUID_LOGS") is None,
142143
get_logs_by_block_hash: bool = False
143144
) -> list[LogReceipt]:
144145
filter_block_range = self.w3.filter_block_range

IceCreamSwapWeb3/Subsquid.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,24 @@
1010
endpoint_cache: dict[int, str] | None = None
1111
def get_endpoints() -> dict[int, str]:
1212
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
1723

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"))
2129

22-
endpoint_cache = endpoints
23-
return endpoints
30+
return latest_block_cache
2431

2532
def get_text(url: str) -> str:
2633
res = requests.get(url)
@@ -44,7 +51,7 @@ def get_filter(
4451
from_block: int = filter_params['fromBlock']
4552
to_block: int = filter_params['toBlock']
4653

47-
latest_block = int(get_text(f"{gateway_url}/height"))
54+
latest_block = get_latest_subsquid_block(gateway_url)
4855

4956
if from_block > latest_block:
5057
raise ValueError(f"Subsquid has only indexed till block {latest_block}")
@@ -76,13 +83,15 @@ def get_filter(
7683
topics = filter_params["topics"]
7784
assert len(topics) <= 4
7885
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):
8190
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()]
8493
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]]
8695
query["logs"][0][f"topic{i}"] = topic
8796

8897
logs: list[LogReceipt] = []

0 commit comments

Comments
 (0)