9

I'm using "web3": "^1.2.4"

This is how I connect to 'wss://kovan.infura.io/ws'

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://kovan.infura.io/ws'))
instance = new web3.eth.Contract(ABI, ADDRESS);

Here I am trying to call PastEvents and for listening

const eventOptions = { filter: {}, fromBlock: lastBlock, toBlock: 'latest' };
const events = await instance.getPastEvents('MyEvent', eventOptions);

instance.events.MyEvent(async function (error, result) {
    console.log(result)
});

How to overcome this error?

2

3 Answers 3

12

I had the same issue.

Turned out the Infura URL I had in my configuration file was invalid. Getting a new endpoint URL from Infura settings solved the issue.

1

I'm facing the same issue with Alchemy on Polygon Mumbai connection (testnet), in a NodeJs app running with PM2 on a virtual machine, starting web3 connection along with the server. It seems that after some time of no use the project starts sending that error.

In Docker projects I haven't face this issue, maybe because the connection is resetting on each request, as container starts.

You could try to make the web3 connection on each request with the strategy that suits you better (Docker microservice or connecting to web3 again on each request).

0

Exactly right. I had the same issue with a new project I had created on infura. Tried out all possible options but no dice. Then, when I created a new project and entered that url on to the dotenv code. voila... It all worked like a charm!!!

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.