11

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?

4

2 Answers 2

22

event.watch is not a function in web3.0 v1.0

Use the snippet below instead, but remember that:

  • myContract should be a variable you assigned which is an instance of your contract
  • MyEvent should be replaced with the actual name of your event.
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.

12
  • Should this emit all past events? Seems to only emit the last even for me
    – Tarrence
    May 30, 2018 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, 2018 at 1:51
  • @Tarrence In fact please use getPastEvents
    – mesqueeb
    Jul 11, 2018 at 6:46
  • 3
    I'm getting TypeError: Cannot read property 'MyEvent' of undefined..anyone else?
    – thefett
    Feb 1, 2019 at 20:40
  • Having same issue. events are undefined.
    – Rob Magier
    Feb 4, 2019 at 21:49
0

In my case, I could only listen to past events, but new events didn't come through. If you're in the same situation, add an indexed parameter to the event in the contract, and filter by it on the front-end. The event will start showing up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.