@@ -445,12 +445,12 @@ the user is done with it.
445
445
.. code- block:: python
446
446
447
447
>> > async def new_heads_handler(
448
- ... context : NewHeadsSubscriptionContext,
448
+ ... handler_context : NewHeadsSubscriptionContext,
449
449
... ) -> None :
450
- ... result = context .result
450
+ ... result = handler_context .result
451
451
... print (f " New block header: { result} \n " )
452
452
... if result[" number" ] > 1234567 :
453
- ... await context .subscription.unsubscribe()
453
+ ... await handler_context .subscription.unsubscribe()
454
454
455
455
>> > async def ws_subscription_example():
456
456
... async with AsyncWeb3(WebSocketProvider(f " ws://127.0.0.1:8546 " )) as w3:
@@ -470,7 +470,17 @@ The manager can also subscribe to many subscriptions at one time. The
470
470
friendly API for managing subscriptions. Since each connection and provider instance
471
471
has its own message listener task and subscription manager instance, you can subscribe
472
472
to many subscriptions at once and handle the many responses that come in over the socket
473
- connections.
473
+ connections via handlers. The handlers contain:
474
+
475
+ - `` async_w3`` : The `` AsyncWeb3`` instance that the subscription was made on.
476
+ - `` subscription`` : The subscription instance that the handler is attached to.
477
+ - `` result`` : The response that came in over the socket connection for the subscription.
478
+
479
+ Subscriptions also accept a `` handler_context`` argument that can be used to pass
480
+ additional information to the handler when subscribing to a subscription. This can be
481
+ used to pass in an event object , for example, that can be used to parse a log event
482
+ when it comes in .
483
+
474
484
475
485
.. code- block:: python
476
486
@@ -490,23 +500,28 @@ connections.
490
500
... )
491
501
492
502
>> > async def new_heads_handler(
493
- ... context : NewHeadsSubscriptionContext,
503
+ ... handler_context : NewHeadsSubscriptionContext,
494
504
... ) -> None :
495
- ... print (f " New block header: { context.result} \n " )
496
- ... if context.result[" number" ] > 1234567 :
497
- ... await context.subscription.unsubscribe()
505
+ ... header = handler_context.result
506
+ ... print (f " New block header: { header} \n " )
507
+ ... if header[" number" ] > 1234567 :
508
+ ... await handler_context.subscription.unsubscribe()
498
509
499
510
>> > async def pending_txs_handler(
500
- ... context : PendingTxSubscriptionContext,
511
+ ... handler_context : PendingTxSubscriptionContext,
501
512
... ) -> None :
502
513
... ...
503
514
504
515
>> > async def log_handler(
505
- ... context : LogsSubscriptionContext,
516
+ ... handler_context : LogsSubscriptionContext,
506
517
... ) -> None :
507
- ... ...
518
+ ... log_receipt = handler_context.result
519
+ ... # event is now available in the handler context, because we pass it to in the
520
+ ... # ``handler_context`` when subscribing to the log
521
+ ... event_data = handler_context.event.process_log(log_receipt)
522
+ ... print (f " Log event data: { event_data} \n " )
508
523
509
- >> > async def sx_manager ():
524
+ >> > async def sub_manager ():
510
525
... local_w3 = await AsyncWeb3(AsyncIPCProvider(LOCAL_IPC , label = " mainnet-ipc" ))
511
526
... # subscribe to many subscriptions via the subscription manager with handlers
512
527
... await local_w3.subscription_manager.subscribe(
@@ -522,6 +537,8 @@ connections.
522
537
... address = local_w3.to_checksum_address(" 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" ),
523
538
... topics = [HexStr(" 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" )],
524
539
... handler = log_handler,
540
+ ... # optional ``handler_context`` args to help parse a response
541
+ ... handler_context = {" event" : my_event},
525
542
... ),
526
543
... ]
527
544
... )
@@ -538,7 +555,7 @@ connections.
538
555
... local_w3.subscription_manager.handle_subscriptions(),
539
556
... )
540
557
541
- >> > asyncio.run(sx_manager ())
558
+ >> > asyncio.run(sub_manager ())
542
559
543
560
544
561
The `` process_subscriptions()`` method on the
0 commit comments