【发布时间】: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