main.py
and paste the following code:
os
, asyncio
, and logging
for general purpose use, and Web3
and geth_poa_middleware
from the web3
library to interact with the Ethereum blockchain.
ENDPOINT_1
, ENDPOINT_2
, and ENDPOINT_3
with the actual URLs of your Ethereum nodes.
address
field specifies the WETH contract address and the topics
field contains the signature for the Transfer event.
seen_events
is used to track event identifiers that have already been processed. This helps in preventing duplicate handling of the same event.
handle_event
function processes the received events. It constructs a unique event identifier from the transaction hash and log index. If the event has not been seen before, it logs the event and adds the identifier to the seen_events
set.
subscribe_to_logs
function continuously tries to connect to an Ethereum node, sets up a filter for the specified events, and processes new entries as they arrive. If the connection fails, it waits for 5 seconds before retrying.
main
function creates tasks for each endpoint and runs them concurrently using asyncio.gather
. The script starts by calling asyncio.run(main())
.
endpoints
list and run the command in the terminal:
event
in handle_event
and you can use it for further processing: