Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add chains with empty blockExplorers to MetaMask #3652

Closed
1 task done
tash-2s opened this issue Feb 29, 2024 · 3 comments · Fixed by #3653
Closed
1 task done

Cannot add chains with empty blockExplorers to MetaMask #3652

tash-2s opened this issue Feb 29, 2024 · 3 comments · Fixed by #3653
Labels
Good First Issue Misc: Good First Issue

Comments

@tash-2s
Copy link
Contributor

tash-2s commented Feb 29, 2024

Describe the bug

When attempting to add the Foundry chain to MetaMask, I encounter the following error:

MetaMask - RPC Error: Expected null or array with at least one valid string HTTPS URL 'blockExplorerUrl'.

I discovered that adding the blockExplorers parameter resolves the issue.

Wagmi configures wallet_addEthereumChain's blockExplorerUrls based on the chain's blockExplorers. The current Wagmi implementation leads to an empty array for blockExplorerUrls when chains lack blockExplorers.

const { default: blockExplorer, ...blockExplorers } =
chain.blockExplorers ?? {}
let blockExplorerUrls: string[] = []
if (blockExplorer)
blockExplorerUrls = [
blockExplorer.url,
...Object.values(blockExplorers).map((x) => x.url),
]
await provider.request({
method: 'wallet_addEthereumChain',
params: [
{
chainId: numberToHex(chainId),
chainName: chain.name,
nativeCurrency: chain.nativeCurrency,
rpcUrls: [chain.rpcUrls.default?.http[0] ?? ''],
blockExplorerUrls,
},
],
})

According to MetaMask documentation, they require "One or more URLs", indicating that the empty array triggers the error.

I propose excluding blockExplorerUrls for chains that do not have blockExplorers.

Link to Minimal Reproducible Example

No response

Steps To Reproduce

Create a project:

pnpm create wagmi@2.0.10 --template vite-vanilla

Apply this diff:

diff --git a/src/main.ts b/src/main.ts
index 257488e..47c0748 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -1,11 +1,17 @@
-import { connect, disconnect, reconnect, watchAccount } from '@wagmi/core'
+import { connect, disconnect, reconnect, watchAccount, switchChain } from '@wagmi/core'
 import { Buffer } from 'buffer'
+import { foundry } from '@wagmi/core/chains'
 
 import './style.css'
 import { config } from './wagmi'
 
 globalThis.Buffer = Buffer
 
+const button = document.createElement('button')
+button.textContent = 'Switch to foundry'
+button.onclick = () => switchChain(config, { chainId: foundry.id })
+document.body.appendChild(button)
+
 document.querySelector<HTMLDivElement>('#app')!.innerHTML = `
   <div>
     <div id="account">
diff --git a/src/wagmi.ts b/src/wagmi.ts
index 3bce606..ebadbf5 100644
--- a/src/wagmi.ts
+++ b/src/wagmi.ts
@@ -1,16 +1,15 @@
-import { coinbaseWallet, injected, walletConnect } from '@wagmi/connectors'
+import { injected } from '@wagmi/connectors'
 import { http, createConfig } from '@wagmi/core'
-import { mainnet, sepolia } from '@wagmi/core/chains'
+import { mainnet, sepolia, foundry } from '@wagmi/core/chains'
 
 export const config = createConfig({
-  chains: [mainnet, sepolia],
+  chains: [mainnet, sepolia, foundry],
   connectors: [
     injected(),
-    coinbaseWallet({ appName: 'Create Wagmi' }),
-    walletConnect({ projectId: import.meta.env.VITE_WC_PROJECT_ID }),
   ],
   transports: {
     [mainnet.id]: http(),
     [sepolia.id]: http(),
+    [foundry.id]: http(),
   },
 })

Run the project, connect MetaMask, and switch to the Foundry chain; then, the following error occurs:

MetaMask - RPC Error: Expected null or array with at least one valid string HTTPS URL 'blockExplorerUrl'.
Screenshot 2024-02-28 at 21 36 27

By modifying as follows:

diff --git a/src/wagmi.ts b/src/wagmi.ts
index ebadbf5..1a91b83 100644
--- a/src/wagmi.ts
+++ b/src/wagmi.ts
@@ -2,8 +2,10 @@ import { injected } from '@wagmi/connectors'
 import { http, createConfig } from '@wagmi/core'
 import { mainnet, sepolia, foundry } from '@wagmi/core/chains'
 
+const f = { ...foundry, blockExplorers: { default: { name: "", url: "https://example.com" } } }
+
 export const config = createConfig({
-  chains: [mainnet, sepolia, foundry],
+  chains: [mainnet, sepolia, f],
   connectors: [
     injected(),
   ],

The error is resolved, and everything works as expected:

Screenshot 2024-02-28 at 21 39 55

Wagmi Version

2.6.5

Viem Version

2.7.16

TypeScript Version

5.3.3

Check existing issues

Anything else?

No response

@tmm
Copy link
Member

tmm commented Feb 29, 2024

Thanks for the detailed issue @tash-2s! You are welcome to submit a PR (excluding blockExplorerUrls for chains that do not have blockExplorers) if you want credit. Otherwise, will get to this soon.

@tmm tmm added the Good First Issue Misc: Good First Issue label Feb 29, 2024
@tash-2s
Copy link
Contributor Author

tash-2s commented Feb 29, 2024

@tmm Thanks for taking a look. Let me try quickly!

Copy link
Contributor

github-actions bot commented Apr 1, 2024

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest wagmi version. If you have any other comments you can create a new discussion.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Good First Issue Misc: Good First Issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants