DAPP实现Metamask自动添加网络和切换网络

本文介绍了如何在DAPP中通过Metamask自动添加和切换不同的以太坊网络,如主网、Ropsten测试网等,并提供了一个详细的常用Metamask网络列表,包括网络ID、RPC地址、区块浏览器URL和网络主币信息。
摘要由CSDN通过智能技术生成

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
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值