solana-py icon indicating copy to clipboard operation
solana-py copied to clipboard

How to use set_log_filter function in solana rpc client

Open devlancer412 opened this issue 2 years ago • 12 comments

I tried to get all logs of solana network with following code

solana_client = Client(cfg.SOL_RPC_URL)
filter = solana_client.set_log_filter("solana_core=debug")
while True:
    await filter.update()
    items = filter.items()
    print(items)
    for log in items:
      await handle_log(log)
    time.sleep(poll_interval)

But such error occurred

{'jsonrpc': '2.0', 'error': {'code': -32601, 'message': 'Method not found'}, 'id': 1}

What's the reason of this error?

devlancer412 avatar Jul 17 '22 02:07 devlancer412

where did you get this code from? I see that you are awaiting a non coroutine object.

Client is sync and not async. you will need to use AsyncClient.

then what is update()? and items()? and you cannot use time.sleep() in asyncio, you will have to use asyncio.sleep()

crypt0miester avatar Jul 21 '22 11:07 crypt0miester

Can you give me a code to listen transaction from network. I used websocket client, but it sometimes is disconnected and therefore can't listen some transaction.

devlancer412 avatar Jul 21 '22 12:07 devlancer412

Can you give me a code to listen transaction from network. I used websocket client, but it sometimes is disconnected and therefore can't listen some transaction.

here you go.

import asyncio
from solana.rpc.websocket_api import connect

async def main():
    async with connect("wss://api.devnet.solana.com") as websocket:
        await websocket.logs_subscribe() # or .account_subscribe("address")
        first_resp = await websocket.recv()
        subscription_id = first_resp.result
        next_resp = await websocket.recv()
        print(next_resp)
        await websocket.logs_unsubscribe(subscription_id)


asyncio.run(main())

crypt0miester avatar Jul 21 '22 13:07 crypt0miester

I used that code.

But if wss connection break down because some network state, we need to reconnect that, it take more than a min. so we can't receive logs in that time.

So I tried to use web3 endpoint like ethereum.

devlancer412 avatar Jul 21 '22 13:07 devlancer412

I used that code.

But if wss connection break down because some network state, we need to reconnect that, it take more than a min. so we can't receive logs in that time.

So I tried to use web3 endpoint like ethereum.

use while 1: it will run indefinitely.

crypt0miester avatar Jul 21 '22 14:07 crypt0miester

LOL. if connection closed, we need to reconnect, it take some time. we can't get logs during that time.

devlancer412 avatar Jul 21 '22 14:07 devlancer412

LOL. if connection closed, we need to reconnect, it take some time. we can't get logs during that time.

alright. goodluck then.

note that github is with issues related to the code. you might find an answer on stackoverflow.

crypt0miester avatar Jul 21 '22 14:07 crypt0miester

Thanks for your time. Why you answer my question. You aren't enough to answer.

devlancer412 avatar Jul 21 '22 14:07 devlancer412

The set_log_filter RPC method seems to be removed from the Solana nodes. We should remove this RPC method.

michaelhly avatar Jul 22 '22 20:07 michaelhly

@michaelhly So there aren't any way to get real time logs from node without use wss?

devlancer412 avatar Jul 22 '22 23:07 devlancer412

No. There is not.

michaelhly avatar Jul 22 '22 23:07 michaelhly

Okay. thanks.

devlancer412 avatar Jul 23 '22 00:07 devlancer412

The set_log_filter RPC method seems to be removed from the Solana nodes. We should remove this RPC method.

Closed by #291

kevinheavey avatar Sep 30 '22 16:09 kevinheavey