【问题标题】:Track Solana wallet change in Web在 Web 中跟踪 Solana 钱包变化
【发布时间】:2022-08-03 02:30:51
【问题描述】:

我正在使用 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)之后,我得到了我需要的所有信息。更换钱包后 - 我在我的应用程序中看不到任何更新。

问题是
我该如何处理?

    标签: websocket solana solana-web3js


    【解决方案1】:

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

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

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

    现在,您可以创建setInterval(例如)来检测这些更改。所以,如果isAccountChanged = true -> 你需要更新用户状态。如果它仍然是false -> 你可以等待。

    供参考:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-08
      • 2022-08-05
      • 2022-06-17
      • 2022-06-14
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 2022-08-11
      相关资源
      最近更新 更多