-
Notifications
You must be signed in to change notification settings - Fork 2
/
metamask.js
138 lines (124 loc) · 3.21 KB
/
metamask.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*jslint node: true */
'use strict';
const { ethers } = require("ethers");
const { getEnvironment } = require("./environment.js");
const chainIds = {
mainnet: {
Ethereum: 1,
BSC: 56,
Polygon: 137
},
testnet: {
Ethereum: 4, // rinkeby
BSC: 97,
Polygon: 80001
},
};
const rpcMeta = {
mainnet: {
Ethereum: undefined,
BSC: {
chainId: '0x38',
chainName: 'BSC Network',
nativeCurrency:
{
name: 'BNB',
symbol: 'BNB',
decimals: 18
},
rpcUrls: ['https://bsc-dataseed.binance.org/'],
blockExplorerUrls: ['https://bscscan.com/'],
},
Polygon: {
chainId: '0x89',
chainName: 'Polygon Network',
nativeCurrency:
{
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
},
rpcUrls: ['https://rpc-mainnet.maticvigil.com'],
blockExplorerUrls: ['https://polygonscan.com/'],
}
},
testnet: {
Ethereum: undefined, // rinkeby
BSC: {
chainId: '0x61',
chainName: 'BSC Test Network',
nativeCurrency:
{
name: 'BNB',
symbol: 'BNB',
decimals: 18
},
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
blockExplorerUrls: ['https://testnet.bscscan.com/'],
},
Polygon: {
chainId: '0x13881',
chainName: 'Polygon TEST Network',
nativeCurrency:
{
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
},
rpcUrls: ['https://rpc-mumbai.maticvigil.com'],
blockExplorerUrls: ['https://mumbai.polygonscan.com/'],
}
},
};
class NoMetamaskError extends Error { }
async function loginEthereum() {
await window.ethereum.request({ method: 'eth_requestAccounts' });
}
async function changeNetwork(network, testnet) {
const environment = getEnvironment(testnet);
const chainId = chainIds[environment][network];
if (chainId === undefined)
throw new Error("network not found");
return await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: `0x${Number(chainId).toString(16)}` }],
}).catch(async (switchError) => {
if (switchError.code === 4902) {
const params = rpcMeta[environment][network];
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [params],
});
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: `0x${Number(chainId).toString(16)}` }],
}).catch((e) => {
throw new Error("wallet_switchEthereumChain error", e);
})
return Promise.resolve()
} else {
throw new Error("wallet_switchEthereumChain error");
}
});
}
async function getProvider(network, testnet) {
if (!window.ethereum)
throw new NoMetamaskError("MetaMask not found");
await loginEthereum();
let provider = new ethers.providers.Web3Provider(window.ethereum);
const requiredChainId = chainIds[getEnvironment(testnet)][network];
const chainId = (await provider.getNetwork()).chainId;
if (!chainId || chainId !== requiredChainId) {
await changeNetwork(network, testnet)
provider = new ethers.providers.Web3Provider(window.ethereum);
}
return provider;
}
async function getSigner(network, testnet) {
const provider = await getProvider(network, testnet);
const signer = provider.getSigner();
return signer;
}
exports.NoMetamaskError = NoMetamaskError;
exports.getProvider = getProvider;
exports.getSigner = getSigner;