MetaMaskでブロックチェーンネットワークの変更リクエストをトリガーする方法 質問する

MetaMaskでブロックチェーンネットワークの変更リクエストをトリガーする方法 質問する

私は最初の dapp テストに web3 を使用していますが、MetaMask がユーザーに、まだ選択されていない場合は Binance (BSC) ネットワークに変更するように促すようにしたいと思います。

メタマスク ネットワーク変更リクエスト

このようなリクエストをトリガーするにはどうすればよいでしょうか?

ベストアンサー1

ついに答えを見つけることができました:

await window.ethereum.request({
  method: 'wallet_switchEthereumChain',
  params: [{ chainId: '0x61' }], // chainId must be in hexadecimal numbers
});

より包括的な回答としては、MetaMask がインストールされているかどうか、また、Dapp が接続したいチェーンがインストールされているかどうかを確認し、インストールされていない場合はインストールします。

 // Check if MetaMask is installed
 // MetaMask injects the global API into window.ethereum
 if (window.ethereum) {
      try {
        // check if the chain to connect to is installed
        await window.ethereum.request({
          method: 'wallet_switchEthereumChain',
          params: [{ chainId: '0x61' }], // chainId must be in hexadecimal numbers
        });
      } catch (error) {
        // This error code indicates that the chain has not been added to MetaMask
        // if it is not, then install it into the user MetaMask
        if (error.code === 4902) {
          try {
            await window.ethereum.request({
              method: 'wallet_addEthereumChain',
              params: [
                {
                  chainId: '0x61',
                  rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/',
                },
              ],
            });
          } catch (addError) {
            console.error(addError);
          }
        }
        console.error(error);
      }
    } else {
      // if no window.ethereum then MetaMask is not installed
      alert('MetaMask is not installed. Please consider installing it: https://metamask.io/download.html');
    } 

おすすめ記事