I wanna to check current rpc network is using Flashbot or Infura.
I know can using window.ethereum.networkVersion
to get ChainID
to check it is Mainnet or Goerli.
But I do not know how to check current rpc network is using Flashbot or Infura.
Not sure nor can check now but you can have the url of the rpc in some window.ethereum
properties.
Try this one:
provider.getNetwork()
It will return the following (as example):
{
"name": "bnb",
"chainId": 56,
"ensAddress": null,
"_defaultProvider": null
}
web3.currentProvider.getNetwork()
but not work, it return me: web3.currentProvider.getNetwork is not a function
Commented
Feb 16, 2023 at 0:16
For those looking for an up-to-date answer (April 2024):
You should be using chain ids rather than network ids. More on that on this post by Pedro Gomes (WalletConnect's founder).
To get the chain id with web3.js 4.x
, you should use web3.eth.getChainId()
instead, and map that to the network name yourself:
const SUPPORTED_CHAINS_NAMES: Record<number, string> = {
1: 'Ethereum Mainnet',
11155111: 'Sepolia',
};
const chainId = Number(await web3.eth.getChainId());
const chainName = SUPPORTED_CHAINS_NAMES[chainId] || 'Unsupported Chain';
Or use a library like eth-chains
to get up-to-date chain information, instead of manually mapping the chain id:
const chainId = Number(await web3.eth.getChainId());
const chainInfo = chains.getById(chainId) || null;
const chainName = chainInfo?.name || 'Unsupported Chain';
Also, see web3.js' migration guide.