question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Watch for and parse events across multiple instances of a contract

See original GitHub issue

I 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:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
zemsecommented, Oct 14, 2020

Oh yeah, I just noticed that you had (log, event) => { there. I assume event 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:

provider.on(filter, (log) => {
    // Emitted whenever the above topic(s) occur
    const event = interface.parseLog(log);
    console.log("results of event logging", log, event)
})

Edit: Just curious on where did you see (log, event) => {? Or was it something you were trying?

1reaction
curldappscommented, Oct 14, 2020

this works, thanks so much for the help. I missed the interface fragment stuff in docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Guide to Events and Logs in Ethereum Smart Contracts
A technical introduction to use cases for events and logs on the Ethereum blockchain with sample code.
Read more >
How to watch log for all instances of a specific type of contract?
Attaching a watcher for every instance of a contract is not a good idea in terms of memory and CPU. So is there...
Read more >
How to Sync and Store Smart Contract Events for Analytics
Sync and store smart contract events using Moralis. Analyze cryptocurrency transactions with Python and Moralis.
Read more >
Neufund/smart-contract-watch: A tool to monitor a ... - GitHub
A smart contract monitoring tool. It can monitor smart contracts activity and interactions based on generated transactions and events.For example, It can be ......
Read more >
How to Query and Monitor Ethereum Contract Events with Web3
You can use contract.events.EventName() to specify actions for specific event types. For example, Transfer : contract.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found