references icon indicating copy to clipboard operation
references copied to clipboard

bug: "Chains must not be empty" using WalletConnect V2

Open mubshrx opened this issue 3 years ago • 38 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Package Version

0.12.10

Current Behavior

I am using wagmi and WalletConnectConnector to create a custom QR Code using display_uri.

I get the uri and create a QR code from it.

When I scan it I get an error with message Chains must not be empty.

But when I use the same code with WalletConnectLegacyConnector from wagmi, then the wallet connects without any issues.

Error for V2: image

💥 And it is not only happening with me. I just tested the Getting Started guide of ConnectKit with WalletConnectConnector and it was showing the same error while it was working fine with WalletConnectLegacyConnector.

👉🏻 I am not sure if the issue is in wagmi's WalletConnectConnector or in @walletconnect/ethereum-provider

Expected Behavior

Wallet Connect should not throw error Chains must not be empty and should connect successfully.

Steps To Reproduce

client.ts

import { configureChains, createClient } from "wagmi";
import { mainnet } from "wagmi/chains";
import { WalletConnectConnector } from "wagmi/connectors/walletConnect";
import { publicProvider } from "wagmi/providers/public";
import { Config } from "@/config";

const supportedChains = [mainnet];

const { chains, provider } = configureChains(supportedChains, [
  publicProvider(),
]);

export const wagmiClient = createClient({
  autoConnect: true,
  connectors: [
    new WalletConnectConnector({
      chains,
      options: {
        projectId: Config.Blockchain.WALLET_CONNECT_PROJECT_ID,
        showQrModal: false,
      },
    }),
  ],
  provider,
});

Context Providers

"use client";

import { WagmiConfig } from "wagmi";
import { ConnectKitProvider } from "connectkit";
import { wagmiClient } from "@/web3/wagmi/client";

interface Props {
  children: React.ReactNode;
}

const Providers: React.FC<Props> = ({ children }) => {
  return (
    <>
      <WagmiConfig client={wagmiClient}>
        <ConnectKitProvider>{children}</ConnectKitProvider>
      </WagmiConfig>
    </>
  );
};

export default Providers;

Home Page

"use client";

import { ConnectKitButton } from "connectkit";

export default async function Home() {
  return (
    <>
      <h1>Home</h1>

      <ConnectKitButton />
    </>
  );
}

Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)

https://github.com/mhm13dev/wagmi-wallet-connect-v2/tree/chains-empty-issue

Anything else?

No response

mubshrx avatar Apr 09 '23 21:04 mubshrx

Okay, so after testing my app with https://react-wallet.walletconnect.com/walletconnect my app was connecting without issues.

So I guess problem is not with wagmi or with wallet connect v2 but with the wallet I was using: Trust Wallet

mubshrx avatar Apr 09 '23 21:04 mubshrx

I found that Trust Wallet supports Wallet Connect V2 https://explorer.walletconnect.com/?version=2

And tested Trust Wallet this app which is using Wallet Connect V2. https://react-app.walletconnect.com/

So I think the problem might be in Wagmi.

Can you please try it with getting started guide of ConnectKit using WalletConnectConnector connector to reproduce it?

mubshrx avatar Apr 09 '23 22:04 mubshrx

@tmm reopen it please

mubshrx avatar Apr 09 '23 22:04 mubshrx

Here are the examples which use Wallet Connect V2 and none them use Wagmi and they work fine with Trust Wallet https://github.com/WalletConnect/web-examples

mubshrx avatar Apr 09 '23 22:04 mubshrx

Thanks for reporting. This issue is temporarily closed due to lack of information. Please provide a minimal reproducible example and reopen this issue.

tmm avatar Apr 09 '23 22:04 tmm

I can't seem to reproduce with the vanilla wagmi bootstrapped from pnpm create wagmi. Can you confirm what ConnectKit version you are on? Ideally, a minimal reproducible example (above comment) would be great so we can confirm the environment details.

jxom avatar Apr 09 '23 23:04 jxom

Sure I will share my repo later.

mubshrx avatar Apr 09 '23 23:04 mubshrx

https://github.com/mhm13dev/wagmi-wallet-connect-v2/tree/chains-empty-issue

Here is the code

mubshrx avatar Apr 09 '23 23:04 mubshrx

I can't seem to reproduce with the vanilla wagmi bootstrapped from pnpm create wagmi. Can you confirm what ConnectKit version you are on? Ideally, a minimal reproducible example (above comment) would be great so we can confirm the environment details.

I am having the same issue and can reproduce it with a standard bootstrapped wagmi project. I just ran npm create wagmi and used the Next.js and default presets.

jisensee avatar May 29 '23 14:05 jisensee

you need to wrap the ConnectKit button on a use client component https://github.com/mhm13dev/wagmi-wallet-connect-v2/pull/1

glitch-txs avatar Jun 05 '23 13:06 glitch-txs

Having the same issue with NextJS using default presets.

adeelibr avatar Jun 06 '23 22:06 adeelibr

Got the same issue here. using NextJs 13.

m4kgsjd avatar Jun 07 '23 13:06 m4kgsjd

removing chains worked for me

export const wagmiClient = createClient({
  autoConnect: true,
  connectors: [
    new WalletConnectConnector({
      options: {
        projectId: Config.Blockchain.WALLET_CONNECT_PROJECT_ID,
        showQrModal: false,
      },
    }),
  ],
  provider,
});

the problem is we are only passing mainnet to the list of chains, once we remove chains, the default chains list is included which includes all of it.

adeelibr avatar Jun 07 '23 14:06 adeelibr

But what if we want to allow only specific chains?

mubshrx avatar Jun 07 '23 14:06 mubshrx

@adeelibr thx, you saved my ass, but when we dont specify the chain we want to interact with, this error occurs;

SwitchChainError: Error switching chain
    at WalletConnectConnector.switchChain (walletConnect.js:166:1)
    at WalletConnectConnector.getProvider (walletConnect.js:132:1)
    at WalletConnectConnector.getSigner (walletConnect.js:137:1)
    at fetchSigner (chunk-VQG3VKOR.js:2865:1)
    at Object.queryFn4 [as queryFn] (index.js:779:21)
    at Object.fetchFn [as fn] (query.mjs:252:1)
    at run (retryer.mjs:101:1)
    at createRetryer (retryer.mjs:149:1)
    at Query.fetch (query.mjs:307:33)
    at QueryObserver.executeFetch (queryObserver.mjs:168:1)
    at QueryObserver.onSubscribe (queryObserver.mjs:28:1)
    at QueryObserver.subscribe (subscribable.mjs:12:1)
    at eval (index.js:174:1)
    at subscribeToStore (react-dom.development.js:16958:1)
    at commitHookEffectListMount (react-dom.development.js:23150:1)
    at commitPassiveMountOnFiber (react-dom.development.js:24926:1)
    at commitPassiveMountEffects_complete (react-dom.development.js:24891:1)
    at commitPassiveMountEffects_begin (react-dom.development.js:24878:1)
    at commitPassiveMountEffects (react-dom.development.js:24866:1)
    at flushPassiveEffectsImpl (react-dom.development.js:27039:1)
    at flushPassiveEffects (react-dom.development.js:26984:1)
    at eval (react-dom.development.js:26769:1)
    at workLoop (scheduler.development.js:266:1)
    at flushWork (scheduler.development.js:239:1)
    at MessagePort.performWorkUntilDeadline (scheduler.development.js:533:1)

Have you tried to sendTransaction without configuring chain?

m4kgsjd avatar Jun 07 '23 17:06 m4kgsjd

seems sendting transaction is not working without configuring chain...

m4kgsjd avatar Jun 07 '23 18:06 m4kgsjd

adding more than one chain seems to fix the issue as well

glitch-txs avatar Jun 07 '23 21:06 glitch-txs

I can also confirm, when having only 1 chain, I am getting "Chains must not be empty".

Once I added 2 chains(see below) I now get the following error:

New Error

react_devtools_backend_compact.js:2367 SwitchChainError: An error occurred when attempting to switch chain.

Details: Chain 'eip155:5' not approved. Please use one of the following: eip155:1
Version: [email protected]
    at WalletConnectConnector.switchChain (walletConnect.js:208:13)
    at async WalletConnectConnector.getProvider (walletConnect.js:134:7)
    at async Promise.all (:3000/index 0)
    at async WalletConnectConnector.getWalletClient (walletConnect.js:138:33)
    at async getWalletClient (chunk-NRSD7F2O.js:1986:24)

Code that fixed "Chains must not be empty" error.

const { chains, publicClient, webSocketPublicClient } = configureChains(
  [mainnet, goerli], // Added additional chain for testing but I really just want 1 depending on environment
  [alchemyProvider({ apiKey: ALCHEMY_API_KEY }), publicProvider()],
);

abartolo avatar Jun 07 '23 22:06 abartolo

@abartolo that error means that the wallet doesn't support Goerli, you should add a different one, probably is a wallet that doesn't support testnet?

glitch-txs avatar Jun 07 '23 22:06 glitch-txs

@jxom @tmm I think I found the issue, might be related to a version update from ethereum-provider or from wallets maybe.

optionalChains will throw 'Chains must not be empty' error if you pass an empty array, which wagmi's connector does if only one chain is selected in config.

const [defaultChain, ...optionalChains] = this.chains.map(({ id }) => id)
console.log(optionalChains) // [] <-- empty array if only one chain is selected

I've tested it in the latest version of wagmi, also using ethereum-provider alone.

//pages/_app.tsx

import '@/styles/globals.css'
import type { AppProps } from 'next/app'
import { createConfig, configureChains, mainnet, WagmiConfig } from 'wagmi'
import { publicProvider } from 'wagmi/providers/public'
import { WalletConnectConnector } from 'wagmi/connectors/walletConnect'
 
const { chains, publicClient } = configureChains([mainnet], [publicProvider()])
 
const config = createConfig({
  connectors: [
    new WalletConnectConnector({
      chains,
      options: {
        projectId: process.env.NEXT_PUBLIC_PROJECT_ID as string,
      },
    }),
  ],
  publicClient,
})

export default function App({ Component, pageProps }: AppProps) {
  return (    
    <WagmiConfig config={config}>
      <Component {...pageProps} />
    </WagmiConfig>
  )
}



// pages/index.tsx

import styles from '@/styles/Home.module.css'
import { useConnect } from 'wagmi'

export default function Home() {
  const { connect, connectors } = useConnect()
  return (
    <>
      <main className={styles.main}>
        {connectors.map(connector => <button key={connector.id} onClick={()=>connect({connector})} >{connector.name}</button>)}
      </main>
    </>
  )
}

I tested this using Trust Wallet*

glitch-txs avatar Jun 08 '23 00:06 glitch-txs