rainbowkit icon indicating copy to clipboard operation
rainbowkit copied to clipboard

[bug] Metamask window is not opening after disconnect

Open josadcha opened this issue 1 year ago • 9 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

RainbowKit Version

2.1.1

wagmi Version

2.9.9

Current Behavior

After disconnecting from Metamask and doing an attempt to connect again, the Metamask window is not opened. Issue resolves after refreshing the page:

https://github.com/rainbow-me/rainbowkit/assets/39953699/6e6eda1f-44d6-4e6b-970f-308523fc02a2

Expected Behavior

After disconnecting from Metamask and trying to initiate the connection again, the Metamask is triggered to be connected.

Steps To Reproduce

  1. Connect to Metamask
  2. DIsconnect
  3. Try to connect to Metamask again

(Sometimes the issue appears on the 2nd attempt, sometimes on the 3rd.

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

No response

Anything else?

No response

josadcha avatar Jun 06 '24 10:06 josadcha

@josadcha Looking into this now

0xmkh avatar Jun 06 '24 14:06 0xmkh

+1 Im also getting this and with the same Wagmi / Rainbowkit versions

REPTILEHAUS avatar Jun 07 '24 09:06 REPTILEHAUS

I'm able to reproduce this as well. Reporting this to the wagmi team to see what's going on.

0xmkh avatar Jun 07 '24 21:06 0xmkh

+1 I'm also getting this with Wagmi/Rainbowkit version "latest"

ducls-2223 avatar Jun 10 '24 00:06 ducls-2223

Seems like this is a EIP-1193 and EIP-6963 issue. We should be deprecating EIP-1193 pretty soon and fix this. I'll keep this issue open meanwhile 👍

0xmkh avatar Jun 12 '24 11:06 0xmkh

The problem is in disconnecting the wallet.

it becomes obvious after debugging packages/rainbowkit/src/components/ConnectOptions/DesktopOptions.tsx

  const connectToWallet = (wallet: WalletConnector) => {
    setConnectionError(false);
    if (wallet.ready) {
      wallet?.connect?.()?.catch(() => {
        setConnectionError(true);
      });
    }
  };

wallet.connect() throws ConnectorAlreadyConnectedError

The solution is to disconnect via direct connector. To achieve this you can utilize useAccount from wagmi.

const { connector } = useAccount()
const handleDisconnect = ()=>{
        connector && await connector.disconnect()
}

babkenmes avatar Jun 18 '24 16:06 babkenmes

In case you need more context on the issue, please don't hesitate to let me know. You can debug packages/core/src/actions/connect.ts and in case of disconnecting directly from connecter config.state.current becomes undefined but if useDisconnect hook is used it remains and wagmi throws the error.

  // Check if connector is already connected
  if (connector.uid === config.state.current)
    throw new ConnectorAlreadyConnectedError()

So it is not EIP-1193 or EIP-6963 issue but wagmi useDisconnect hook issue

babkenmes avatar Jun 18 '24 17:06 babkenmes

@babkenmes We have metaMaskWallet EIP-1193 and EIP-6963 connector which shares the same window.ethereum instance, that's why it throws that error since it's marked as already "connected" from Wagmi.

Feel free to check out the wagmi issue here https://github.com/wevm/wagmi/issues/4027. The issue should be resolved once we deprecate the EIP-1193 connectors 👍

0xmkh avatar Jun 19 '24 11:06 0xmkh

Here is another EIP-1193 bug we've encountered https://github.com/wevm/wagmi/issues/3606. As i said all these EIP-1193 breaking behaviours should be resolved once we migrate fully over to EIP-6963.

0xmkh avatar Jun 19 '24 11:06 0xmkh

@josadcha Are you able to reproduce this with the latest Wagmi version 2.11.1 ?

0xmkh avatar Jul 17 '24 09:07 0xmkh

@magiziz I think this is resolved now using

    "@rainbow-me/rainbowkit": "^2.1.3",
    "viem": "^2.17.4",
    "wagmi": "^2.11.1",

REPTILEHAUS avatar Jul 17 '24 15:07 REPTILEHAUS

@REPTILEHAUS Awesome. Yeah Wagmi has done a few changes to their connectors so hopefully we should see some less conflicts now.

0xmkh avatar Jul 17 '24 16:07 0xmkh

This is now fixed in the latest Wagmi version 2.11.3.

0xmkh avatar Jul 19 '24 15:07 0xmkh