Skip to content

Commit 96018b4

Browse files
committed
properly caching chain_id in Web3Advanced, so it only gets called once.
1 parent f269d73 commit 96018b4

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class EthAdvanced(Eth):
4646
]
4747

4848
PROPERTIES_TO_RETRY = [
49-
'accounts', 'hashrate', 'block_number', 'chain_id', 'coinbase', 'gas_price',
49+
'accounts', 'hashrate', 'block_number', 'coinbase', 'gas_price',
5050
'max_priority_fee', 'mining', 'syncing'
5151
]
5252

@@ -73,6 +73,8 @@ def __init__(self, w3):
7373
if self.w3.should_retry:
7474
self._wrap_methods_with_retry()
7575

76+
self.chain_id_cached = super()._chain_id()
77+
7678
self.filter_block_range = self._find_max_filter_range()
7779

7880
def _wrap_methods_with_retry(self):
@@ -94,6 +96,8 @@ def get_logs(self, filter_params: FilterParams, show_progress_bar=False, p_bar=N
9496
if not isinstance(to_block, int):
9597
to_block = self.get_block(to_block)["number"]
9698

99+
assert to_block >= from_block, f"{from_block=}, {to_block=}"
100+
97101
# note: fromBlock and toBlock are both inclusive. e.g. 5 to 6 are 2 blocks
98102
num_blocks = to_block - from_block + 1
99103

@@ -140,7 +144,7 @@ def get_logs(self, filter_params: FilterParams, show_progress_bar=False, p_bar=N
140144
return results
141145

142146
# filter is trying to get a single block, retrying till it works
143-
assert from_block == to_block and num_blocks == 1
147+
assert from_block == to_block and num_blocks == 1, f"{from_block=}, {to_block=}, {num_blocks=}"
144148
events = exponential_retry(func_name="get_logs")(self._get_logs)(filter_params)
145149
if p_bar is not None:
146150
p_bar.update(num_blocks)
@@ -162,3 +166,7 @@ def _find_max_filter_range(self):
162166
except Exception:
163167
pass
164168
raise ValueError("Unable to use eth_getLogs")
169+
170+
def _chain_id(self):
171+
# usually this causes an RPC call and is used in every eth_call. Getting it once in the init and then not again.
172+
return self.chain_id_cached

IceCreamSwapWeb3/Web3Advanced.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def __init__(
2424
super().__init__(provider=provider, modules=modules)
2525

2626
self.middleware_onion.inject(geth_poa_middleware, layer=0, name="poa") # required for pos chains
27-
self._chain_id = self.eth.chain_id # avoids many RPC calls to get chain ID
2827

2928
@staticmethod
3029
def _construct_provider(node_url):

0 commit comments

Comments
 (0)