【问题标题】:web3.eth.getBlock is not working except in Firefoxweb3.eth.getBlock 除了在 Firefox 中之外不工作
【发布时间】:2022-10-05 06:15:04
【问题描述】:

我有一个奇怪的错误。此代码在除 Firefox 之外的所有浏览器中都返回错误

在codepen上试试 https://codepen.io/yoyoy13/pen/poVVxvN

console.log('getblock');
         web3.eth.getBlock().then((theBlock) => {
            console.log('theBlock',theBlock);
        }).catch((err) => {
            console.log('error theBlock',err);
        });

【问题讨论】:

标签: javascript blockchain ethereum web3js


【解决方案1】:

我认为那是因为您正在注入 window.ethereum

   web3 = new Web3(window.ethereum);

可能每个浏览器中提供程序的版本都不同。

尝试这个:

let web3;
if (window.ethereum) {
    provider = window.ethereum;
    try {
      await provider.request({ method: "eth_requestAccounts" });
    } catch {
      console.error("User denied account access");
    }
  } else if (window.web3) {
    provider = window.web3.currentProvider;
  } else if (!process.env.production) {
    // this is ganache cli
    provider = new Web3.providers.HttpProvider("http://localhost:7545");
  }

【讨论】:

    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2014-01-10
    • 2012-08-09
    • 1970-01-01
    • 2021-12-23
    相关资源
    最近更新 更多