Skip to content

Commit 20409c8

Browse files
committed
raise custom ForkedBlock error rather than assert block correctness and allow entire block to be specified as fromBlock and toBlock in get_logs filter parameters
1 parent 80c6d66 commit 20409c8

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
from time import sleep
2-
from typing import Optional
2+
from typing import Optional, TypedDict, Sequence
33

4-
from eth_typing import BlockNumber
4+
from eth_typing import BlockNumber, Address, ChecksumAddress
5+
from hexbytes import HexBytes
56
from web3.eth import Eth
67
from web3.exceptions import ContractLogicError
7-
from web3.types import FilterParams, LogReceipt, CallOverride, BlockIdentifier, TxParams, BlockData
8+
from web3.types import FilterParams, LogReceipt, CallOverride, BlockIdentifier, TxParams, BlockData, _Hash32
89

910
from IceCreamSwapWeb3 import Web3Advanced
1011
from IceCreamSwapWeb3.Subsquid import get_filter
1112

1213

14+
class ForkedBlock(Exception):
15+
pass
16+
17+
18+
class FilterParamsExtended(TypedDict, total=False):
19+
address: Address | ChecksumAddress | list[Address] | list[ChecksumAddress]
20+
blockHash: HexBytes
21+
fromBlock: BlockIdentifier | BlockData
22+
toBlock: BlockIdentifier | BlockData
23+
topics: Sequence[_Hash32 | Sequence[_Hash32] | None]
24+
25+
1326
def exponential_retry(func_name: str = None):
1427
def wrapper(func):
1528
def inner(*args, no_retry: bool = False, **kwargs):
@@ -121,7 +134,7 @@ def get_block(
121134

122135
def get_logs(
123136
self,
124-
filter_params: FilterParams,
137+
filter_params: FilterParamsExtended,
125138
show_progress_bar: bool = False,
126139
p_bar=None,
127140
no_retry: bool = False,
@@ -143,6 +156,10 @@ def get_logs(
143156
if isinstance(from_block_original, int):
144157
from_block_body = None
145158
from_block = from_block_original
159+
elif isinstance(from_block_original, BlockData):
160+
from_block_body = from_block_original
161+
from_block = from_block_original["number"]
162+
filter_params = {**filter_params, "fromBlock": from_block}
146163
else:
147164
from_block_body = self.get_block(from_block_original)
148165
from_block = from_block_body["number"]
@@ -151,6 +168,10 @@ def get_logs(
151168
if isinstance(to_block_original, int):
152169
to_block_body = None
153170
to_block = to_block_original
171+
elif isinstance(to_block_original, BlockData):
172+
to_block_body = to_block_original
173+
to_block = to_block_original["number"]
174+
filter_params = {**filter_params, "toBlock": to_block}
154175
else:
155176
to_block_body = self.get_block(to_block_original)
156177
to_block = to_block_body["number"]
@@ -198,11 +219,13 @@ def get_logs(
198219
block = self.get_block(block_number, no_retry=no_retry)
199220
if block_hashes:
200221
# make sure chain of blocks is consistent with each block building on the previous one
201-
assert block["parentHash"] == block_hashes[-1], f"{block_hashes[-1]=}, {block['parentHash']=}"
222+
assert block["parentHash"] == block_hashes[-1], f"{block_hashes[-1].hex()=}, {block['parentHash'].hex()=}"
202223
if block_number == from_block and from_block_body is not None:
203-
assert block["hash"] == from_block_body["hash"], f"{from_block_body['hash']=}, {block['hash']=}"
224+
if block["hash"] != from_block_body["hash"]:
225+
raise ForkedBlock(f"expected={from_block_body['hash'].hex()}, actual={block['hash'].hex()}")
204226
if block_number == to_block and to_block_body is not None:
205-
assert block["hash"] == to_block_body["hash"], f"{to_block_body['hash']=}, {block['hash']=}"
227+
if block["hash"] != to_block_body["hash"]:
228+
raise ForkedBlock(f"expected={to_block_body['hash'].hex()}, actual={block['hash'].hex()}")
206229
block_hashes.append(block["hash"])
207230

208231
single_hash_filter = filter_params.copy()

0 commit comments

Comments
 (0)