2

I'm building a Dapp using MetaMask. However, I'm getting an error when I try to call a method on my contract.

Here's the simplified code:

var web3 = new Web3(web3.currentProvider);
var contract = new web3.eth.Contract(contractData.abi);
contract.options.address = address;
contract.methods.getName().call().then(name => console.log(name))

This throws the following error:

Unhandled rejection Error: Provider not set or invalid

Digging through the debugger, it seems that the RequestManager doesn't have a provider set. However, I can't figure out how that provider should be set?

6
  • 1
    Try to add var currentProvider = new Web3.providers.HttpProvider('http://localhost:7545'); and change first line to var web3 = new Web3(currentProvider); Jan 13, 2018 at 15:05
  • 'http://localhost:7545' be sure to change port if needed. Jan 13, 2018 at 15:06
  • 1
    @RomanFrolov He's using MetaMask.
    – user19510
    Jan 13, 2018 at 17:52
  • @DavidMihal Did you found a solution for this? I have the same problem.
    – t_io
    Jan 22, 2018 at 14:57
  • 1
    @t_io Not really, but I've worked around the problem by calling contract.setProvider(web3.currentProvider); on each contract I create Jan 24, 2018 at 4:51

2 Answers 2

1

Have you set MetaMask to whatever test network you are using? If testrpc, select in Metamask that you want to connect to LocalHost 8545, and if using Ganache, you can create a custom RPC to connect to LocalHost 7545. From there, use the seed phrase given from testrpc or Ganache to "restore"/connect. This should pull in your accounts, and let you connect properly to a provider.

1
  • MetaMask seems to be working fine, I've been able to work around the problem by calling contract.setProvider(web3.currentProvider); on each contract I create. The problem seems to be either a bug or confusing API with web3. Jan 24, 2018 at 4:51
0

It looks like you haven't set the from address. Check out the contract properties. With MetaMask you can get the current selected address with var account = web3.eth.accounts[0];

If you add something like contract.options.from = account; it should work.

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.