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

happypl happypl 提出于 2022-06-23 15:30 4641 浏览

如题,想实现这个需求,搜了下资料只看到用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);
  }
  }
Tiny熊

擅长:智能合约,以太坊 · 采纳率 33% · 回答于 2022-06-24 08:39

其它 1 个回答

写回答

你需要登录后才可以回答问题,登录