即使我切换帐户,ethers.js也返回同一钱包地址
我正在使用 ethers.js 允许用户将其MetAmask钱包连接到我的应用程序。这是我拥有的代码:
import { ethers } from "ethers"
async function connect() {
const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
await provider.send("eth_requestAccounts", [])
const signer = provider.getSigner()
const address = await signer.getAddress()
// Always prints the address that I first connected with
console.log(address)
}
问题是,一旦我连接了一个metamask帐户,即使我切换到另一个metAmask帐户并尝试将其连接起来,我也总是会获得其钱包地址。
为什么那是这样,我应该如何解决?
I'm using Ethers.js to allow users to connect their Metamask wallets to my app. Here's the code that I have:
import { ethers } from "ethers"
async function connect() {
const provider = new ethers.providers.Web3Provider(window.ethereum, "any")
await provider.send("eth_requestAccounts", [])
const signer = provider.getSigner()
const address = await signer.getAddress()
// Always prints the address that I first connected with
console.log(address)
}
The issue is that once I have connected one of my Metamask accounts, then I always get its wallet address even if I switch to another Metamask account and try to connect it as well.
Why is that and how should I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正确答案:
要获取当前帐户,获取
0
eth_requestaccounts
的地址:自动更新它
要
旧的(不正确)答案:
我相信问题是您需要在切换帐户时重新构成签名者和提供商。
此外,如果我正确阅读了文档,那么在
eth_requestaccounts
之后,更好地实例化签名者是更好的练习。Correct answer:
To get the current account, get the
0
address ofeth_requestAccounts
:To update it automatically, listen to the
accountsChanged
event:Code:
Old (incorrect) answer:
I believe that the issue is that you need to re-instantiate both the signer and the provider when the account is switched.
In addition, if I read the docs correctly, it's better practice to instantiate the signer after
eth_requestAccounts
is successfully called.我认为问题是,如果您的应用在Localhost上:3000并且您的应用程序连接到
metAmask
,则该连接请求以某种方式缓存并始终获得相同的地址。如果关闭应用程序,请在Localhost:3001启动该应用程序,请手动连接到MetAmask(这意味着在您的应用程序窗口上,单击扩展名,您将看到连接按钮并手动连接),您将获得新连接的地址
I think the issue is, if your app is on localhost:3000 and your app is connected to the
metamask
, that connection request somehow cached and always gets the same address.If you shut down the app, start the app at localhost:3001, connect to the metamask manually (meaning that on your app window, click on extension and you will see connect button and manually connect) you will get the newly connected address
好吧,在大量时间浪费之后,我了解到这仅在您使用
提供商
时才发生当时调用智能合约的函数。然后,您必须使用
签名
而不是提供商
。我不知道它背后的故事是什么,但是这是解决方案时,当您使用提供商作为仅读取的提供商时,他们可能会存储一些数据。但是使用签名者,它总是反映正确的地址。希望对某人有帮助并节省一些时间:)如果有人知道这背后的确切原因,请告诉我吗?
Well After a lot of wastage of time I understood that this only happens when you are using
provider
But if you want to get the the address that you have changed in the metamask and the one that you are using to call a function of smart contract at that moment.Then you gotta use
Signer
instead ofProvider
. I don't know what's the story behind it but this is the solution maybe they kind of store some data when you use provider as its going to be only read only. But with the signer it always reflect the correct address. Hope that helps to someone and save some time :)Let me know if someone knows the exact reason behind this?