4

Let's say I have an event notify() in a function called buy().

event notify();
   buy(){
   //...
   notify();
}

Problem here is that my the notify watch handle is triggered only after that transaction is mined. Is this how events work? If so, is there alternative to know the transactions happening in my contract instantly? var filter = contractObject.notify();

filter.watch(function (error, log, data)
 {  console.log(data);  });

1 Answer 1

1

Use pending when constructing your web3.eth.filter.

web3.eth.filter has one parameter:

String|Object - The string "latest" or "pending" to watch for changes in the latest block or pending transactions respectively. Or a filter options object as follows: fromBlock: Number|String - The number of the earliest block (latest may be given to mean the most recent and pending currently mining, block). By default latest. toBlock: Number|String - The number of the latest block (latest may be given to mean the most recent and pending currently mining, block). By default latest.

Some things to be aware of with pending transactions:

  • they might never get mined, for example one that has a gas price that's too low
  • they're local: the network does not have a global shared view of them, a client may know about pending txA and not about pending txB, and another client may only know vice-versa.

Those are some reasons why the default is to listen only to events that have been mined into a block (latest). Even then, be aware that you may want to handle a chain reorganization.

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.