[Ethereum] How to disconnect metamask wallet from a dapp using web3

metamaskreactweb3js

I tried connecting metamask wallet with web3.js, and it got succeeded.

const handleClick = async () => {
    try {
      // Get network provider and web3 instance.
      const web3 = await GetWeb3();
      // console.log(web3);

      // Use web3 to get the user's accounts.
      const accounts = await web3.eth.getAccounts();

      // Get the contract instance.
      const networkId = await web3.eth.net.getId();
      const deployedNetwork = smartContract.networks[networkId];
      const instance = new web3.eth.Contract(
        smartContract.abi,
        deployedNetwork && deployedNetwork.address
      );

      if (accounts) {
        dispatch({
          type: "WALLET_CONNECT",
          payload: {
            isWalletConnected: true,
            web3: web3,
            accounts: accounts,
            contract: instance,
          },
        });
      }

      console.log("connected");

      console.log(web3, accounts, instance);
    } catch (error) {
      alert(
        `Failed to load web3, accounts, or contract. Check console for details.`
      );
      console.log(error);
    }
  };

Now I would like to disconnect metamask wallet from the dapp, when the disconnect button is clicked, just like a logout button. Can anyone help me with this?

Best Answer

It's not possible.

Dapp's which have a "disconnect" button didn't actually disconnecting metamask. You are able to see that in Metamask interface still displays connected even after clicking logout.

Related Topic