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()
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
@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 âŚ
@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.
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âŚ
@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.