how to watch events in web3.js when the event is fired from inside the constructor functionof a contract? Eg:

contract A{
   event Invoked(string);
   function A(){
   Invoked("constructor invoked!");

what to write in web3.js layer to get the output of event in the console.


2 Answers 2


According to the documentation here https://github.com/ethereum/wiki/wiki/JavaScript-API#parameters-29 fromBlock is "latest" by default. So in the case of an event sent in the constructor, "latest" may miss your event.

Try setting fromBlock to an earlier numerical value. At worst, try 0.


Have a look at the snippet in the documentation:

var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at(0x123 /* address */);

var event = clientReceipt.Deposit();

// watch for changes
event.watch(function(error, result){
    // result will contain various information
    // including the argumets given to the Deposit
    // call.
    if (!error)

// Or pass a callback to start watching immediately
var event = clientReceipt.Deposit(function(error, result) {
    if (!error)

Use one of the listeners and change the event name and functionality.

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.