最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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();
}
发布评论

评论列表(0)

  1. 暂无评论