-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddnetwork.js
63 lines (59 loc) · 2.51 KB
/
addnetwork.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var eth;
var isTestnet = 'false';
async function addNetwork(type) {
if (type === 'web3') {
if (typeof ethereum !== 'undefined') {
eth = new Web3Eth(ethereum);
} else if (typeof web3 !== 'undefined') {
eth = new Web3Eth(web3.givenProvider);
} else {
eth = new Web3Eth(ethereum);
}
}
if (typeof eth !== 'undefined') {
var network = 0;
network = await eth.net.getId();
netID = network.toString();
var params;
if (isTestnet == "false") {
if (netID == "137") {
alert("Polygon Network has already been added to Metamask.");
return;
} else {
params = [{
chainId: '0x89',
chainName: 'Matic Mainnet',
nativeCurrency: {
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
},
rpcUrls: ['https://polygon-rpc.com/'],
blockExplorerUrls: ['https://polygonscan.com/']
}]
}
} else {
if (netID == "80001") {
alert("Polygon Mumbai Network has already been added to Metamask.");
return;
} else {
params = [{
chainId: '0x13881',
chainName: 'Polygon Testnet',
nativeCurrency: {
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
},
rpcUrls: ['https://rpc-mumbai.maticvigil.com/'],
blockExplorerUrls: ['https://mumbai.polygonscan.com/']
}]
}
}
window.ethereum.request({ method: 'wallet_addEthereumChain', params })
.then(() => console.log('Success'))
.catch((error) => console.log("Error", error.message));
} else {
alert('Unable to locate a compatible web3 browser!');
}
}