Retrieving and listening to contract events

Now let's look at how to watch for events from a contract. Watching for events is very important because the result of method invocations by transactions are usually returned by triggering events.

Before we get into how to retrieve and watch for events, we need to learn indexed parameters of events. A maximum of three parameters of an event can have the indexed attribute. This attribute is used to signal the node to index it so that the app client can search for events with matching return values. If you don't use the indexed attribute, then it will have to retrieve all the events from the node and filter the ones needed. For example, you can write the logFileAddedStatus event this way:
event logFileAddedStatus(bool indexed status, uint indexed timestamp, string owner, string indexed fileHash); 

Here is an example to demonstrate how to listen to contract events:

var event = proof.logFileAddedStatus(null, { 
fromBlock: 0,
toBlock: "latest"
});
event.get(function(error, result){
if(!error)
{
console.log(result);
}
else
{
console.log(error);
}
})
event.watch(function(error, result){
if(!error)
{
console.log(result.args.status);
}
else
{
console.log(error);
}
})
setTimeout(function(){
event.stopWatching();
}, 60000)
var events = proof.allEvents({
fromBlock: 0,
toBlock: "latest"
});
events.get(function(error, result){
if(!error)
{
console.log(result);
}
else
{
console.log(error);
}
})
events.watch(function(error, result){
if(!error)
{
console.log(result.args.status);
}
else
{
console.log(error);
}
})
setTimeout(function(){
events.stopWatching();
}, 60000)

This is how the preceding code works:

  1. At first, we get the event object by calling the method of the event namesake on a contract instance. This method takes two objects as arguments, which are used to filter events:
    • The first object is used to filter events by indexed return values: for example, {'valueA': 1, 'valueB': [myFirstAddress, mySecondAddress]}. By default, all filter values are set to null. This means that they will match any event of a given type sent from this contract.
    • The next object can contain three properties: fromBlock (the earliest block; by default, it is "latest"), toBlock (the latest block; by default, it is "latest"), and address (a list of addresses to only get logs from; by default, the contract address).
  1. The event object exposes three methods: get, watch, and stopWatching. get is used to get all the events in the block range. watch is like get but it watches for changes after getting the events. And stopWatching can be used to stop watching for changes.
  2. Then, we have the allEvents method of the contract instance. It is used to retrieve all the events of a contract.
  3. Every event is represented by an object that contains the following properties:
    • args: An object with the arguments from the event
    • event: A string representing the event name
    • logIndex: An integer representing the log index position in the block
    • transactionIndex: An integer representing the transactions the index position log was created from
    • transactionHash: A string representing the hash of the transactions this log was created from
    • address: A string representing the address from which this log originated
    • blockHash: A string representing the hash of the block where this log was in; null when its pending
    • blockNumber: The block number where this log was in; null when its pending
web3.js provides a web3.eth.filter API to retrieve and watch for events. You can use this API, but the earlier method's way of handling events is much easier. You can learn more about it at https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethfilter.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.145.122