Modal keeps on loading for external wallet options but sometimes randomly works

Please provide the following details too when asking for help in this category:

  • SDK Version: 7.0.1
  • Platform: Windows ( Brave Browser )
  • Browser Console Screenshots:
  • Related to Custom Authentication? Please provide the following info too: (Optional)
    • Verifier Name:
    • JWKS Endpoint:
    • Sample idToken(JWT)

Please provide the Web3Auth initialization and login code snippet below:

const [web3auth, setWeb3Auth] = useState<Web3Auth | null>(null)
  const [web3authConnector, setWeb3AuthConnector] =
    useState<Web3AuthConnector | null>(null)

  const { chains, publicClient, webSocketPublicClient } = configureChains(
    [polygonMumbai],
    [publicProvider()]
  )

  const defaultConnectors = [
    new CoinbaseWalletConnector({
      chains,
      options: {
        appName: 'wagmi',
      },
    }),
    new WalletConnectConnector({
      chains,
      options: {
        qrModalOptions: {
          themeMode: 'dark',
        },
        projectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string,
        showQrModal: true,
      },
    }),
    new InjectedConnector({
      chains,
      options: {
        name: 'Injected',
        shimDisconnect: true,
      },
    }),
  ]

  const connectors = web3authConnector
    ? [...defaultConnectors, web3authConnector as any]
    : defaultConnectors

  const config = createConfig({
    autoConnect: true,
    connectors: connectors,
    publicClient,
    webSocketPublicClient,
  })

  useEffect(() => {
    ;(async () => {
      // Initialize within your constructor
      const web3auth = new Web3Auth({
        clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID as string,
        web3AuthNetwork: 'testnet',
        chainConfig: {
          chainNamespace: CHAIN_NAMESPACES.EIP155,
          chainId: '0x13881',
        },
      })

      // Adding Wallet Connect adapter opens wallet connect modal behind the web3auth modal but doesn't fix the issue
      // Also I get this error: "NotSupportedError: Failed to execute 'define' on 'CustomElementRegistry': the name "wcm-button" has already been used with this registry" So to fix this I've to remove WalletConnectConnector in wagmi config
      // const walletConnectModal = new WalletConnectModal({
      //   projectId: process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string,
      // })

      // const walletConnectV2Settings = await getWalletConnectV2Settings(
      //   CHAIN_NAMESPACES.EIP155,
      //   [0x13881],
      //   process.env.NEXT_PUBLIC_WALLET_CONNECT_PROJECT_ID as string
      // )

      // const walletConnectV2Adapter = new WalletConnectV2Adapter({
      //   adapterSettings: {
      //     qrcodeModal: walletConnectModal,
      //     ...walletConnectV2Settings.adapterSettings,
      //   },
      //   loginSettings: {
      //     ...walletConnectV2Settings.loginSettings,
      //   },
      // })

      // // @ts-ignore
      // web3auth.configureAdapter(walletConnectV2Adapter)

      const connector = new Web3AuthConnector({
        chains: chains,
        options: {
          web3AuthInstance: web3auth,
        },
      })
      setWeb3Auth(web3auth)
      setWeb3AuthConnector(connector)
    })()
  }, [])

  useEffect(() => {
    if (web3auth) {
      // Subscribe to lifecycle events emitted by web3auth
      web3auth.on(ADAPTER_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
        defaultOnConnectEvent(data)
      })
      web3auth.on(ADAPTER_EVENTS.DISCONNECTED, () => {
        defaultOnDisconnectEvent()
      })

      web3auth.initModal()
    }
  }, [web3auth])

When I click on Connect Wallet under External Wallets section it keeps on loading ( probably it’s the issue of wallet connect ) and sometimes after hot reload or randomly trying, it somehow loads qr code and shows all appropriate options.

I’m getting same issue in the example repo: https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/wagmi-connector/wagmi-react-modal-example

I’ve also upgraded my dependencies from:
“@walletconnect/modal”: “^2.6.2”,
“@web3auth/base”: “^6.1.4”,
“@web3auth/modal”: “^6.1.1”,
“@web3auth/wallet-connect-v2-adapter”: “^7.0.2”,
“@web3auth/web3auth-wagmi-connector”: “^5.0.1”,
“wagmi”: “^1.4.1”

to:
“@walletconnect/modal”: “^2.6.2”,
“@web3auth/base”: “^7.0.1”,
“@web3auth/modal”: “^7.0.3”,
“@web3auth/wallet-connect-v2-adapter”: “^7.0.2”,
“@web3auth/web3auth-wagmi-connector”: “^5.0.1”,
“wagmi”: “^1.4.1”

( And I was expecting issue to get fix but guess what, now changing theme to dark mode doesn’t work by adding it in uiConfig )

These are the screenshots:

For issue:

And sometimes it just loads two icons (metamask and torus or something ) and not qr code but after sometime, it again keeps on loading. And for this case if I configureAdapter walletConnectV2Adapter, it keeps on loading but wallet connect modal opens behind web3auth modal ( removing web3auth modal from browser inspector shows that it’s wallet connect modal behind web3auth modal )

( I’ve attached many screenshots to describe it in more details but guess what, when I clicked on Create Topic, it then shows me that “new user can only put one embedded media item in a post”, at least it should show this message as warning when I’ve added more that one image. I’ve then remove all other screenshots :frowning: )

@ahmedaghadi123 Welcome Aboard!

Are you facing this issue with other browsers apart from Brave? If you need to share more screenshots, you can add them as seperate posts.

@vjgee I’ve tried other browsers ( chrome and firefox ), and getting same issue in other browsers too. One more thing I noticed is that when I try for the first time in new browser it works, but after that the issue persists. So If I open the website in incognito mode it works for the first time and doesn’t works after reloading. Also thus, if I close the incognito session and tries again then also it works for the first time. And this is the exact case with the official example repo: https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/wagmi-connector/wagmi-react-modal-example

hello @ahmedaghadi123
try refering to this example code, https://github.com/Web3Auth/web3auth-pnp-examples/tree/main/web-modal-sdk/blockchain-connection-examples/evm-modal-example which is always working for me
basically initializing steps are same, so it will be help to you.

I’ll update the refering wagmi connector example as well. Thanks

@pepper I know that web3auth is working without wagmi connector but I want to use wagmi. Also I found out that a person has created an issue in web3auth-wagmi-connector repo ( #108 ) regarding same ( can’t share the link thus I’ve mentioned issue number ).

Thanks for reporting.
We’ll look into this issue internally and bring the answer.

Hi @ahmedaghadi123
for the endless loading issue, this was happening due to the bug in the wallet connect

got this resolved here and it’s expected to be released until this week

1 Like

Hey @pepper
Thanks for the update!
So for the workaround I’m deleting wallet connect subscription entry from local storage before connect wallet button is loaded. So this issue is fixed but there’s new issue. When I connect metamask wallet, it shows error:

Uncaught (in promise) TypeError: Cannot read properties of null (reading 'eventNames')
    at Proxy.setTarget (baseControllers.esm.js:303:1)
    at CommonJRPCProvider.updateProviderEngineProxy (baseProvider.esm.js:141:1)
    at MetamaskAdapter.eval (noModal.esm.js:156:1)
    at safeApply (openloginJrpc.esm.js:112:1)
    at MetamaskAdapter.emit (openloginJrpc.esm.js:162:1)
    at MetamaskAdapter.connect (metamaskAdapter.esm.js:92:1)
    at async Web3Auth.connectTo (noModal.esm.js:124:1)
    at async LoginModal.eval (modal.esm.js:475:1)

I noticed that if autoConnect is true in createConfig for wagmi and we disconnect while it’s true then for the subsequent connection would result in this error untill and unless autoConnect is set to false.

So to fix this issue, I’ve to set autoConnect to false.

My code:

const { chains, publicClient, webSocketPublicClient } = configureChains(
  [polygonMumbai],
  [publicProvider()]
)

export const AuthContextProvider = ({ children }: AuthContextProviderProps) => {
  const [web3auth, setWeb3Auth] = useState<Web3Auth | null>(null)
  const [web3authConnector, setWeb3AuthConnector] =
    useState<Web3AuthConnector | null>(null)
  const [provider, setProvider] = useState<WalletClient | null>(null)
  const [loggedIn, setLoggedIn] = useState<boolean>(false)

  const connectors = [...(web3authConnector ? [web3authConnector as any] : [])]

  const config = createConfig({
    autoConnect: false, // Throws error if trying to connect after disconnecting the wallet while it's true ( untill and unless it's false again )
    connectors: connectors,
    publicClient,
    webSocketPublicClient,
  })

  // Hack to fix wallet connect issue untill they release fixed code
  useLayoutEffect(() => {
    localStorage.removeItem('wc@2:core:0.3//subscription')
  }, [])

  useEffect(() => {
    // Initialize within your constructor
    const web3auth = new Web3Auth({
      clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID as string,
      web3AuthNetwork: 'testnet',
      chainConfig: {
        chainNamespace: CHAIN_NAMESPACES.EIP155,
        chainId: '0x13881',
      },
      uiConfig: {
        theme: 'dark',
        appName: 'MyApp',
        appLogo: '/asset/logo.png',
      },
    })

    const web3authConnector = new Web3AuthConnector({
      chains: chains,
      options: {
        web3AuthInstance: web3auth,
      },
    })

    setWeb3Auth(web3auth)
    setWeb3AuthConnector(web3authConnector)
  }, [])

  useEffect(() => {
    if (web3auth) {
      // Subscribe to lifecycle events emitted by web3auth
      web3auth.on(ADAPTER_EVENTS.CONNECTED, (data: CONNECTED_EVENT_DATA) => {
        defaultOnConnectEvent(data)
      })
      web3auth.on(ADAPTER_EVENTS.DISCONNECTED, () => {
        defaultOnDisconnectEvent()
      })

      web3auth.initModal()
    }
  }, [web3auth])

  async function login() {
    if (!web3auth || web3auth.status === ADAPTER_STATUS.NOT_READY) {
      return
    }
    const providerRet = await web3auth.connect()
    if (providerRet) {
      setProvider(
        createWalletClient({
          chain: polygonMumbai,
          transport: custom(providerRet),
        })
      )
      setLoggedIn(true)
    }
  }

  async function logout() {
    if (!web3auth || web3auth.status === ADAPTER_STATUS.NOT_READY) {
      return
    }
    await web3auth.logout()
    setLoggedIn(false)
...
...
...
}

Incase you want to know versions for the dependencies:

    "@web3auth/base": "^6.1.7",
    "@web3auth/modal": "^6.1.8",
    "@web3auth/web3auth-wagmi-connector": "^5.0.1",
    "viem": "^1.10.7",
    "wagmi": "^1.4.1"

Hi @pepper
So I understand this is a bug in walletconnect library that will be fixes by the end of the week and you should update web3auth libraries after they will update walletconnect library.
What is the ETA from your side for this fix?

Thanks

Hey @ahmedaghadi123
thanks for letting me know the issue, will notice to team

Hey @yaro.hakimi
It is expected to be completed in 1-2 business days at the latest after the update on the wallet connect side.
We’ll let you know when the changes are implemented.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.