1.DAPP实现Metamask自动添加网络/切换网络:
实现方法调用matamask的wallet_switchEthereumChain
方法
async addNetwork() {
// console.log("当前链接的节点为:", getInjectedProviderName())
if (getInjectedProviderName() !== "MetaMask") {
throw "This Network is Error";
}
let chain_info = ChainInfo[ChainId];
try {
await this.web3.currentProvider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: this.web3.utils.numberToHex(chain_info.chainId), }],
});
} catch (switchError: any) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
await this.web3.currentProvider.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: this.web3.utils.numberToHex(chain_info.chainId),
chainName: chain_info.chainParams.chainName,
rpcUrls: chain_info.chainParams.rpcUrls,
blockExplorerUrls: chain_info.chainParams.blockExplorerUrls,
nativeCurrency: chain_info.chainParams.nativeCurrency
},
],
});
}
}
}
2.常用的Metamask网络列表
下面为常用的网络列表和区块浏览器地址:
const CHAIN_DATA_LIST = {
1: {
chainId: 1,
chain: "ETH",
network: "mainnet",
networkId: 1,
chainParams: {
chainName: "Ethereum Chain Mainnet", // 添加到钱包后显示的网络名称
rpcUrls: [
'https://mainnet.infura.io/v3/', // rpc地址
],
// iconUrls: [
// 'https://testnet.hecoinfo.com/favicon.png' // 网络的图标
// ],
blockExplorerUrls: [
'https://etherscan.io' // 网络对应的区块浏览器
],
nativeCurrency: { // 网络主币的信息
name: 'ETH',
symbol: 'ETH',
decimals: 18
}
}
},
2: {
chainId: 2,
chain: "EXP",
network: "expanse",
networkId: 1
},
3: {
chainId: 3,
chain: "ETH",
network: "ropsten",
networkId: 3,
chainParams: {
chainName: "Ethereum Chain Ropsten",
rpcUrls: [
'https://ropsten.infura.io/',
],
blockExplorerUrls: [
'https://ropsten.etherscan.io'
],
nativeCurrency: {
name: 'RopstenETH',
symbol: 'RopstenETH',
decimals: 18
}
}
},
4: {
chainId: 4,
chain: "ETH",
network: "rinkeby",
networkId: 4,
chainParams: {
chainName: "Ethereum Chain Rinkeby",
rpcUrls: [
'https://rinkeby.infura.io/v3/',
],
blockExplorerUrls: [
'https://rinkeby.etherscan.io'
],
nativeCurrency: {
name: 'RinkebyETH',
symbol: 'RinkebyETH',
decimals: 18
}
}
},
5: {
chainId: 5,
chain: "ETH",
network: "goerli",
networkId: 5,
chainParams: {
chainName: "Ethereum Chain Goerli",
rpcUrls: [
'https://goerli.infura.io/v3/',
],
blockExplorerUrls: [
'https://goerli.etherscan.io'
],
nativeCurrency: {
name: 'GoerliETH',
symbol: 'GoerliETH',
decimals: 18
}
}
},
6: {
chainId: 6,
chain: "ETC",
network: "kotti",
networkId: 6
},
8: {
chainId: 8,
chain: "UBQ",
network: "ubiq",
networkId: 88
},
9: {
chainId: 9,
chain: "UBQ",
network: "ubiq-testnet",
networkId: 2
},
11: {
chainId: 11,
chain: "META",
network: "metadium",
networkId: 11
},
12: {
chainId: 12,
chain: "META",
network: "metadium-testnet",
networkId: 12
},
18: {
chainId: 18,
chain: "TST",
network: "thundercore-testnet",
networkId: 18
},
30: {
chainId: 30,
chain: "RSK",
network: "rsk",
networkId: 30
},
31: {
chainId: 31,
chain: "RSK",
network: "rsk-testnet",
networkId: 31
},
42: {
chainId: 42,
chain: "ETH",
network: "kovan",
networkId: 42
},
56: {
chainId: 56,
chain: "BSC",
network: "binance",
networkId: 56,
chainParams: {
chainName: "Binance Smart Chain Mainnet", // 添加到钱包后显示的网络名称
rpcUrls: [
'https://bsc-dataseed.binance.org/', // rpc地址
],
blockExplorerUrls: [
'https://bscscan.com' // 网络对应的区块浏览器
],
nativeCurrency: { // 网络主币的信息
name: 'BNB',
symbol: 'BNB',
decimals: 18
}
}
},
60: {
chainId: 60,
chain: "GO",
network: "gochain",
networkId: 60
},
61: {
chainId: 61,
chain: "ETC",
network: "etc",
networkId: 1
},
62: {
chainId: 62,
chain: "ETC",
network: "etc-morden",
networkId: 2
},
63: {
chainId: 63,
chain: "ETC",
network: "etc-testnet",
networkId: 7
},
64: {
chainId: 64,
chain: "ELLA",
network: "ellaism",
networkId: 64
},
76: {
chainId: 76,
chain: "MIX",
network: "mix",
networkId: 76
},
77: {
chainId: 77,
chain: "POA",
network: "poa-sokol",
networkId: 77
},
88: {
chainId: 88,
chain: "TOMO",
network: "tomochain",
networkId: 88
},
97: {
chainId: 97,
chain: "BSCT",
network: "binance-test",
networkId: 97
},
99: {
chainId: 99,
chain: "POA",
network: "poa-core",
networkId: 99
},
100: {
chainId: 100,
chain: "XDAI",
network: "xdai",
networkId: 100
},
101: {
chainId: 101,
chain: "ETI",
network: "etherinc",
networkId: 1
},
108: {
chainId: 108,
chain: "TT",
network: "thundercore",
networkId: 108
},
162: {
chainId: 162,
chain: "PHT",
network: "sirius",
networkId: 162
},
163: {
chainId: 163,
chain: "PHT",
network: "lightstreams",
networkId: 163
},
211: {
chainId: 211,
chain: "FTN",
network: "freight",
networkId: 0
},
269: {
chainId: 269,
chain: "HPB",
network: "hpb",
networkId: 100
},
385: {
chainId: 385,
chain: "CRO",
network: "lisinski",
networkId: 385
},
820: {
chainId: 820,
chain: "CLO",
network: "callisto",
networkId: 1
},
821: {
chainId: 821,
chain: "CLO",
network: "callisto-testnet",
networkId: 2
},
137: {
chainId: 137,
chain: "MATIC",
network: "matic",
networkId: 137
},
80001: {
chainId: 80001,
chain: "MUMBAI",
network: "mumbai",
networkId: 80001
},
246529: {
chainId: 246529,
chain: "ARTIS sigma1",
network: "artis-s1",
networkId: 246529
},
246785: {
chainId: 246785,
chain: "ARTIS tau1",
network: "artis-t1",
networkId: 246785
}
};