I'm using Web3.js and am trying to monitor the Ethereum mainnet. Up until now I have been testing on a local environment, but I want to test it live now. Here's the relevant code:
const Web3 = require('web3')
let web3
if (!config.PROJECT_SETTINGS.isLocal) {
web3 = new Web3(`wss://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`)
// web3 = new Web3(`wss://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`)
} else {
web3 = new Web3('ws://127.0.0.1:7545')
}
// Other stuff happens
module.exports = {
web3,
// It exports other stuff too, but this is what matters for the question.
}
As you can see, when a setting isLocal
is set to false, it should begin pulling data from an API key. However, I continue to receive the error in the subject line. Here's the full error log:
/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:66
const error = new Error(msg);
^
Error: connection not open on send()
at Object.ConnectionError (/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:66:23)
at Object.ConnectionNotOpenError (/Users/Me/bot/node_modules/web3-core-helpers/lib/errors.js:49:21)
at /Users/Me/bot/node_modules/web3-providers-ws/lib/index.js:155:37
at Map.forEach (<anonymous>)
at WebsocketProvider._onClose (/Users/Me/bot/node_modules/web3-providers-ws/lib/index.js:154:27)
at W3CWebSocket._dispatchEvent [as dispatchEvent] (/Users/Me/bot/node_modules/yaeti/lib/EventTarget.js:115:12)
at W3CWebSocket.onConnectFailed (/Users/Me/bot/node_modules/websocket/lib/W3CWebSocket.js:219:14)
at WebSocketClient.<anonymous> (/Users/Me/bot/node_modules/websocket/lib/W3CWebSocket.js:59:25)
at WebSocketClient.emit (node:events:513:28)
at ClientRequest.handleRequestError (/Users/Me/bot/node_modules/websocket/lib/WebSocketClient.js:227:14)
at ClientRequest.emit (node:events:513:28)
at Socket.socketErrorListener (node:_http_client:494:9)
at Socket.emit (node:events:513:28)
at emitErrorNT (node:internal/streams/destroy:151:8)
at emitErrorCloseNT (node:internal/streams/destroy:116:3)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
code: 1006,
reason: 'connection failed'
}
I have seen from other similar posts that simply refreshing the API key is enough to make this work. However, I have tried multiple API keys from Infura and Alchemy. Each time, I have received the same error message.
Any other ideas about why this might be failing?