在 Web 中跟踪 Solana 钱包的变化

Track Solana wallet change in Web

我正在使用 Solana 构建 Web3 应用程序。
我正在使用 @solana/wallet-adapter 进行钱包连接

代码:

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

这是一个基本组件。与 @solana/wallet-adapter docs

中的相同

问题:
在连接了一些钱包管理器(例如 Phantom)之后,我得到了我需要的所有信息。 但是 更换钱包后 -- 我在我的应用程序中没有看到任何更新。

问题是
我该如何处理?

经过几天的研究,我得出的结论是这是一个 API 错误

我找到了一种方法,可以让您了解帐户是否已更改。如果对您很重要,可以使用它:

const isAccountChanged = window.solana.publicKey.toBase58() !== `${your_current_public_key}`;

if (isAccountChanged) {
  // do some updates
}

目前,您可以创建 setInterval(例如)来检测这些变化。所以,如果 isAccountChanged = true -> 你需要更新用户状态。如果还是 false -> 你可以等一下。

仅供参考: