wagmi icon indicating copy to clipboard operation
wagmi copied to clipboard

useConnect with MetaMask Takes Very Long Time to Confirm Connection Success

Open isLiuyinghao opened this issue 1 year ago • 4 comments

Check existing issues

Describe the bug

When using Wagmi's useConnect hook to connect to a MetaMask wallet, it takes an excessively long time (several seconds or more) to retrieve the connection status and confirm whether the user has successfully connected. In contrast, connecting directly with Web3.js works smoothly and returns the result almost instantly. This delay negatively impacts the user experience. Steps to Reproduce Set up a Vue 3 project with Wagmi using the configuration below.

Use the provided component code to initiate a MetaMask connection via useConnect.

Click the "MetaMask" button to connect.

Observe that it takes a very long time to update the status or retrieve the connected account.

Expected Behavior The connection process with MetaMask using Wagmi should be fast and seamless, similar to Web3.js, with the status updating promptly after the user confirms the connection in MetaMask. Actual Behavior The connection process takes an unusually long time (e.g., 5-10 seconds or more) to reflect the success state, even after the user authorizes the connection in MetaMask. The status remains in a pending state for too long. Code Example Component Code vue

<script setup lang="ts">
import { useChainId, useConnect } from '@wagmi/vue'

const chainId = useChainId()
const { connect, connectors, error, status } = useConnect()
</script>

<template>
  <h2>Connect</h2>

  <button v-for="connector in connectors" :key="connector.id" type="button" @click="connect({ connector, chainId })">
    {{ connector.name }}
  </button>

  <div>
    {{ status }}
    {{ error?.message }}
  </div>
</template>

Wagmi Configuration javascript

import { http, createConfig, createStorage } from '@wagmi/vue'
import { mainnet, optimism, sepolia } from '@wagmi/vue/chains'
import { coinbaseWallet, walletConnect } from '@wagmi/vue/connectors'

export const config = createConfig({
  chains: [mainnet, sepolia, optimism],
  connectors: [
    walletConnect({
      projectId: import.meta.env.VITE_WC_PROJECT_ID,
    }),
    coinbaseWallet({ appName: 'Vite Vue Playground', darkMode: true }),
  ],
  storage: createStorage({ storage: localStorage, key: 'vite-vue' }),
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
    [optimism.id]: http(),
  },
})

declare module '@wagmi/vue' {
  interface Register {
    config: typeof config
  }
}

Working Web3.js Comparison For comparison, the following Web3.js code connects to MetaMask instantly: javascript

import Web3 from 'web3';
const web3 = new Web3(window.ethereum);
const accounts = await web3.eth.requestAccounts();
console.log('Connected:', accounts[0]);

Environment Wagmi Version: @wagmi/[email protected] (please replace with your exact version, e.g., 1.8.0)

Viem Version: [email protected] (please replace with your exact version, e.g., 2.23.5)

MetaMask Version: 7.43.0

Browser: MetaMask Dapp

Network: Ethereum Mainnet (or specify if using a different chain)

Additional Context The issue does not seem to occur when using Web3.js with the same MetaMask version, suggesting a potential delay or bottleneck in Wagmi's connection handling.

There are no visible errors in the console, but the status remains pending for an extended period before transitioning to success.

I suspect this could be related to how Wagmi interacts with MetaMask's window.ethereum provider or the http() transport configuration.

Request Could you please investigate why Wagmi's useConnect hook takes significantly longer than Web3.js to confirm a MetaMask connection? Any insights or optimizations to make the process faster would be greatly appreciated. Let me know if you need more details or logs to debug this issue.

Link to Minimal Reproducible Example

No response

Steps To Reproduce

No response

What Wagmi package(s) are you using?

@wagmi/vue

Wagmi Package(s) Version(s)

latest

Viem Version

latest

TypeScript Version

No response

Anything else?

No response

isLiuyinghao avatar Apr 01 '25 15:04 isLiuyinghao

Hello @isLiuyinghao.

Please provide a minimal reproduction using StackBlitz, TypeScript Playground (for type issues), or a separate minimal GitHub repository.

Minimal reproductions are required as they save us a lot of time reproducing your config/environment and issue, and allow us to help you faster.

Once a minimal reproduction is added, a team member will confirm it works, then re-open the issue.

github-actions[bot] avatar Apr 02 '25 00:04 github-actions[bot]

github: https://github.com/isLiuyinghao/wagmi-project.git

Operating Environment: ios、android model:iphone 13、iphone 16、samsung s24 ultra

isLiuyinghao avatar Apr 02 '25 02:04 isLiuyinghao

Hello @isLiuyinghao.

Please provide a minimal reproduction using StackBlitz, TypeScript Playground (for type issues), or a separate minimal GitHub repository.

Minimal reproductions are required as they save us a lot of time reproducing your config/environment and issue, and allow us to help you faster.

Once a minimal reproduction is added, a team member will confirm it works, then re-open the issue.

github: https://github.com/isLiuyinghao/wagmi-project.git

Operating Environment: ios、android model:iphone 13、iphone 16、samsung s24 ultra

isLiuyinghao avatar Apr 02 '25 08:04 isLiuyinghao

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 questions or comments you can create a new discussion thread.

github-actions[bot] avatar Apr 17 '25 00:04 github-actions[bot]