sonRpcProvider failed to detect network and cannot start up

const { ethers } = require(“ethers”);

const provider = new ethers.JsonRpcProvider(‘https://mainnet.infura.io/v3/MY API KEY’);

const main = async()=>{
const balance = await provider.getBalance(‘0x246Ab133337Bedb465906D244EeF74B48D3526d9’);
console.log(balance);
}

main();

When I run the above code, it always said
External Image

But it can work with other net on infura, only the Etherum mainnet has problem.

2 Likes

Wellcome @CharlesYu,

the error “ENETUNREACH” suggests that you have a problem with your network, or a misspelling in your code, or something. I had to make small changes to even allow your example to run - did you cut/paste from a real test program? I tried to replicate with this:

const { ethers } = require("ethers");

// const provider = new ethers.providers.JsonRpcProvider(`https://mainnet.infura.io/v3/` + process.env.ETH);
const provider = new ethers.providers.JsonRpcProvider(`https://goerli.infura.io/v3/` + process.env.ETH);

const main = async()=>{
const balance = await provider.getBalance('0x246Ab133337Bedb465906D244EeF74B48D3526d9');
console.log(balance);
}

require("dotenv").config();
main();

But failed to see the error. Are you still having trouble with this?

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.