gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:37:30 25 4
gpt4 key购买 nike

我正在编写一个 JS 文件来为智能合约提供功能。据我所知,MetaMask 正在将 web3 注入(inject)到网页中,但是当我尝试创建合约对象时,我的浏览器控制台(勇敢的浏览器)指出 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();
});

最佳答案

适合我

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

关于javascript - Web3 不将 web3.eth.Contract 视为构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57579680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com