1

I am trying to call a function on a smart contract using web3 and web3-eth-contract but I get the error Provider does not have a request or send method to use.

Here is the general code I am attempting:

const provider = new Web3.providers.HttpProvider(infuraEndpoint);
const web3 = new Web3(provider);
const contract = new Contract(tokenAbi.abi, tokenContractAddress);
contract.methods.someFun().call()
        .on('receipt', (res) => {.....

I am clearly missing something about how this works, but I can't figure out what it is. Does anyone have an example of making a call to a function on an ethereum contract from node?

3
  • 2
    While send returns an event emitter, call doesn't. Dec 24 '20 at 21:38
  • 1
    If someFun is a constant (pure or view) method, then you don't need to wait for a receipt event. If it is a non-constant method and you want to execute it in a transaction, then you need to use send or similar (sendTransaction, sendSignedTransaction, etc). Dec 24 '20 at 21:42
  • Ahh, this must be where my mistake is. It's a view method, so what is the proper way of calling it?
    – esastincy
    Dec 25 '20 at 1:04
1

I'll paste an example from web3 docs for you to take a look

myContract.methods.myMethod(123)
.call({from:'0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'})
.then(function(result){
    console.log(result)
});

I also think that you need to change new Contract to new web3.eth.Contract if you are using the current version of web3.

2
  • I get this error: Error: Provider does not have a request or send method to use.
    – esastincy
    Dec 25 '20 at 1:23
  • Yeah that was it, I was using the wrong Contract class. Thank You!
    – esastincy
    Dec 25 '20 at 1:28

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.