繁体   English   中英

Web3 不将 web3.eth.Contract 视为构造函数

[英]Web3 not treating web3.eth.Contract as a contructor

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

我查看了浏览器控制台提供的 object,但没有看到 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();
});

适合我

const Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("infuralink"));
const ABI=[];
const Contract = new web3.eth.Contract(ABI,"contractaddress");

尝试改用以太坊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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM