Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregate event subscriptions #91

Open
norswap opened this issue Dec 17, 2023 · 5 comments
Open

Aggregate event subscriptions #91

norswap opened this issue Dec 17, 2023 · 5 comments

Comments

@norswap
Copy link
Member

norswap commented Dec 17, 2023

Currently, we're subscribing to each event separately. This was a limitation of viem at the time I set that up, and maybe it still is.

This needs to change, even if we have to bypass Viem, because we're listening for A LOT of events and currently we're positively hammering the RPC with eth_getFilterChanges requests.

@norswap norswap self-assigned this Dec 17, 2023
@jxom
Copy link

jxom commented Dec 17, 2023

You can listen to multiple topics in Viem too: https://viem.sh/docs/actions/public/watchEvent.html#multiple-events

The caveat here is that you cannot scope by indexed arguments (JSON-RPC/topic design limitation).

Understand you are using watchContractEvent, maybe it could make sense to add support for multiple eventNames, but for now I guess you could just use the lower-level watchEvent.

@norswap
Copy link
Member Author

norswap commented Dec 18, 2023

Fantastic, thanks a lot for the help, I'll use that :)

@debuggingfuture
Copy link

debuggingfuture commented Feb 15, 2024

come across this and thanks @jxom for the advice

so according to the docs even with watchFilter, we can't meaningfully use single filter to filter for both criteria (list of events, current gameId which is in args)

The caveat here is that you cannot scope by indexed arguments (JSON-RPC/topic design limitation).

I digged the implementation a bit, viem actually do apply the arg when creating the filter

at https://github.com/wevm/viem/blob/81ae35284e22164e8f8ac4d3b44f846b83f548f1/src/actions/public/watchEvent.ts#L195

which calls

https://github.com/wevm/viem/blob/81ae35284e22164e8f8ac4d3b44f846b83f548f1/src/actions/public/createEventFilter.ts#L194

but the catch is the arg in filter has to be consistent across events -- i.e. game id is the only thing we filter in all event, which is true for the current contract & use case

from what I see with this, it did end up using one eth_getFilterChanges rpc call for all events (as in the logs)

Let me know if we're good with this hack I can PR

@debuggingfuture
Copy link

@norswap btw will recommend do a simple rxjs wrapper for easier event filtering & async control, where it also integrates well with jotai from my understanding if we need that in future https://jotai.org/docs/utilities/async#atomwithobservable

@norswap
Copy link
Member Author

norswap commented Feb 16, 2024 via email

debuggingfuture added a commit to debuggingfuture/0xFable that referenced this issue Feb 16, 2024
debuggingfuture added a commit to debuggingfuture/0xFable that referenced this issue Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants