4

I have a problem when listening for events from my contract.

Code js :

var Web3 = require('web3');
var abi = '...';
var contractAddress = '0x62e7Dd1Af52d5A08D401b1e156cC4CB1d2f89d57';
var eventName = 'Transfer';
//var web3;
var etat;
const web3 = new Web3(new Web3.providers.WebsocketProvider('http://127.0.0.1:8545'));
/*
var MyContract = new web3.eth.Contract(JSON.parse(abi));
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

// watch for an event with {some: 'args'}
var myEvent = myContractInstance.Transfer({}, {fromBlock: 0, toBlock: 'latest'});
myEvent.watch(function(error, result){
    console.log(result)
});

// would get all past logs again.
var myResults = myEvent.get(function(error, logs){ console.log(logs) });



// would stop and uninstall the filter
myEvent.stopWatching();
*/



var TokenContract = new web3.eth.Contract(JSON.parse(abi),contractAddress);
TokenContract.events.allEvents({ fromBlock: 'latest' }, console.log)
console.log('1********************************************************************************')
console.log(TokenContract);
console.log('2********************************************************************************')
var event = TokenContract.events.Transfer();
console.log(event);
console.log('3********************************************************************************')

TokenContract.once('Transfer', {
}, function(error, event){ console.log(event); });

event.watch(function(error, result){
    if (!error) {
        alert("wait for a while, check for block Synchronization or block creation");
        console.log(result);
        console.log('pas d erreur');
    }else {
        console.log(error);
        console.log('erreur')
    }
});

I have tested a lot of possibilities but the function .watch doesn't run :

TypeError: event.watch is not a function

How do you listen to events using Web3 1.0?

6

event.watch is not a function in web3.0 v1.0 Use this instead:

myContract.events.MyEvent({
    filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
    fromBlock: 0
}, 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);

Also see the docs here.

  • Should this emit all past events? Seems to only emit the last even for me – Tarrence May 30 '18 at 19:37
  • In this particular case the params state fromBlock: 0 so it should emit everything past block 0... However, based on your node provider it might not work. Try downloading a full node on your computer and executing it on that full node. – mesqueeb Jun 7 '18 at 1:51
  • @Tarrence In fact please use getPastEvents – mesqueeb Jul 11 '18 at 6:46
  • 2
    I'm getting TypeError: Cannot read property 'MyEvent' of undefined..anyone else? – thefett Feb 1 at 20:40
  • Having same issue. events are undefined. – Rob Magier Feb 4 at 21:49

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.