5 请问怎么让ethers自动切换metamask到当前页面正在使用的网络呢?

如题,想实现这个需求,搜了下资料只看到用web3来实现这样的需求。但我习惯用ethers。所以想了解下ethers怎么做到这些功能。

请先 登录 后评论

最佳答案 2022-06-24 08:39

参考: https://learnblockchain.cn/article/3675

/**
* @description: Switch to the rest of the network (mainly used)
* @param {*} findChain
* @return {*}
  */
  async function switchToOtherNetwork(findChain) {
  const data = [];
  data.push(findChain);
  console.log(findChain, "switchNetwork");
  try {
  await window.ethereum.request({
  method: "wallet_addEthereumChain",
  params: data // [{XXXXX}]  is Array
  });
  } catch (error) {
  console.log(error);
  }
  }
请先 登录 后评论

其它 1 个回答

QuinnLee1020
请先 登录 后评论