Watch for and parse events across multiple instances of a contract
See original GitHub issueI hope all is well. I have a specific use case that I’m working through right now that essentially follows a factory pattern:
I deploy a Factory contract that deploys new instances of another contract that users interact with. My challenge is that because there are potentially hundreds of these spawned contracts, trying to react to events that are emitted from them is not feasible using the Contract API as normal. It would require an ever growing list of open connections for each contract instance.
I wanted to see if I can use the Event / Topics functionality to do this, where I can catch ANY event that matches the Topic regardless of contract address? Basically, I need a global event handler that can run in a process and respond to various events across all the instances of the user contract.
For example, let’s say I emit these events from the contracts users use:
event HelloWorld(string greeting)
event CreateElement(string elementId, address sender)
In a Node.js API for example, could I use Ethers to watch the logs for any events that match these criteria and pull the values out of those event emissions?
Notionally, I tried to use the provider.on() syntax which was able to react to the event, but I didn’t see any of the data points to extract from the event.
let filter = {
topics: [
ethers.utils.id("HelloWorld(string)"),
ethers.utils.id("CreateElement(string,address)")
]
}
provider.on(filter, (log, event) => {
// Emitted whenever the above topic(s) occur
console.log("results of event logging", log, event)
})
If this is the proper syntax above, how can I extract the contents of the event in the callback? Thanks for the help in advance 😃
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top GitHub Comments
Oh yeah, I just noticed that you had
(log, event) => {
there. I assumeevent
is the parsed version of the log. I don’t think provider can give you a parsed log, as provider does not know how to parse it (it needs an interface) so it cannot technically parse it and give you. You will only get a log and then you need an interface to parse it:Edit: Just curious on where did you see
(log, event) => {
? Or was it something you were trying?this works, thanks so much for the help. I missed the interface fragment stuff in docs.