Apibara Blog

News and updates about Apibara

Follow us on Twitter

3 Oct 2022

Dynamic event filters

This week's release of the Python SDK brings dynamic event filters! Dynamic event filters enable developers to add event filters while indexing, for example in response to another event.

Use cases for dynamic event filters include indexing:

  • DEX pools that are created by pool factories,
  • game rounds that are created by the main instance of the on-chain game.

To try out this feature, upgrade the apibara package to version 0.5.4.

After that you can subscribe to new events directly in your event handler. For example, the following snippet shows how to decode a PairCreated event from a Uniswap V2-like DEX and then subscribe to the Swap and Sync events on the new trade pair.

async def handle_events(info: Info, block_events: NewEvents):
for event in block_events.events:
if event.name == "PairCreated":
pair_created = decode_event(
pair_created_decoder, event.data)
info.add_event_filters(filters=[
EventFilter.from_event_name(
"Swap", address=pair_created.pair),
EventFilter.from_event_name(
"Sync", address=pair_created.pair),
])

You can find more information about dynamic filters in the documentation.

Open-source on-chain data streaming platform secured by Zero Knowledge proofs.

Resources

Blog

© 2023 GNC Labs Limited. All rights reserved.