【问题标题】:Metamask stopping to inject web3.jsMetamask 停止注入 web3.js
【发布时间】:2020-03-29 01:14:14
【问题描述】:

我们知道,metamask 从 2020 年 1 月 13 日起将不再注入 web3.js。我们应该采取哪些方法来停止对 web3 的依赖?

此外,我们如何使用目前正在注入 web3.js 的现有 Metamask 对其进行测试。

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow。请避免在 cmets 中添加详细信息,希望对您的原始帖子进行编辑。

标签: blockchain ethereum web3js metamask decentralized-applications


【解决方案1】:

window.ethereum 仍将包含 Web3 提供程序。您可以使用它设置您选择的 web3 便利库,例如 web3.js 或 ethers.js。例如:

const Web3 = require('web3');
// web3 lib instance
const web3 = new Web3(window.ethereum);
// get all accounts
const accounts = await web3.eth.getAccounts();

【讨论】:

    【解决方案2】:
    import Web3 from "web3";
    
    let web3;
    const ethEnabled = async () => {
        if (window.ethereum) {      
          web3 = new Web3(window.ethereum);
          await window.ethereum.enable();
        }
        
      }
    ethEnabled();
    export default web3;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2021-08-23
      • 2018-05-04
      • 1970-01-01
      • 2022-06-27
      • 2020-05-29
      • 2021-12-14
      相关资源
      最近更新 更多