主页 > imtoken安卓官网 > 将 Metamask 网络切换到链 1(以太坊主网)
将 Metamask 网络切换到链 1(以太坊主网)
imtoken安卓官网 2023-01-26 05:12:21
麦芽酒*_*rry 5
上周 Metamask 推出了一项名为“wallet_addEthereumChain”的新功能以太坊主网网址,该功能允许用户在收到提示时自动将新的以太坊 RPC 添加到他们的钱包中。 此功能还允许用户更改他们连接的网络以太坊主网网址,例如,如果我将币安智能链连接到 Metamask,则调用 wallet_addEthereumChain 会将活动网络更改为 BSC。 但是,在以太坊链上尝试此操作时,出现无法添加主网 RPC 的错误。 我已经使用以下代码从以太坊主网更改为币安智能链,它工作正常: switchToBinance: async function () {
let ethereum = window.ethereum;
const data = [{
chainId: '0x38',
chainName: 'Binance Smart Chain',
nativeCurrency:
{
name: 'BNB',
symbol: 'BNB',
decimals: 18
},
rpcUrls: ['https://bsc-dataseed.binance.org/'],
blockExplorerUrls: ['https://bscscan.com/'],
}]
/* eslint-disable */
const tx = await ethereum.request({method: 'wallet_addEthereumChain', params:data}).catch()
if (tx) {
console.log(tx)
}
},
但是当我尝试 Exact 时,metamask 抛出一个异常,说我无法添加主网 RPC:switchToEthereum:async function () {
let ethereum = window.ethereum;
const data = [{
chainId: '0x1',
chainName: 'Ethereum',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18,
},
rpcUrls: ['https://mainnet.infura.io/v3/undefined'],
blockExplorerUrls: ['https://etherscan.io'],
}]
/* eslint-disable */
const tx = await ethereum.request({method: 'wallet_addEthereumChain', params:data}).catch()
if (tx) {
console.log(tx)
}
},
但是,添加新 RPC 连接和更改活动 RPC 连接的请求是相同的。 那么有什么方法可以将活跃的以太坊提供者从自定义链更改为主网(链 id-1)