4

I Have a contract defined which logs 2 events :

 myEvent(string data, uint32 indexed id),
 myEvent1(string data, uint32 indexed id). 

I want to filter all the events on the browser using web3 by passing the indexed id.

  1. How to get all the events with the passed id.

  2. when I try to get the event from the txHash it returns the event data but only string paramater is decode correctly. the uint32 type is returned as a Big number. How do is resolve their two issues??

  • Can you please copy the whole javascript block here which you are using to perform the filter operation – Susmit Apr 6 '17 at 13:07
5

Here I am listing an example how I have applied event filter

In this example I have used the candidate name as a filter parameter. Secondly it is looking from block 0 to latest.

Hope this helps.

Event Declaration - Solidity Contract

event RegisterCandidate(uint256 indexed
eventTimeStamp, address indexed _from, bytes32 indexed _candidateName);

Code in Java Script

var event = contractInstance.RegisterCandidate( {_candidateName: "Abc"}, {fromBlock: 0, toBlock: 'latest'});

    event.watch(function(error, response)
    {
       //once the event has been detected, take actions as desired
        var data = 'from: ' + response.args._from+"<br>candidateName: "+web3.toUtf8(response.args._candidateName) +"<br>";
    });
3

In your specific case that would be:

var myEvent = contract.myEvent({id: 1});

myEvent.watch((error, result) => {

    var data = result["args"]["data"];
    var id = result["args"]["id"]; // always 1
    // Do Something 
});

Which would watch only events with id = 1.

2

How to get all the events with the passed id.

As indicated by @cees-van-wijk, this fetches every new event:

var myEvent = contract.myEvent({id: 1});
myEvent.watch((error, result) => {
    var data = result["args"]["data"];
    var id = result["args"]["id"]; // always 1
    // Do Something 
});

this fetches all previous events

myEvent.get((error, result) => {
    var data = result["args"]["data"];
    var id = result["args"]["id"]; // always 1
    // Do Something 
});`

when I try to get the event from the txHash it returns the event data but only string paramater is decode correctly. the uint32 type is returned as a Big number. How do is resolve their two issues??

You can easily decode most uint type using Number(uint)

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.