乐闻世界logo
搜索文章和话题

How do I add ether to my localhost Metamask wallet with Hardhat?

1个答案

1

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:

bash
npm install --save-dev hardhat

Next, initialize a new Hardhat project:

bash
npx 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:

javascript
module.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:

bash
npx 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

  1. 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.

  2. Select the 'Import Account' option in MetaMask.

  3. Copy the private key of one of the accounts from the Hardhat terminal output.

  4. 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.

2024年7月24日 09:54 回复

你的答案