0

This question already has an answer here:

I’m trying to use the .watch() command on an event in my contract. Whenever I run the .js file, I’m presented with the following:

Error: Invalid JSON RPC response: “”

I’m not at my computer and can’t paste my .js file until later today, but what does this error suggest? It is displayed every time the file is executed. Could it be an issue with the contract instance or web3 itself?

Thank you in advance.

marked as duplicate by Ismael, Achala Dissanayake, Richard Horrocks, flygoing, eth May 19 '18 at 18:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • You are not able to connect to your node (I think you are using geth client). – Prashant Prabhakar Singh May 15 '18 at 11:13
  • What tools are you using (e.g., truffle, ganache, etc)? – goodvibration May 15 '18 at 11:19
  • I am deploying the contract to the Ropsten test network via Remix and setting the web3 provider to the Ropsten Infura node – noob_23 May 15 '18 at 11:28
  • Infura doesn't support events using watch, you have to use subscribe from web3 v1.0 and the websocket endpoint. – Ismael May 15 '18 at 19:39
1

As mentioned by Ismael, Infura doesn't support the use of .watch() events. The solution is to use the Infura websockets. Working code can be found here: Infura web3 provider for Events (.get & .watch)

0

I also add issues using watch() on web3 to watch for events.

But I manage to solve them by using the alternative method (docs):

// Or pass a callback to start watching immediately
var event = myContractInstance.MyEvent([{valueA: 23}] [, additionalFilterObject] , function(error, result){
  if (!error)
    console.log(result);
});

For an event of this kind:

event singleNumber(uint256 number);

I get the value of it like this:

var event = myContractInstance.singleNumber({}, function(error, result){
  if (!error)
    var number = result.args.number.toNumber();
});
0

There is github thread about this response: https://github.com/trufflesuite/truffle/issues/852

According to this it's an issue with Infura and open handlers.

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