I'm writing a program in JS/Node that, amongst other things, subscribes to a smart contract event waiting for it to happen.

There's a lot going on on the code, so I've got it separated.

The main function calls (code reduced):

var task = cron.schedule("0 * * * * *", function() {
    console.log(new Date());
}, true);


contractRouter.subscribeToEvent().then(function(result) {
}).fail(function(err) {

app.listen(config.get('port'), function() {
    loggerButler.info('App listening on port...', config.get('port'));

The contractRouter.js has:

function subscribeToEvent() {
    var deferred = q.defer();

    handler.getContractToSubscribe().then(function(abiContract) {
        abiContract.allEvents({ fromBlock:'latest' }, function(error, result) {
            if(!error) {
                // other functions here with the values returned
            } else {

    return deferred.promise;

and the handler.js just returns the contract like (code reduced):

var abiContract = new web3.eth.Contract(abiAddress, contractAddress);

For some reason, I'm not getting the events when they're fired. I know for a fact that they're being fired, but they're not getting through to the program.

Can you help me find out what's wrong? Is there something wrong with having the code split between different classes?

1 Answer 1


It seems you are using web3 v1.0 in that case you have to use myContract.events.allEvents

abiContract.events.allEvents({ fromBlock:'latest' }, function(error, result) {
    // ...

Also you use promises inside the callback but it doesn't seem a good idea since the callback will fire multiple times once for each event.


Your Answer

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

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