Error "could not detect network" when using WebSocket provider with Alchemy

134 views Asked by At

I have a file with over 1 million+ ethereum addresses and private keys grouped together in a single file separated only using a ','.

Example:

0x7256bCf73C2309893AA12d6b26A142AB3097560c,0x5281130a3a84f531a1e30649a881207a65ce17fae084e0ae6e78xxxxxxxxxxxx

I want to use nodejs to check the balances of each address in the line to be greater than 0.000 and then export the valid addresses along with private keys to another file.

I always experience this error whenever I run my script.


        var error = new Error(message);
                    ^

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
    at Logger.makeError (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\logger\lib\index.js:233:21)
    at Logger.throwError (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\logger\lib\index.js:242:20)
    at WebSocketProvider.<anonymous> (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:561:54)
    at step (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (C:\Users\Asus\Downloads\eth-fast-mnemonic-checker-main\eth-fast-mnemonic-checker-main\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: 'could not detect network',
  code: 'NETWORK_ERROR',
  event: 'noNetwork'
}

Here's the initial script , would be grateful if you could fix the error for me, thanks.

const fs = require('fs')
const ethers = require('ethers')
require('colors')

const provider = new ethers.providers.WebSocketProvider(
    'wss://eth-mainnet.g.alchemy.com/v2/k5d8RoDGOyxZmVWy2UPNxxxxxxxxxxxx'
)

const addresses = fs
    .readFileSync('hits.txt', 'utf8')
    .split('\n')
    .map((val) => {
        return val.split(',')
    })

;(async () => {
    for (let i = 0; i < addresses.length; i++) {
        const address = addresses[i][0]
        const balance = await provider.getBalance(address)

        if (balance.gt(0)) {
            console.log(address.bgGreen.black, balance.toString().bgGreen.black)
            console.log('Private Key: '.yellow, addresses[i][1])
        } else {
            console.log(address, 0)
        }
    }
})()
1

There are 1 answers

5
CherryDT On

You are using the wrong endpoint domain.

According to the docs, the domain of the endpoint for WebSocket connections is eth-mainnet.ws.g.alchemy.com (note the extra ws in there). However, that domain doesn't seem to exist! Perhaps the docs are outdated... I found another source which instead shows the correct domain as eth-mainnet.ws.alchemyapi.io which does exist.

Maybe also check within your Alchemy account, the correct URL should be shown there as well.