0

I am using eth_subscribe to log for all token transfer events that are happening on the blockchain.

Here is my code:

import asyncio
import json
from secret_config import RPC_ENDPOINT
from websockets import connect

async def get_event(): print("Started loop") async with connect(RPC_ENDPOINT) as ws: await ws.send('{"id": 1, "method": "eth_subscribe", "params": ["logs", {"topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]}]}') subscription_response = await ws.recv() print(subscription_response) # you are now subscribed to the event # you keep trying to listen to new events (similar idea to longPolling) while True: try: message = await asyncio.wait_for(ws.recv(), timeout=60) data = json.loads(message) result = data["params"]["result"] print(f"token address: {result['address']}, block: {int(result['blockNumber'], 16)}, tr_hash: {result['transactionHash']}") pass except: pass

if name == "main": loop = asyncio.get_event_loop() while True: loop.run_until_complete(get_event())

My question is: Is it 100% sure that the events will come in the right order of execution?

Or it is possible that sometimes, the events are not sent in the right order? I am planning to run this code on all EVM blockchains.

Lockface
  • 5
  • 2
  • Maybe this helps https://ethereum.stackexchange.com/questions/63745/can-we-assume-that-ethereum-events-aka-logs-are-in-sequence – MadeInDreams Feb 23 '23 at 01:41
  • @MadeInDreams thank you. The link you sent prove that the logs are stored in the right order but my query would be more to know if my code above is safe and will always receive the events in the right order or there might be a bug or something, which may make the blockchain send me an event not in the right order – Lockface Feb 23 '23 at 10:15

0 Answers0