【问题标题】:Web3 not treating web3.eth.Contract as a contructorWeb3 不将 web3.eth.Contract 视为构造函数
【发布时间】:2019-08-20 18:33:40
【问题描述】:

我正在编写一个 JS 文件来为智能合约提供功能。据我所知,MetaMask 正在将 web3 注入到网页中,但是当我尝试创建合约对象时,我的浏览器控制台(勇敢的浏览器)显示 web3.eth.Contract 不是构造函数。

我查看了浏览器控制台提供的对象,但没有看到 Contract 构造函数。这是正常的吗?还是您认为 web3 可能安装不正确?在这一点上我已经打了一个沃尔玛。

var blockContractJSON = $.getJSON("../build/contracts/Blocks.json", function(data) {
  return data;
});

console.log(blockContractJSON)

// console.log(blocksContract)

var blocksContract;
var currentUser;
var web3js;
console.log(web3js);
// console.log(blockContractJSON);

// defines contract address and initializes contract functions
var startApp = function() {
  var contractAddress = "0x2520127E14E8A14C67Ee2B561714ADae53D48110";
  console.log('got the contract'); <- web3 not passing to this line?
  blocksContract = new web3js.eth.Contract(blockContractJSON, contractAddress);
  // console.log(blocksContract);
  var checkAccounts = setInterval(function() {
    if (web3js.eth.accounts[0] !== currentUser) {
      currentUser = web3js.eth.accounts[0];
    }
  }, 100)();
};

// adds in web3
  window.addEventListener('load', function() {
    console.log('item loaded from page is')
    console.log()
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    console.log('using metamask');
    console.log(web3)
    // Use Mist/MetaMask's provider
    web3js = new Web3(web3.currentProvider);
    console.log(web3js)
  } else {
    alert('install metamask')
  }
  startApp();
});

【问题讨论】:

    标签: javascript web3 truffle metamask


    【解决方案1】:

    尝试使用 Ethereum lib (web3-eth)

    [https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html][1]

    var eth = new Eth('http://localhost:8545');
    

    检查第一个参数是json的abi节点而不是abi元数据

    new eth.Contract(contractAbi.abi, CONTRACT_ADDRESS);
    

    GL

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-16
      • 2018-06-18
      • 2022-06-28
      • 2019-12-12
      • 2019-07-03
      • 2019-06-23
      • 2022-07-14
      • 2021-07-28
      相关资源
      最近更新 更多