2

I am trying to connect to my local hardhat node (which is mainnet fork) from my locally running web app. However, because I have the ethersjs provider set to http://localhost:8545, so it knows to talk locally, I get an error from Metamask:

MetaMask - RPC Error: Expected an array with at least one valid string HTTPS url 'rpcUrls'

Curious what the workaround is?

I am sure there is probably a way to run a local hardhat node locally over tls (?) but I feel like there is a better solution.

Thanks for your help.

Please note, I have (of course) setup MM with the local network and I and my provider code definition looks like:

provider = new ethers.providers.Web3Provider(web3ProviderFrom(config.defaultProvider), config.chainId);

3 Answers 3

2

Turns out my Metamaks network was setup incorrectly... I was missing the proper chainId, 31337, for the local mainnet fork.

Despite reviewing this related question multiple times, this answer tipped me off.

1

I'm not entirely following what the issue is here so I'm going to be a bit more verbose in the answer in hopes that it helps. I believe your issue is that you're setting the provider using ethers.providers.JsonRpcProvider(url) when instead you should be using the provider from Metamask. const provider = new ethers.providers.Web3Provider(window.ethereum, "any")

I'm assuming you already went into Metamask and added the localhost 8545 network to it. Then from the project repo you can run npx hardhat node. From then on you should be able to deploy your contract to the local node with something like npx hardhat run --network localhost scripts/deploy.js.

Once that's done you get the newly deployed contract address and use that to connect to your contract from the frontend. Lastly, make sure to switch the network in Metamask to localhost 8545.

1
  • Thanks for your response! I did indeed setup the local network within metamask. Also, I am using the Web3Provider like you suggested. const newProvider = new ethers.providers.Web3Provider(provider, this.config.chainId);. Will update my question.
    – rsmets
    Mar 1, 2022 at 3:53
0

I have entirely not understood the question. Is this what you are looking for? to connect etherjs to local block or node.

    var url = 'http://localhost:8545';
    var customProvider = new ethers.providers.JsonRpcProvider(url);

Now you can proceed with the above provider.

2
  • The answer does not address the the question which is a problem with connecting MetaMask to a local blockchain
    – sola24
    Jul 2, 2022 at 13:45
  • May be this might help. link
    – JAGADEESH
    Jul 2, 2022 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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