-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Listening for events #492
Comments
Hey there. You can do something like this (for events in the future)
To read past events, you can do something like
Get the topic from the ethers.js |
@mrwillis Could you elaborate a bit how does the first example works internally? How is the loop created, how to control it? I'm using similar code plus |
When you call resetEventsBlock, it does optimize log events, and fetch them all at once using a filter with the If you need to pagination it more, you should use the Make sense? |
Hello @ricmoo @mrwillis
And the returned structure is like this:
I tried to use the provided examples above for listening the future events, but all I am able to catch is the |
Thanks. I have yet to digest how to work with Can you explain how does |
I've tried it, but I don't understand how one could process events from the received logs. I used |
So I managed to do this using something like:
But I have more broad question to @ricmoo (or other person who could perhaps answer it). Previously I used |
@reuptaken Possible answer at #505 ? |
So, internally all it does is query from the last queried block to the current block number (on every block). This works in general, but can certainly cause issues with resetBlockNumber, since you are asking for a much larger range. There isn’t really a good way to break up the block ranges in the library. Any max block span could still suffer from the same problem, which is too many events in within that span (e.g. ICO airdrop results in MANY events, even a block span of a week could be too many). And any max span would be far too small for most events, since most events are sparse. A span of a week would likely take hours to days to fetch all the events, when a single shot would have returns the 10 results in about 4 seconds. If you produce a system that need historic access to events, it is probably best to build a caching service. Also, I will be looking into the Alchemy event API more, which may have something that could help in these cases. I had not thought of this, but in talks regarding Eth2.0, I will bring up pagination for lots in the kernels. I do have a new idea that is kinda interesting, and I might experiment with it, even if just to write a blog... have an eventBlock event, which triggers every 200 events (the contract would track this and emit the additional event). Then anything interested in the events, can query this sparser event to know what ranges to target. I’m not actually suggesting this, as it is only a little-c solution, just something you made me think about. :) |
I think this issue is mostly resolved and stale, so I'm going to close it. If you have further issues though, please feel free to re-open it. As a quick note, the v5 API makes this much simpler. With a Contract, you can use the There still isn't a built-in useful way to paginate against the JSON-RPC, but that's the best we can do for now. Thanks! :) |
@ricmoo but if we want to subscribe to events, and only get for future events, there is no way to do that with contract.on() correct? queryFilter is only going to allow for a polling approach as opposed to being notified. The only way I have found for this to work, is to get the current block number getBlockNumber and check on every fired event that I am subscribed to (contract.on(filter, callback)) and react to only block number above the current block number. @mrwillis previous example of getting only future events, does produce events from the past as well. |
Hello,
Does anybody know how to listen for events with ethers.js.
The following code is implemented using web3:
but I need to do it with ethers.js.
Thank you.
The text was updated successfully, but these errors were encountered: