How to do it...

We can watch events from DApp using the following steps:

  1. Consider the following contract, which emits events for each state changing operation. There is a dedicated Essential events–EVM Logger recipe in the previous chapter, which describes the process of writing events in solidity:
pragma solidity ^0.4.22;

contract eventEmitter {

event simpleEvent(address _sender);
event indexedEvent(address indexed _sender, uint _id);

function simpleEmit() public {
// do something
emit simpleEvent(msg.sender);
}

function indexedEmit(uint _id) public {
// do something
emit indexedEvent(msg.sender, _id);
}
}
  1. Watch an emitted event from a decentralized application using the following method. The syntax may be slightly different for various versions of web3.js:
// For v0.2x.x
contractInstance.MyEvent([options]).watch([callback]);

// For v1.x.x
contractInstance.events.MyEvent([options][, callback])
  1. If you are using the existing version of web3.js (0.2x.x), use the watch method to execute a callback for each event. The following snippet listens to the simpleEvent emitted from the sample contract:
// For web3.js v0.2x.x
var
eventContract = web3.eth.contract(abi); var eventContractInstance = eventContract.at(address);
// Create an event instance
var simpleEventInstance = myContractInstance.simpleEvent({}, {
fromBlock: 0,
toBlock: 'latest'
});

// Start watching the event
simpleEventInstance.watch(function(error, result){ console.log(result); });

The preceding listener executes callbacks passed to it for events emitted starting from the 0th block. It keeps listening for each new block mined until we ask it to stop listening. You can use the stopWatching method to do that:

// Stops watching the event
simpleEventInstance.stopWatching();
  1. If you are using a newer version of web3.js (1.x.x), use the implementation given here to watch an event:
// For web3.js v1.x.x
eventContractInstance.
events.simpleEvent({ filter: {}, fromBlock: 0 }, function(error, event) {
console.log(event);
})

You can listen to the following events that are emitted from the event listener:

  • data: Fired on each event emitted with the event object as an argument
  • changed: Fired on each event that was removed from the blockchain
  • error: Fired when an exception occurs:
// For web3.js v1.x.x
eventContractInstance.events.simpleEvent({ filter: {}, fromBlock: 0 }) .on('data', function(event){ console.log(event); }) .on('changed', function(event){ console.log(event); }) .on('error', console.error);
  1. Filter the events further with the help of indexed arguments:
// For web3.js 0.2x.x
var indexedEventInstance = eventContractInstance.indexedEvent({
_sender: '0xce5C2D181f6DD99091351f6E6056c333A969AEC9'
}, {
fromBlock: 0,
toBlock: 'latest'
});
// Logs event emitted from a specific address simpleEventInstance.watch(function(error, result){ console.log(result); });

// For web3.js 1.x.x
eventContractInstance.events.indexedEvent({ filter: {
_sender: [
'0xce5C2D181f6DD99091351f6E6056c333A969AEC9'
,
'0xD0D18F4A02beb7E528cE010742Db1Cc992070135'

] // Use an array for OR condition
}, fromBlock: 0 }) .on('data', function(event){ console.log(event); }) .on('error', console.error);
Using web3.js v1.x.x, you can subscribe to an event and unsubscribe from it immediately after the event or error. This can be done using eventContractInstance.once(event[, options], callback). This will only fire for a single event.
  1. Try querying all the past logs from the blockchain using web3.js:
// For web3.js 0.2x.x
var
simpleEventInstance = eventContractInstance
.simpleEvent({}, {
fromBlock: 0,
toBlock: 'latest'
});

// All past logs
var eventResults = simpleEventInstance
.get(function(error, logs){
console.log(logs);
});
  1. For the newer version of web3.js, retrieve past logs with the help of the getPastEvents method:
// For web3.js 1.x.x - Syntax
contractInstance
.getPastEvents(event[, options][, callback])

// Example
eventContractInstance.getPastEvents('simpleEvent', { filter: { }, fromBlock: 0, toBlock: 'latest' }, function(error, events) {
console.log(events);
}) .then(function(events){ console.log(events) // same result as the callback });
  1. Listen to all the events that are raised from a contract with the allEvents method:
// For web3.js 0.2x.x
var
events = eventContractInstance.allEvents({
fromBlock: 0,
toBlock: 'latest'
}); events.watch(function(error, result){ ... }); events.get(function(error, logs){ ... });

// For web3.js 1.x.x
eventContractInstance.getPastEvents({ filter: {}, fromBlock: 0 }) .on('data', function(event){ console.log(event); }) .on('error', console.error);
..................Content has been hidden....................

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