2

Here is what the doc says:

Up to three parameters can receive the attribute indexed which will cause the respective arguments to be searched for: It is possible to filter for specific values of indexed arguments in the user interface.

OK, so I understand that I can define an event, for example, with three indexed parameters:

event Event(address indexed from, address indexed to, uint indexed id, uint value);

But what exactly is the benefit of using that indexed keyword?

Up until now, I've been defining events with no indexing, for example:

event Event(uint value);

Then, on the web3-client side, I retrieve the value of the event as follows:

let hash = await myContract.myFunc(...);
let data = web3.toBigNumber(hash.receipt.logs[0].data);

If I know that several events are emitted during the execution on the blockchain side, then instead of 0, I use the index of the specific event which I want to retrieve.

So I'm guessing that perhaps this indexed keyword can be used in order to retrieve events on the client side in a cleaner manner.

Am I right, and if yes, how exactly would I retrieve an indexed event on the client side differently than how I am retrieving it at present?

Thank you.

3

Indexing can be used for filtering the events for specific values.

 var myEvent = contract.Event({from:"0xc32c36e7cea09c00e0dbd4bb4e72b8baaa51fd45"},{fromBlock: 0, toBlock: 'latest'});
    myEvent.get((error, result) => {
                     console.log(result);
    });

Here I am filtering the events based on the from param having a specific value(0xc32c36e7cea09c00e0dbd4bb4e72b8baaa51fd45). It will return the event logs for the particular address instead of all event logs.

  • Thank you. Can you please provide your event definition on the blockchain side (i.e., in the Solidity contract)? Also, though not directly related to my question, in my JS test, the value of hash.receipt.logs[0].data consists of all the parameters of the event, concatenated together into an hexadecimal string. Does web3 provide a convenient API for extracting the value of each parameter? Thanks!!! – goodvibration Feb 21 '18 at 9:59
  • @goodvibration You can use the above method for extracting each param.web3 provides another API for your requirement web3.eth.filter(filterString); Reference :- github.com/ethereum/wiki/wiki/JavaScript-API#web3ethfilter – Crissi Mariam Robert Feb 21 '18 at 10:14
  • I've found an answer to my question here and here. Awesome, thank you very much!!! – goodvibration Feb 21 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.