简体   繁体   English

无法从 web3model 或 web3 断开或关闭或清除帐户

[英]cannot disconnect or close or clear accounts from web3model or web3

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.不是 function。 the condition is never met.条件从未满足。 all examples on the web doesn't work. web 上的所有示例都不起作用。

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.下面的这段代码总是返回一个地址,除了手动禁用元掩码并从 chrome 扩展设置再次启用它之外,我没有尝试过清除它。

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

Please can anyone assist.请任何人都可以提供帮助。

Found the solution: My condition was testing the wrong thing (not sure how else to say it).找到解决方案:我的情况是测试错误的东西(不知道怎么说)。 If you call await web3Modal.clearCachedProvider();如果你调用 await web3Modal.clearCachedProvider(); then web3Modal.cachedProvider returns an empty string.然后web3Modal.cachedProvider返回一个空字符串。

// 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM