From b5a444e8c51e63d965315fe88ec1cdfff2d0fd72 Mon Sep 17 00:00:00 2001 From: duckception Date: Wed, 16 Aug 2023 18:49:21 +0200 Subject: [PATCH] Add support for Ganache, Hardhat and Anvil --- src/index.js | 79 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 20 deletions(-) diff --git a/src/index.js b/src/index.js index 23a0541f..fad9a83e 100644 --- a/src/index.js +++ b/src/index.js @@ -483,30 +483,69 @@ const initialize = async () => { } }; + async function getLocalNodeChainId() { + try { + const response = await fetch('http://127.0.0.1:8545', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + jsonrpc: '2.0', + method: 'eth_chainId', + params: [], + id: 1, + }), + }); + + const chainId = (await response.json()).result; + const chainIdDecimal = parseInt(chainId, 16); + console.log( + `Fetched chain ID from local node: ${chainId} (${chainIdDecimal})`, + ); + + return chainId; + } catch (err) { + if (err.message === 'Failed to fetch') { + throw new Error('Local node RPC is unavailable. Cannot fetch chain ID'); + } + + throw err; + } + } + addEthereumChain.onclick = async () => { - await ethereum.request({ - method: 'wallet_addEthereumChain', - params: [ - { - chainId: '0x539', - rpcUrls: ['http://127.0.0.1:8545'], - chainName: 'Localhost 8545', - nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' }, - blockExplorerUrls: null, - }, - ], - }); + try { + const chainId = await getLocalNodeChainId(); + await ethereum.request({ + method: 'wallet_addEthereumChain', + params: [ + { + chainId, + rpcUrls: ['http://127.0.0.1:8545'], + chainName: 'Localhost 8545', + nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' }, + blockExplorerUrls: null, + }, + ], + }); + } catch (err) { + console.error(err); + } }; switchEthereumChain.onclick = async () => { - await ethereum.request({ - method: 'wallet_switchEthereumChain', - params: [ - { - chainId: '0x539', - }, - ], - }); + try { + const chainId = await getLocalNodeChainId(); + await ethereum.request({ + method: 'wallet_switchEthereumChain', + params: [ + { + chainId, + }, + ], + }); + } catch (err) { + console.error(err); + } }; const initializeAccountButtons = () => {