0

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!

1 Answer 1

0

I found the problem. When passing multiple topics into the subscribe the topics themselves need to be within a list inside the "topics" list, like so:

create_close_position_topic = '0x..70'
create_open_position_topic = '0x0..0'
execute_n_positions_with_prices_topic = '0x..7'



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": ['0x..a5'],
                    "topics": [[create_close_position_topic, create_open_position_topic, execute_n_positions_with_prices_topic]]
                    }]}))

I still don't know why the method signatures are not lining up, but if I figure that out, I will update this answer as well.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.