3

I'm writing a JS file to provide functionality to a smart contract. From what I can tell, MetaMask is injecting web3 into the webpage, but when I attempt to create a contract object, my browsers console (brave browser) is stating that web3.eth.Contract is not a constructor.

I've looked in the object provided by my browsers console, and I don't see the Contract constructor. Is this normal? Or do you think web3 may be incorrectly installed? I've hit a wal at this point.

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();
});
TylerH
  • 20,799
  • 66
  • 75
  • 101
jkean
  • 31
  • 1

2 Answers2

1

Try using Ethereum lib instead (web3-eth)

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

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

check first parameter is the abi node of the JSON not the abi metadata

new eth.Contract(contractAbi.abi, CONTRACT_ADDRESS);
TylerH
  • 20,799
  • 66
  • 75
  • 101
Tom
  • 11
  • 1
1

Works for me

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