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
from time import sleep
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.