When developing Ethereum applications with Hardhat, you typically need to have ETH in your local test environment for transaction testing. Below are the steps to add ETH to your localhost MetaMask wallet:
Step 1: Installing and Configuring Hardhat
First, ensure that you have installed Hardhat in your project. If not, install it using the following command:
bashnpm install --save-dev hardhat
Next, initialize a new Hardhat project:
bashnpx hardhat
Follow the prompts to complete the configuration and select a basic project structure.
Step 2: Configuring Hardhat Network
Locate the hardhat.config.js file in the root directory of your Hardhat project and ensure it is configured for the local network. For example:
javascriptmodule.exports = { solidity: "0.8.4", networks: { localhost: { url: "http://127.0.0.1:8545" } } };
Step 3: Running Hardhat Network
Start the Hardhat local network using the following command:
bashnpx hardhat node
This will launch a local Ethereum network, typically displaying several accounts and their associated private keys. These accounts are pre-funded with a substantial amount of ETH.
Step 4: Adding Accounts to MetaMask
-
Open MetaMask and ensure you have selected the 'Localhost 8545' network or manually add a new network with the RPC URL
http://127.0.0.1:8545. -
Select the 'Import Account' option in MetaMask.
-
Copy the private key of one of the accounts from the Hardhat terminal output.
-
Paste this private key into MetaMask and import it.
Step 5: Verifying Balance
After importing the account, you should see that the account has the pre-allocated ETH in MetaMask.
Example
For example, after launching npx hardhat node, the terminal displays account information such as:
- Account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
- Private Key: 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784a2e8a5223ee
- Balance: 10000 ETH
Following these steps, import this account's private key into MetaMask, and you can use the ETH for development and testing within the 'Localhost 8545' network.
These steps will help you effectively use Hardhat and MetaMask for local development and testing.