Websocket Readme demo code issue
shaft3796 opened this issue · comments
I have an issue with the demo websocket code snippet, maybe it is due to a package version issue ?
Initial snippet
import asyncio
from asyncstdlib import enumerate
from solana.rpc.websocket_api import connect
async def main():
async with connect("wss://api.devnet.solana.com") as websocket:
await websocket.logs_subscribe()
first_resp = await websocket.recv()
subscription_id = first_resp.result
next_resp = await websocket.recv()
print(next_resp)
await websocket.logs_unsubscribe(subscription_id)
# Alternatively, use the client as an infinite asynchronous iterator:
async with connect("wss://api.devnet.solana.com") as websocket:
await websocket.logs_subscribe()
first_resp = await websocket.recv()
subscription_id = first_resp.result
async for idx, msg in enumerate(websocket):
if idx == 3:
break
print(msg)
await websocket.logs_unsubscribe(subscription_id)
asyncio.run(main())
Error:
Error at lines 9 and 18
AttributeError: 'list' object has no attribute 'result'
Proposed solution
import asyncio
from asyncstdlib import enumerate
from solana.rpc.websocket_api import connect
async def main():
async with connect("wss://api.devnet.solana.com") as websocket:
await websocket.logs_subscribe()
first_resp = await websocket.recv()
subscription_id = first_resp[0].result
next_resp = await websocket.recv()
print(next_resp)
await websocket.logs_unsubscribe(subscription_id)
# Alternatively, use the client as an infinite asynchronous iterator:
async with connect("wss://api.devnet.solana.com") as websocket:
await websocket.logs_subscribe()
first_resp = await websocket.recv()
subscription_id = first_resp[0].result
async for idx, msg in enumerate(websocket):
if idx == 3:
break
print(msg)
await websocket.logs_unsubscribe(subscription_id)
asyncio.run(main())
Changes:
Lines 9 and 18
from "subscription_id = first_resp.result" to "subscription_id = first_resp[0].result"
Works fine now:
[LogsNotification {
result: LogsNotificationResult {
context: RpcResponseContext {
slot: 226055744,
api_version: None,
},
value: RpcLogsResponse(
. . .
"Program log: scaled_val: 44176526825",
"Program suj4uh547AdFJztkPY1DsTCynJWpUn6VS33DFkVSLmb consumed 13699 of 1262332 compute units",
"Program suj4uh547AdFJztkPY1DsTCynJWpUn6VS33DFkVSLmb success",
],
},
),
},
subscription: 34373780,
}]
Thank you! Fixing now