2

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.

Aditi
  • 327
  • 2
  • 8
  • 1
    Full example to extract events and interpreting the data at http://ethereum.stackexchange.com/questions/8241/event-result-documentation . – BokkyPooBah Aug 24 '16 at 05:24

2 Answers2

0

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.

-1

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)
        console.log(result);
});

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

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

Sebi
  • 5,294
  • 6
  • 27
  • 52