javascript - cannot disconnect or close or clear accounts from web3model or web3 - Stack Overflow

I am trying to disconnect from the provider so that I can show a screen with a button to connect when n

I am trying to disconnect from the provider so that I can show a screen with a button to connect when not connected and show a display screen when already connected.

the example code doesn't work:

public resetApp = async () => {
    const { web3 } = this.state;
    if (web3 && web3.currentProvider && web3.currentProvider.close) {
      await web3.currentProvider.close();
    }
    await this.web3Modal.clearCachedProvider();
    this.setState({ ...INITIAL_STATE });
  };

web3.currentProvider.close(); is not a function. the condition is never met. all examples on the web doesn't work.

this code below always returns an address and nothing ive tried has cleared it except for manual disabling metamask and enable it again from the chrome extension settings.

      const accounts = await web3.eth.getAccounts();
      if (accounts[0] != null) {
          // connected
          resolve(true);
      } else {
          // not connected
          resolve(false);
      }

I am trying to disconnect from the provider so that I can show a screen with a button to connect when not connected and show a display screen when already connected.

the example code doesn't work:

public resetApp = async () => {
    const { web3 } = this.state;
    if (web3 && web3.currentProvider && web3.currentProvider.close) {
      await web3.currentProvider.close();
    }
    await this.web3Modal.clearCachedProvider();
    this.setState({ ...INITIAL_STATE });
  };

web3.currentProvider.close(); is not a function. the condition is never met. all examples on the web doesn't work.

this code below always returns an address and nothing ive tried has cleared it except for manual disabling metamask and enable it again from the chrome extension settings.

      const accounts = await web3.eth.getAccounts();
      if (accounts[0] != null) {
          // connected
          resolve(true);
      } else {
          // not connected
          resolve(false);
      }
Share Improve this question edited Aug 30, 2022 at 16:33 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Jul 6, 2021 at 14:18 loekTheDreamerloekTheDreamer 5861 gold badge8 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

Found the solution: My condition was testing the wrong thing (not sure how else to say it). If you call await web3Modal.clearCachedProvider(); then web3Modal.cachedProvider returns an empty string.

// test if wallet is connected
if (web3Modal.cachedProvider) {
    // connected now you can get accounts
    const accounts = await web3.eth.getAccounts();
}

// disconnect wallet
const disconnectWallet = async (web3Modal: any) => {
    await web3Modal.clearCachedProvider();
}

This will work;

async onDisconnect() {
  await web3Modal.clearCachedProvider();
  window.localStorage.clear();
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745413009a4626617.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信