Run a development network
You can run a personal Ethereum development network using Hardhat, which allows you to develop a dapp in a secure test environment.
When using a local development blockchain such as Hardhat Network or anvil, your node must calculate gas to make transactions on MetaMask.
Connect to Hardhat Network
Follow these steps to connect MetaMask to Hardhat Network.
-
Create a new MetaMask seed phrase specifically for development.
importantYour seed phrase controls all your accounts, so we recommend keeping at least one seed phrase for development, separate from any used to store real value. You can manage multiple seed phrases by using multiple browser profiles, each with its own MetaMask installation.
-
In your
hardhat.config.js
file, specify anetworks
configuration with ahardhat
network. In thisnetworks.hardhat
configuration:-
Specify your MetaMask seed phrase in the
accounts.mnemonic
field.tipAlternatively, to prevent committing your seed phrase, we recommend adding your seed phrase to a
.env
file and using theprocess.env
global variable inhardhat.config.js
. -
Specify the chain ID
1337
in thechainId
field.
For example:
hardhat.config.jsmodule.exports = {
networks: {
hardhat: {
accounts: {
mnemonic: process.env.SEED_PHRASE,
},
chainId: 1337,
},
},
};Hardhat automatically gives each of your first 20 accounts 10000 test ether (you can modify these numbers in the
accounts
configuration), which makes it easy to start development. -
-
Run
npx hardhat node
to run Hardhat Network and expose a JSON-RPC interface. -
You can now connect MetaMask to your Hardhat Network RPC URL,
http://127.0.0.1:8545/
. In the MetaMask extension:-
In the upper left corner, select the network you're currently connected to.
-
Select Add network.
-
Select Add a network manually.
-
Enter your Hardhat Network RPC URL,
http://127.0.0.1:8545/
(orhttp://localhost:8545
). -
Enter your Hardhat Network chain ID,
1337
(or0x539
in hexadecimal format).
tipAlternatively, you can add Hardhat Network to MetaMask using
wallet_addEthereumChain
in the interactive API playground. -
Reset your local nonce calculation
If you restart your development network, you can accidentally confuse MetaMask because it calculates the next nonce based on both the network state and the known sent transactions.
To clear MetaMask's transaction queue and reset its nonce calculation, go to Settings > Advanced and select Reset account.
Next steps
Once you have your development environment set up and development network running, you can connect your dapp to MetaMask by setting up MetaMask SDK, detecting MetaMask, detecting a user's network, and accessing a user's accounts.
For an end-to-end example, you can also follow the Create a simple React dapp tutorial.