ethereum - 将 Metamask 网络切换到链 1(以太坊主网)
问题描述
上周 Metamask 引入了一个名为“wallet_addEthereumChain”的新功能,允许用户在收到提示时自动将新的 Etheruem RPC 添加到他们的钱包中。此功能还允许用户更改他们连接的网络,例如,如果我已经将 Binance Smart Chain 连接到 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)
解决方案
推荐阅读
- jsoniq - JSONiq 文档代码片段是否有效?
- mysql - 合并两个查询时有什么方法可以优化这个 MySQL 查询?
- xmlhttprequest - 页面关闭替代方案中的 SyncXHR
- oauth - Google OAuth 同意屏幕不显示新请求的敏感范围
- typo3 - Typo3 10 TCA "'type' => 'inline'," 创建新关系并上传按钮自动隐藏
- testing - 如何在同一个示例项目中使用 JUnit 和手动测试自定义 gradle 插件
- postgresql - 如何在 Hasura 中创建函数 postgresql 连接表
- c++ - 如何使用 libcurl 将大字符串发送到乌鸦服务器?
- python - Python if-condition根据不同的列值导入json文件
- firebase - 使用firebase云功能和firestore实现发送/接受好友请求的通知系统