gpt4 book ai didi

reactjs - 当用户切换 Phantom 钱包账户时在浏览器上检测

转载 作者:行者123 更新时间:2023-12-05 01:56:00 27 4
gpt4 key购买 nike

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

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

最佳答案

因此 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')
}
}
})

您可以找到我们目前在钱包适配器上的一篇文章和 PR 来检查这个流程 here

关于reactjs - 当用户切换 Phantom 钱包账户时在浏览器上检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69973752/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com