From 13fad41b4c2ab0490afa1d29fd00e006b0dc03e4 Mon Sep 17 00:00:00 2001 From: duckception Date: Wed, 16 Aug 2023 18:10:05 +0200 Subject: [PATCH 1/4] Use default local Ganache network info --- src/index.html | 4 ++-- src/index.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.html b/src/index.html index 92b38b25..6a7da330 100644 --- a/src/index.html +++ b/src/index.html @@ -944,14 +944,14 @@

id="addEthereumChain" disabled > - Add Localhost 8546 + Add Localhost 8545 diff --git a/src/index.js b/src/index.js index 640cddee..23a0541f 100644 --- a/src/index.js +++ b/src/index.js @@ -488,10 +488,10 @@ const initialize = async () => { method: 'wallet_addEthereumChain', params: [ { - chainId: '0x53a', - rpcUrls: ['http://127.0.0.1:8546'], - chainName: 'Localhost 8546', - nativeCurrency: { name: 'TEST', decimals: 18, symbol: 'TEST' }, + chainId: '0x539', + rpcUrls: ['http://127.0.0.1:8545'], + chainName: 'Localhost 8545', + nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' }, blockExplorerUrls: null, }, ], @@ -503,7 +503,7 @@ const initialize = async () => { method: 'wallet_switchEthereumChain', params: [ { - chainId: '0x53a', + chainId: '0x539', }, ], }); From b5a444e8c51e63d965315fe88ec1cdfff2d0fd72 Mon Sep 17 00:00:00 2001 From: duckception Date: Wed, 16 Aug 2023 18:49:21 +0200 Subject: [PATCH 2/4] 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 = () => { From d5c9f0eb908c659e7ff5b7488e27efdc00dd5b87 Mon Sep 17 00:00:00 2001 From: duckception Date: Wed, 16 Aug 2023 21:32:40 +0200 Subject: [PATCH 3/4] Go back to port `8546` for local node --- src/index.html | 4 ++-- src/index.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.html b/src/index.html index 6a7da330..92b38b25 100644 --- a/src/index.html +++ b/src/index.html @@ -944,14 +944,14 @@

id="addEthereumChain" disabled > - Add Localhost 8545 + Add Localhost 8546 diff --git a/src/index.js b/src/index.js index fad9a83e..fded8155 100644 --- a/src/index.js +++ b/src/index.js @@ -485,7 +485,7 @@ const initialize = async () => { async function getLocalNodeChainId() { try { - const response = await fetch('http://127.0.0.1:8545', { + const response = await fetch('http://127.0.0.1:8546', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -520,8 +520,8 @@ const initialize = async () => { params: [ { chainId, - rpcUrls: ['http://127.0.0.1:8545'], - chainName: 'Localhost 8545', + rpcUrls: ['http://127.0.0.1:8546'], + chainName: 'Localhost 8546', nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' }, blockExplorerUrls: null, }, From bf019909433875462b3d590efa1fad2419c9ff29 Mon Sep 17 00:00:00 2001 From: duckception Date: Wed, 16 Aug 2023 21:34:01 +0200 Subject: [PATCH 4/4] Go back to `TEST` currency --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index fded8155..d23c7d06 100644 --- a/src/index.js +++ b/src/index.js @@ -522,7 +522,7 @@ const initialize = async () => { chainId, rpcUrls: ['http://127.0.0.1:8546'], chainName: 'Localhost 8546', - nativeCurrency: { name: 'Ether', decimals: 18, symbol: 'ETH' }, + nativeCurrency: { name: 'TEST', decimals: 18, symbol: 'TEST' }, blockExplorerUrls: null, }, ],