当用户切换 Phantom 钱包帐户时在浏览器上进行检测

Ser*_*onț 3 reactjs solana

因此,我有一个带有 nextjs 的反应应用程序,当用户使用 Phantom 钱包扩展并切换帐户时,我需要获得事件触发器。我在他们的文档中找不到任何相关内容:https://docs.phantom.app/

我想知道是否有人遇到这个问题。基本上我有 window.solana 对象,但它没有用户切换帐户时的触发器

Jac*_*ech 5

因此 Phantom 本身不会在其window.solana对象上公开任何帐户切换特定 API。

您可以采取一些技巧来找出帐户何时切换。

您可以连续轮询当前连接的帐户并将 publicKey 设置为某个变量。当publicKey改变时,你可以触发你的事件。

伪代码示例:

let currentKey = '';

poll(() => {
  if (/* wallet available and connected */) {
    await /* Action that updates publicKey */
    if (currentKey !== wallet.publicKey.toBase58()) {
      currentKey = wallet.publicKey.toBase58();
      this.publicKey = wallet.publicKey;
      this.emit('change')
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到我们目前关于钱包适配器的文章和 PR,讨论此流程