Cannot listen to smart contract event on Infura Ropsten

Hello ! i ve been trying to listen to my smart contract events, first i was using ganache and i was able to see the log of the events. And then i changed the provider to connect to ropsten network with Infura , but the transaction passes and i see no logs of the event .
Here is how i instantiate my web3 instance

async function web3Helper(provider= new HDWalletProvider(
     privateKeys,
     new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/token"),0,4
 )) {
  var web3 = new Web3(provider);
  return { web3}
}
module.exports.web3Helper = web3Helper;

Ans here is how i listen to the events

var namesRegistryinstance = new web3.eth.Contract(abi,global.NamesRegisty._address,{gas: '1500000', from: accounts[1]});
    namesRegistryinstance.events.NameAdded({
        fromBlock: 'latest'
    }, function(error, event){  })
        .on('data', function(event){
            console.log('hello event ',event);
        })
        .on('changed', function(event){
            // remove event from local database
        })
        .on('error', console.error);

I guess i am missing something … but i can’t figure out what i am doing wrong …
Thank you !

Ow i just read that the HttpProvider does not support event subsciption and that i should use websocket provider ! So i changed it to this :

async function web3Helper(provider= new HDWalletProvider(
     privateKeys,
     new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/v3/token"),0,4
 )) {
  var web3 = new Web3(provider);
  return { web3}
}

But now i am getting Error : connection not open on send() :confused:

Hi Sarah,

Happy to help dig in to this with you. Does the console log anything at all when the code is executed?

Michael

Hello Michael,

Thank you for the reply ! It says nothing , it just says connection not open on send().
Also i installed wscat and did : ```
wscat -c wss://ropsten.infura.io/ws/v3/YOUR-PROJECT-ID

But i still have same error ... :/

Can you email me at michael@infura.io the email address you use for your Infura account? I will look into the Project to see what I can find.

1 Like

@sarah can you provide the exact output you’re getting when using wscat?

Are you requiring your Project Secret in your project settings? You will need to use the following wscat format if you are,
wscat -c wss://ropsten.infura.io/ws/v3/YOUR-PROJECT-ID --auth ":YOUR-PROJECT-SECRET"

@mike , i wasn’t getting any output when ran the previous wscat command , but now i changed it and i executed that format , but still it doesn’t show any output …

Can you show me screenshots of your terminal where you’re executing the command, with your ID and Secret obfuscated?

@mike Sorry yesterday i was trying the command line in the cloned version locally and it wasn’t giving any logs but now i just tryed it in my VM and i get this :


It says connected at first then when i press enter it shows that error and exits .
And when i run my project of course i still get same error " connection not open on send() "

Hi @sarah, when you hit enter at the connected stage you are passing an invalid command to the connection and that is causing it to close, if you try passing a valid command such as,
{"jsonrpc":"2.0", "id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
You will create a newHeads subscription and should see block data start to come in from Ropsten.

Can you confirm this works on your side?

1 Like

Hey @mike ! thank you so much ! it works and i dnt have the “connection not open on send()” anymore!
Just one last question if i may , i tried the example of using “getPastEvents” in the documentation, it works , then i wanted to get events notifs automatically as functions are called so i tried this in my apps.js file



(async () => {
    var { deploy, accounts, web3 } = await  helper.testHelper(`./src/contracts/`); //
    global.accounts = accounts
    global.NamesRegisty = await deploy('NamesRegistry', { from: accounts[1]}); 
    console.log('contract address',  global.NamesRegisty._address)
    let abi = global.NamesRegisty 
    var namesRegistryinstance = new web3.eth.Contract(abi,global.NamesRegisty._address,{gas: '1500000', from: accounts[1]});
    namesRegistryinstance.events.NameAdded({
         fromBlock: 0, toBlock:'latest'
     }, function(error, event){  })
         .on('data', function(event){
             console.log('hello new event ! ',event); // same results as the optional callback above
         })
         .on('changed', function(event){
             // remove event from local database
         })
         .on('error', console.error);
 
})().catch(e => {
   console.log(e)
});

But then when i call the function i get no logs in the console, i mean listening with Infura through web3js is done like that right ? i mean i didn’t miss another step maybe ?

Thanks again you’ve been a great help, i spent a while on that “connection” error…

I’m not familiar with the exact way to do it using web3.js, that would be a good question for their gitter, https://gitter.im/ethereum/web3.js?source=orgpage

I would recommend creating a websocket using the eth_subscribe method with the logs subscription type outlined here, https://infura.io/docs/ethereum/wss/eth_subscribe.

@mike i guess i know why that doesn’t work , actually when i first connected to infura i did it using HDWalletProvider like this :

new HDWalletProvider(
     privateKeys,
     new Web3.providers.WebsocketProvider("wss://ropsten.infura.io/ws/v3/token"),0,4
 )

But the HDWalletprovider does not support subscriptions, so when i tried subscribing to ‘logs’ i kept getting

Error: The current provider doesn't support subscriptions: HDWalletProvider

I looked it up and obviously HDWallet does not allow you to make subscriptions to events …
So i guess one way to do it is to remove the HDWalletProvider and to sign transactions manually, but since infura does not manage privateKeys , i am not sure how to link my account created on ropsten to the Infura Node so i can send transactions from it …

I see, that’s good to know, thanks for digging in and finding that out!

Good news, here’s another user that requested info on signing txns and then sending them through us using eth_sendRawTransaction, eth_sendRawTransaction.

1 Like