[Ethereum] Web3.js Filter Events


My Dapp needs to dynamically deploy contracts. As such, I need to store the addresses of these contracts somewhere and opted for the following solution:

  1. Log creation event
  2. Retrieve contracts based on logs

The snippet which performs logging is this one:

event Created(address indexed _who, address indexed _to_whom, uint money, uint _max_duration);


Created(msg.sender, _to_address, money_quantity, _contract_max_duration);

In web3.js I'm having a hard time trying to understand how to filter logs based on: Contract Type, _who parameter and eventually _to_whom parameter.

I'm getting all the logs (in js):

var filter = web3.eth.filter({fromBlock:0, toBlock: 'latest', topics: [/* ????!!! */]});

filter.get(function(error, result) {

In the console I get the hashes of 3 "topics" which I suggest, by reverse engineering, are the Contract Type hash and the other two indexed event parameters.

Let's say that I want to query for all events of type Contract Type having the current user as _who parameter. How do I do it? What do I insert in the topics argument?

Many thanks

Best Answer

From the experience, the issue of using the filter is quite complicated, since the use of callback and synchronization with the blockchain make this type of utilities difficult.

To begin with, I tell you that the ideal would be to filter through the exact block that is the only one that gives me resultant feedback and does not give me failures. The fastest would be to use an external log to save the transactions and addresses.

I also recommend that you see this API, so you know a little how to work with these topics.

Tea I leave a pretty useful link with a case similar to what you try. Link

Related Topic