0

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?

  • Try to add var currentProvider = new Web3.providers.HttpProvider('http://localhost:7545'); and change first line to var web3 = new Web3(currentProvider); – Roman Frolov Jan 13 '18 at 15:05
  • 'http://localhost:7545' be sure to change port if needed. – Roman Frolov Jan 13 '18 at 15:06
  • 1
    @RomanFrolov He's using MetaMask. – user19510 Jan 13 '18 at 17:52
  • @DavidMihal Did you found a solution for this? I have the same problem. – t_io Jan 22 '18 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 – David Mihal Jan 24 '18 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.

| improve this answer | |
0

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.

| improve this answer | |
  • 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. – David Mihal Jan 24 '18 at 4:51

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.