2

I am attempting to call a method from a smart contract from my chrome console. My dApps index.html file is deployed locally at http://127.0.0.1:5500.

I set up web3 and instantiate a smart contract with the following code:

window.addEventListener('load', async () => {
    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
        console.log('metamask success');
        await window.ethereum.enable();
        web3.eth.defaultAccount = web3.eth.accounts.givenProvider.selectedAddress;

    } else { // I removed this code for brevity


abi = [ { constant: false, ... // I removed the full abi for conciseness

MyContract = web3.eth.contract(abi);

contractInstance = MyContract.at("0xd01e06f2e70bea9ae7640367bdf0b24ac5f35ed1");

I can successfully make the following transaction from my console in Google Chrome:

web3.eth.sendTransaction({value: web3.utils.toWei('.1'), to: "0x493b6DEb52B40A517Fa64f580B5bfd325537764E"}, (e,r) =>{console.log(e,r)})

But when I try an make the following transaction:

contractInstance.notarize(item, (e,r) => {console.log(e,r)})

I get the following error message in my Chrome console:

Uncaught TypeError: Cannot read property 'match' of undefined

Any ideas on what is going on here?

1

Methods of a contract can be accessed by methods method of a contract instance. And the transaction can be either called by call() method or sent by send() method.

So, you should use smth like that:

contractInstance.methods.notarize(item).send((e,r) => {console.log(e,r)})

See more info in the web3js documentation.

0

What is the ABI for your contract? That is, how many arguments does the notarize method take and of what type?

Usually a call to a method in web3 takes arguments like methodName(arg1, arg2, ..., argn, { from: '0xblah', gas: '0xblah', ... }, callbackFunc)

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.