0

I'm currently struggling to interact with my smart contract, I don't if it comes from the way I call the function or the way I connect to my smart contract. Here is the code

const contractAddress = '0xf85fe12e1eb30844422e95252b68bb1fbbbe7fc6';

const Web3 = require('web3');


window.addEventListener('load', async function () {
    if (typeof web3 !== 'undefined') {
        console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
        window.web3 = new Web3(web3.currentProvider);

        await ethereum.enable();
        console.log('address: ' + web3.eth.accounts[0])

        web3.eth.getBalance(web3.eth.accounts[0], (err, wei) => {

          console.log('le balance est ' + web3.fromWei(wei, 'ether'))
        });

        var contract = new web3.eth.contract(ABI).at(contractAddress);//, contractAddress);
        //console.log(contract);
        console.log(await contract.isOpen.call());
        // console.log(await contract.methods.isOpen.call());
        //console.log(await contract.methods.isOpen());



    } 
    else {
        document.getElementById('output').innerHtml = 'Please download and install Metamask: <a href="https://metamask.io/">https://metamask.io/</a>'
    }
})

here, the error in the browser

index.js:482 Uncaught (in promise) TypeError: Cannot read property 'isOpen' of undefined

at index.js:482

I have no problem with it on server side with nodejs and infura, but on client side using metamask, web3 functions looks like different, no?

Thanks for you helps

  • 1
    Assuming that you're using web3.js v1.x, change new web3.eth.contract to new web3.eth.Contract. – goodvibration Apr 24 at 5:44
  • I installed the 1.x with node js, if I look at in the browser's console with console,log(web3), it says that I'm using the version 0.20.4. However, I install the 6.3.2 metamask version, which is the last I guess. What's wrong? How can I update this version to 1.0? – champi Apr 24 at 6:12
  • Obviously, if I try with new web3.eth.Contract it tells me the constuctor doesn't exist. – champi Apr 24 at 6:13
  • With web3.js v0.x, just remove the new. – goodvibration Apr 24 at 6:52
  • And of course, I'm not sure what your ABI is, but it should be an array of objects (not a string, in case you have it this way). – goodvibration Apr 24 at 6:54
0

using version 0.20.x with metamask.

it worked by doing

//create a contract instance
contract = window.web3.eth.contract(ABI).at(contractAddress);
//call function isOpen() in the contract
contract.isOpen((err,res) =>{ console.log(res)});

Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.