Skip to content

How to continously get current block number #1

@ma7555

Description

@ma7555

Hello,
I have seen your comment here

I am trying to do the same using sockets, this is my adapted code but it does not seem to work

import asyncio
from web3_proxy_providers import AsyncSubscriptionWebsocketProvider
from connector import provider_quicknode_wss


async def callback(subs_id: str, json_result):
    print(json_result)


async def main(loop: asyncio.AbstractEventLoop):
    provider = AsyncSubscriptionWebsocketProvider(
        loop=loop,
        websocket_endpoint_uri="wss_url",
    )

    subscription_id = await provider.subscribe(
        ["logs", {"address": "newHeads", "topics": []}], callback
    )
    print(f"Subscribed with id {subscription_id}")

    # unsubscribe after 30 seconds
    await asyncio.sleep(30)
    await provider.unsubscribe(subscription_id)


if __name__ == "__main__":
    async_loop = asyncio.get_event_loop()
    async_loop.run_until_complete(main(loop=async_loop))

c:\Users\ma7mo\Documents\Github\pcs-preds\asyncio_latest_block.py:31: DeprecationWarning: There is no current event loop
  async_loop = asyncio.get_event_loop()
C:\Users\ma7mo\mambaforge\envs\bsc\lib\site-packages\web3\providers\async_base.py:47: UserWarning: Async providers are still being developed and refined. Expect breaking changes in minor releases.
  warnings.warn(
{'code': -32602, 'message': 'invalid argument 1: invalid address: hex string without 0x prefix'}
Traceback (most recent call last):
  File "c:\Users\ma7mo\Documents\Github\pcs-preds\asyncio_latest_block.py", line 32, in <module>
  File "c:\Users\ma7mo\Documents\Github\pcs-preds\asyncio_latest_block.py", line 20, in main
    subscription_id = await provider.subscribe(
  File "C:\Users\ma7mo\mambaforge\envs\bsc\lib\site-packages\web3_proxy_providers\providers\async_websocket_subscription.py", line 264, in subscribe   
    subscription_id = result['result']
TypeError: 'NoneType' object is not subscriptable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions