【问题标题】:How to get a list of accounts from the new MetaMask ethereum API?如何从新的 MetaMask 以太坊 API 中获取账户列表?
【发布时间】:2021-11-15 16:01:42
【问题描述】:

MetaMask 最近更改了他们注入 API 的方式,不再公开 window.web3 对象。

MetaMask 不再注入 web3。详情见:https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3

Uncaught TypeError: web3.eth is undefined

如何从新的 MetaMask ethereum API 获取账户列表?

【问题讨论】:

    标签: javascript ethereum web3 metamask


    【解决方案1】:

    您可以使用 MetaMask 或其他钱包注入的window.ethereum 对象。

    if (typeof window.ethereum !== 'undefined') {
      // connects to MetaMask
      const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
    } else {
      // tell the user to install an `ethereum` provider extension
    }
    

    这使用新的 API 在提供者上调用异步以太坊请求。它接受大多数以太坊 API,并且在实现中非常通用。

    【讨论】:

      猜你喜欢
      • 2019-03-29
      • 1970-01-01
      • 2015-11-25
      • 2021-12-16
      • 2022-01-19
      • 2021-11-15
      • 2022-11-16
      • 2022-08-16
      • 2020-03-31
      相关资源
      最近更新 更多