即使我切换帐户,ethers.js也返回同一钱包地址

发布于 2025-01-22 09:22:04 字数 637 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

離人涙 2025-01-29 09:22:04

正确答案:

要获取当前帐户,获取0 eth_requestaccounts的地址:

let accounts = await provider.send("eth_requestAccounts", []);
let account = accounts[0];

自动更新它

provider.on('accountsChanged', function (accounts) {
    account = accounts[0];
});

import { ethers } from "ethers"

async function connect() {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
    let accounts = await provider.send("eth_requestAccounts", []);
    let account = accounts[0];
    provider.on('accountsChanged', function (accounts) {
        account = accounts[0];
        console.log(address); // Print new address
    });

    const signer = provider.getSigner();

    const address = await signer.getAddress();

    console.log(address);
}

旧的(不正确)答案:

我相信问题是您需要在切换帐户时重新构成签名者和提供商。

此外,如果我正确阅读了文档,那么在eth_requestaccounts之后,更好地实例化签名者是更好的练习。

Correct answer:

To get the current account, get the 0 address of eth_requestAccounts:

let accounts = await provider.send("eth_requestAccounts", []);
let account = accounts[0];

To update it automatically, listen to the accountsChanged event:

provider.on('accountsChanged', function (accounts) {
    account = accounts[0];
});

Code:

import { ethers } from "ethers"

async function connect() {
    const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
    let accounts = await provider.send("eth_requestAccounts", []);
    let account = accounts[0];
    provider.on('accountsChanged', function (accounts) {
        account = accounts[0];
        console.log(address); // Print new address
    });

    const signer = provider.getSigner();

    const address = await signer.getAddress();

    console.log(address);
}

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.

中性美 2025-01-29 09:22:04

我认为问题是,如果您的应用在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

无所的.畏惧 2025-01-29 09:22:04

好吧,在大量时间浪费之后,我了解到这仅在您使用提供商时才发生当时调用智能合约的函数。

然后,您必须使用签名而不是提供商。我不知道它背后的故事是什么,但是这是解决方案时,当您使用提供商作为仅读取的提供商时,他们可能会存储一些数据。但是使用签名者,它总是反映正确的地址。希望对某人有帮助并节省一些时间:)

如果有人知道这背后的确切原因,请告诉我吗?

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 of Provider. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文