3

I have deployed contracts on ropsten test network. truffle.js

ropsten: {
  provider: function() {  
    return new HDWalletProvider(mnemonic, "https://ropsten.infura.io/<api-token>")
  },
  network_id: '*',
  gas:"4000000"
}

store.sol

pragma solidity ^0.4.17;

contract SimpleStorage {
  uint myVariable;
  event emitval();

  function setValue(uint x) public {
    emitval();
    myVariable = x;
  }

  function getValue() public returns (uint) {
    emitval();
    return myVariable;
  }
}

In my node js code I am using web3 js to call/listen methods/events from contracts.

my node js file is

web3.setProvider(new Web3.providers.WebsocketProvider('https://ropsten.infura.io/<api-token>'));

and for listening events I wrote

var contracts = new web3.eth.Contract(importedjson.abi, deployedaddressat)

contracts.events.emitval(function(error, result){
    if(error) { console.log(error);} 
    else { console.log('Event setVal:', result);}
})

When I am calling methods of solidity (setValue() or getValue() which emits emitval() event), I am not able to listen it on node js code.

Could you please help me out.
I am uisng latest web3js version "1.0.0-beta.33".

2 Answers 2

0

I can't see such setVal events in your contract. Indeed, in the contract, the name is emitval. You can try to change your code

contracts.events.emitval(function(error, result){
    if(error) { console.log(error);} 
    else { console.log('Event setVal:', result);}
});

and alternative is to listen to all the contract events using myContract.events.allEvents. ref

2
  • Sorry my bad while posting query I have renamed event in solidity file but forget to rename it in node js code.. I have checked event name is same in solidity and node js code now but still unable to listen event.
    – Kapil
    Commented Apr 12, 2018 at 9:06
  • I am also facing the same issue. Unable to listen for the custom event. Did you get any solution?
    – Trinu
    Commented Oct 16, 2018 at 23:02
0

Can you try replacing web3.setProvider(new Web3.providers.WebsocketProvider('https://ropsten.infura.io/'));

With

web3.setProvider(new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/'));

It might be a websocket issue as web3 1.0 doesn't support events for http.

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.