from eth_abi import decode
from eth_utils import event_abi_to_log_topic, to_checksum_address
from bloklake import Bloklake
bl = Bloklake(chain="eth")
SWAP_V2 = {"type": "event", "name": "Swap", "inputs": [
{"name": "sender", "type": "address", "indexed": True},
{"name": "amount0In", "type": "uint256", "indexed": False},
{"name": "amount1In", "type": "uint256", "indexed": False},
{"name": "amount0Out", "type": "uint256", "indexed": False},
{"name": "amount1Out", "type": "uint256", "indexed": False},
{"name": "to", "type": "address", "indexed": True},
]}
topic0 = "0x" + event_abi_to_log_topic(SWAP_V2).hex()
page = bl.query({
"from_block": 19426000, "to_block": 19426099,
"logs": [{"topic0": [topic0]}],
"fields": {"log": ["block_number", "address", "topic1", "topic2", "data"]},
})
for log in page.logs[:2]:
a0in, a1in, a0out, a1out = decode(["uint256"] * 4, bytes.fromhex(log["data"][2:]))
sender = to_checksum_address("0x" + log["topic1"][-40:])
print(log["block_number"], to_checksum_address(log["address"]), "sender", sender, "in", a0in, a1in, "out", a0out, a1out)