websocketsolanasolana-web3js

Track Solana wallet change in Web


I'm building a Web3 app while using Solana.
I'm using @solana/wallet-adapter for wallet connection

Code:

const Wallet = ({ children }) => {
// The network can be set to 'devnet', 'testnet', or 'mainnet-beta'.
const network = WalletAdapterNetwork.Devnet;

// You can also provide a custom RPC endpoint.
const endpoint = useMemo(() => clusterApiUrl(network), [network]);

// @solana/wallet-adapter-wallets includes all the adapters but supports tree shaking and lazy loading --
// Only the wallets you configure here will be compiled into your application and only the dependencies
// of wallets that your users connect to will be loaded.
const wallets = useMemo(
() => [
   new PhantomWalletAdapter(),
   new SlopeWalletAdapter(),
   new SolflareWalletAdapter(),
   new TorusWalletAdapter(),
   new LedgerWalletAdapter(),
   new SolletWalletAdapter({ network }),
   new SolletExtensionWalletAdapter({ network }),
],[network]);

return (
  <ConnectionProvider endpoint={endpoint}>
    <WalletProvider wallets={wallets} autoConnect>
      <WalletModalProvider>
        <WalletMultiButton />
        <WalletDisconnectButton />
        {children}
      </WalletModalProvider>
    </WalletProvider>
  </ConnectionProvider>
);
};

It's a basic components. Same as a presented in @solana/wallet-adapter docs

The problem:
After connecting some wallet manager(let's say Phantom, for instance), I'm getting all the information I need. But after changing wallet -- I don't see any updates in my app.

The question is
How can I handle this?


Solution

  • After a couple of days of research, I came to the conclusion that this is an API bug.

    I found a way that allows you to find out if the account has changed or not. It can be used if it is critical for you:

    const isAccountChanged = window.solana.publicKey.toBase58() !== `${your_current_public_key}`;
    
    if (isAccountChanged) {
      // do some updates
    }
    

    For now, you can create setInterval(for instance) to detect these changes. So, if isAccountChanged = true -> you need to update the users state. If it's still false -> you can wait.

    fyi: