Allowing an RPC-Connection to the Forked Network
Now, we want to virtually fork the mainnet and make our virtual network available via RPC endpoint to apps like MetaMask or Remix IDE. For safety reasons, we again do this on the public Goerli testnet.
- Create a folder hardhatTest2
- Navigate to that folder
- Create a JavaScript project
- Open the file hardhat.config.js with an editor.
- Enter the following lines in the curly brackets of module.exports = { }:
- You can also pin a block if you like.
networks: {
hardhat:{
forking:{
url: "<url to archive node>"
}
}
}
Replace <url to archive node> with the url to your archive node. Use the testnet url!
Now, you can open MetaMaask.
Create a new network by clicking on the account symbol.
Select Add Network and enter the following details:
- Network Name: hardhat
- New RPC URL: http://127.0.0.1:8545
- Chain ID: 31337
- Currency Symbol: ETH_H
Click on Save.
Start hardhat with the following command.
npx hardhat node
This starts a node that provides an RPC endpoint.
You can now send Ether on your virtual network by MetaMask.
You can also use another web3 library like web3.js or ethers.js to communicate with the RPC endpoint.