TransWikia.com

How to get event from geth ethereum node using web3.js

Ethereum Asked by Moh on August 26, 2021

I have created a simple contract where I am trying to write a value to a state variable in solidity smart contract, and emit that written value.

I am using Truffle v5.1.28 (core: 5.1.28)
Solidity – 0.6.6 (solc-js)
Node v8.10.0
Web3.js v1.2.1
Geth
Version: 1.9.15-stable

I have created my own geth node, and started it using the below command.

sudo geth --datadir="ethdata" --verbosity 10 --ipcdisable --port 30303 --networkid 15 --nodiscover console --allow-insecure-unlock  --unlock 0x81f2d8DcaEffE200826710DD4f642387b3b0D182  --password password --rpc --rpcport "8000" --rpcaddr "0.0.0.0" –rpccorsdomain "*" --rpcapi "eth,net,web3,miner,debug,personal,rpc" --wsorigins "*"

The node starts successfully, and I start mining it on using miner.start(1).

After, that I am also able to deploy my contract successfully, on the node.

Now, when I am trying to use web3.js from a file that I have created to interact with the contract, it is throwing me an error. I am not able to get instance of my contract if I am using

     App.web3Provider = new Web3.providers.WebsocketProvider(
    "ws://localhost:8000"
  );

instead of

   App.web3Provider = new Web3.providers.HttpProvider(
    "http://localhost:8000"
  );

If I use HttpProvider, I get the instance, and I am also able to write to my contract using it, but, as I need to listen for events for my blockchain, I cannot use HttpProvider .

But, if I use WebsocketProvider, I get below error, while trying to get the instance of my contract: The rest all of the code is same for both the cases, I only change the providers in these cases.

Error: connection not open on send()
at Object.ConnectionError (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/web3-core-helpers/src/errors.js:69:23)
at Object.ConnectionNotOpenError (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/web3-core-helpers/src/errors.js:48:21)
at /home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/web3-providers-ws/src/index.js:181:37
at Map.forEach (<anonymous>)
at WebsocketProvider._onClose (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/web3-providers-ws/src/index.js:180:27)
at W3CWebSocket._dispatchEvent [as dispatchEvent] (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/yaeti/lib/EventTarget.js:115:12)
at W3CWebSocket.onConnectFailed (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/websocket/lib/W3CWebSocket.js:219:14)
at WebSocketClient.<anonymous> (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/websocket/lib/W3CWebSocket.js:59:25)
at emitOne (events.js:116:13)
at WebSocketClient.emit (events.js:211:7)
at WebSocketClient.failHandshake (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/websocket/lib/WebSocketClient.js:339:10)
at ClientRequest.<anonymous> (/home/silmonk/code/BlockchainNetwork/truffle-init-default/src/node_modules/websocket/lib/WebSocketClient.js:278:18)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:551:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:115:23)

I would really appreciate it, if I can get any help regarding this.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP