On Web3.js/1.0
there is no watch()
function for the contracts to retrieve events.
I have followed the following guide, web3.eth.Contract - Events - getPastEvents - example, and answer to retrieve emitted events.
event.watch(...)
used to wait until a newevent
shows up and does not move on to the next instruction till watch is stopped.
In the following example if there is no event in between fromBlock and toBlock: 'latest'
it returns []
and stops waiting for new events to show up right away, which was not case on when we are using watch()
.
Example-1 getPastEvents()
:
myContractInstance.getPastEvents('LogJob', {
filter: {clusterAddress: [web3.eth.defaultAccount]},
fromBlock: 1899162,
toBlock: 'latest'
}, function(error, event){ console.log(event); });
Example-2 events.LogJob
from web3.py guide: returns undefined
myContractInstance.events.LogJob({
filter: {clusterAddress: [web3.eth.defaultAccount]},
fromBlock: 1899162
}, function(error, event){ console.log(event); })
.on('data', function(event){
console.log(event); // same results as the optional callback above
})
.on('changed', function(event){
// remove event from local database
})
.on('error', console.error);
[Q] Is there any way to apply same approach like watch()
; where if there is no event in between given fromBlock and toBlock: 'latest'
, then continue to watch and do not jump on to the next instruction.
Basically if an event
returns []
; I want the process keep watching new upcoming blocks and complete watching until the new event is emitted. Only solution comes to my mind is to make this call every 2 seconds until filter does not return []
.
Web3.py
please see (ethereum.stackexchange.com/questions/54700/…) @YuanwuHe