Web3js

獲取目前元遮罩帳戶方法不再起作用

  • September 13, 2020

我正在使用以下方法通過 web3 v1 訪問 metamask 上的選定帳戶:

 //metamask method that refresh the active account in the web
   this.web3.currentProvider.publicConfigStore.on('update',async function(event){
     this.setState({
       account: event.selectedAddress.toLowerCase()
     },()=>{
       this.load();
     });
   }.bind(this));

一切正常,似乎發生了新的更新,並且不再可用。有什麼建議麼?

謝謝

使用accountsChanged事件:

 /**
  * Listening for MetaMask address changes.
  * @param  {Function} callback Resolve when address is changed
  */
  function onAccountsChanged(callback) {
    ethereum.on('accountsChanged', (accounts) => {
      // Time to reload your interface with accounts[0]
      console.log(accounts);

      callback(null, accounts[0]);
    });
  }

在帳戶上測試此更改:

this.web3.currentProvider.publicConfigStore.on('update',async function(event){
 this.setState({
   account: this.web3.currentProvider.selectedAddress.toLowerCase()
 },()=>{
   this.load();
 });
}.bind(this));

引用自:https://ethereum.stackexchange.com/questions/85037