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.

2
  • Maybe this helps ethereum.stackexchange.com/questions/63745/… Commented Feb 23, 2023 at 1: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
    Commented Feb 23, 2023 at 10:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.