I am trying to listen on a websocket connection to an optimism node for specific events on a specific contract. I am getting the message back that I am connected to the node, but even though I see events happen on chain, I get nothing from the WS in my shell. Here is my redacted code:
import asyncio
import json
from web3 import Web3
from web3.middleware import geth_poa_middleware # only needed for PoA networks like BSC
import requests
from websockets import connect
from eth_abi import decode_single, decode_abi
adapter = requests.sessions.HTTPAdapter(pool_connections=50000, pool_maxsize=50000) # pool connections and max size are for HTTP calls only, since we are using WS they are not needed.
session = requests.Session()
w3 = Web3(Web3.WebsocketProvider("wss://opt-mainnet.g.alchemy.com/v2/REDACTED"))
w3.middleware_onion.inject(geth_poa_middleware, layer=0) # only needed for PoA networks like BSC
async def get_event():
async with connect("wss://opt-mainnet.g.alchemy.com/v2/REDACTED") as ws:
await ws.send(json.dumps({"id": 1, "method": "eth_subscribe", "params": ["logs", {
"address": ['0xREDACTED'],
"topics": ['0xREDACTED']}]}))
subscription_response = await ws.recv()
print(subscription_response)
while True:
try:
message = await asyncio.wait_for(ws.recv(), timeout=60)
print(message)
decoded = decode_single('(address[],uint256[],uint256,address)',bytearray.fromhex(json.loads(message)['params']['result']['data'][2:]))
print(list(decoded))
pass
except:
pass
if __name__ == "__main__":
loop = asyncio.get_event_loop()
while True:
loop.run_until_complete(get_event())
For the "topics" key I am using the MethodID hash found on etherscan as a string, could that be the problem?
This is the only output I get:
{"id":1,"result":"0xf9d165f0d0edccfda86d40cf113f9bd2","jsonrpc":"2.0"}
Thanks!