Is it possible to figure out whether the network is mainnet or testnet (Ropsten) from web3.js? Or through MetaMask?
-
Use ES6: await web3.eth.net.getNetworkType((err, network)=> {...}); – Russo Oct 1 '18 at 14:12
Prior to Web 1.0 you can use web3.version.getNetwork as following:
web3.version.getNetwork((err, netId) => {
switch (netId) {
case "1":
console.log('This is mainnet')
break
case "2":
console.log('This is the deprecated Morden test network.')
break
case "3":
console.log('This is the ropsten test network.')
break
default:
console.log('This is an unknown network.')
}
})
WEB 1.0 :
Since web 1.0 we have web3.eth.net.getNetworkType which Guesses the network we are connected to.
web3.eth.net.getNetworkType()
.then(console.log);
It returns a string referring network's name :
- "main" for main network
- "morden" for the morden test network
- "ropsten" for the ropsten test network
- "private" for undetectable networks.
web3.version.network or the async version web3.version.getNetwork() allow one to directly determine the network ID from web3.js.
Usage is as expected:
var network = web3.version.network
Network IDs can be found in this thread.
-
This might lead to exceptions stating that it is no good to access web3 methods synchronously. I had it this way and ran into issues in my rendering of react components, so i changed it to the accepted answer – David Fariña Mar 6 '18 at 17:56
-
1web3.version.getNetwork() is asynchronous, for what it's worth. – Matthew Schmidt Mar 7 '18 at 23:21
-
youre right of course, but the only code fragment you actually formatted as code is the synchronous version which should be avoided, especially since there is a async variant. Didnt want to make your post down or anything, just wanted to give a helpful sidenote on that topic – David Fariña Mar 8 '18 at 11:25
For web3 1.0.0
you should do:
web3.eth.net.getId().then(netId => {
switch (netId) {
case 1:
console.log('This is mainnet')
break
case 2:
console.log('This is the deprecated Morden test network.')
break
case 3:
console.log('This is the ropsten test network.')
break
default:
console.log('This is an unknown network.')
}
})
In addition to @Badr's answer, there are two more test networks, Rinkeby and Kovan:
switch (networkId) {
case "1":
networkName = "Main";
break;
case "2":
networkName = "Morden";
break;
case "3":
networkName = "Ropsten";
break;
case "4":
networkName = "Rinkeby";
break;
case "42":
networkName = "Kovan";
break;
default:
networkName = "Unknown";
}
UPDATE: web3@1.0.0 has a web3.eth.net.getNetworkType([callback])
function to get a network name guess
Is it possible to figure out whether the network is mainnet or testnet (Ropsten) from web3.js?
This has been asked before, and is answered here: Getting Go-Ethereum current network id
In addition to these suggestions, you could take a look a the net_version
RPC call.
Or through MetaMask?
The main MetaMask pop-up shows the network you're connected to in the top left. It also functions as a drop-down, to allow you to select which network you want it to connect to.