0

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());
    contract.funct();
}, true);

task.start();

contractRouter.subscribeToEvent().then(function(result) {
    deferred.resolve(result);
}).fail(function(err) {
    deferred.reject(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
                deferred.resolve(result.returnValues);
            } else {
                deferred.reject(error);
            }
        });
    }).fail(function(err){
        deferred.reject(err);
    });

    return deferred.promise;
}

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

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

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?

0

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, privacy policy and cookie policy

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