appkit icon indicating copy to clipboard operation
appkit copied to clipboard

Difficulty Disconnecting MetaMask Wallet through useDisconnect Method in Wagmi

Open ghost opened this issue 3 years ago • 2 comments

What problem does this new feature solve?

Hello there! I hope you're doing well. I wanted to bring up an issue I encountered while using the Wagmi library in combination with Web3Modal and the WalletConnect connector. The problem arises when attempting to fully disconnect the MetaMask wallet after establishing a connection using the following configuration:

const chainsList = [arbitrum, mainnet, polygon];
const { chains, publicClient, webSocketPublicClient } = configureChains(
  chainsList,
  [w3mProvider({ projectId: web3ProjectId }), publicProvider()]
);
const wagmiConfig = createConfig({
  autoConnect: true,
  connectors: w3mConnectors({ projectId: web3ProjectId, version: 1, chains }),
  publicClient,
  webSocketPublicClient,
});
return (<WagmiConfig config={wagmiConfig}><App /></WagmiConfig><Web3Modal projectId={web3ProjectId} ethereumClient={ethereumClient} />);

After successfully connecting my MetaMask wallet to Web3Modal using the useWeb3Modal hook:

const { open } = useWeb3Modal();

I found that the useDisconnect method provided by Wagmi doesn't fully disconnect the MetaMask wallet. Instead, it only removes the connected account from the useAccount hook.

Here's an example of how I used the useDisconnect method:

const { disconnect } = useDisconnect();

I expected this method to fully disconnect the MetaMask wallet, reverting it to its initial state where it prompts the user to select an account from their MetaMask account list. However, it appears that it only disconnects the account associated with the website, without affecting the MetaMask wallet itself.

I also noticed that the official Next.js Wagmi example provided in the Web3Modal repository exhibits the same limitation. You can find the example here: Next.js Wagmi Example

Could you please guide me on how to force a complete disconnection of the MetaMask wallet? It would be great to have a method or workaround to fully disconnect the wallet or perform a hard reset, ensuring that the user is prompted to select an account again.

Thank you for your time and assistance. I appreciate your help in resolving this issue!

"@walletconnect/sign-client": "^2.8.0",
"@web3modal/ethereum": "^2.4.5",
"@web3modal/react": "^2.4.5",
"viem": "1.0.2",
"wagmi": "^1.1.1",

Describe the solution you'd like

ghost avatar Jun 10 '23 20:06 ghost

I have the same issue. the worst bugs begin when switching accounts. In my case, I thought the wallet was disconnected and used the same disconnect method of useDisconnect in wagmi, and then switched accounts. it was unable to detect my wallet address because the new account was never connected but at the same time the useAccount still had the info and functionality from the previous account that was in use.

whateverneveranywhere avatar Jun 10 '23 20:06 whateverneveranywhere

In general connecting and disconnecting via wagmi and any other similar library will remove connection on "storage" level. You account still remains in connected state in MetaMask, but dapp no longer remembers this. When you connect again it is instant i.e. no user approval.

The only way to fully disconnect is from metamask extension itself.

This works similar with other extensions, when you use other wallets which are mobile, disconnect works as a full one.

I'm not sure about other issues caused by account switching, so would suggest chatting to wagmi's ream (web3modal doesn't manipulate this data in any way, we only "read" it).

That could also be a logic issue in the dapp itself, make sure your hooks are place in correct render loops.

xzilja avatar Jun 11 '23 07:06 xzilja