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

I've connected metamask to a node created with hardhat. I can connect to this node on http://localhost:8545 network in metamask after setting the chain id to match the hardhat network chain id (31337)

How can I send ether to the accounts/addresses on the localhost network so that these accounts have enough ether to deploy a contract?



Solution 1:[1]

You don't exactly add ether to your localhost hardhat wallet as there's no localhost faucet that can send ether to your account. What you can do is connect to the pre-funded accounts that are created automatically by Hardhat with the following steps:

  1. Run the Hardhat Network in a standalone fashion using npx hardhat node --show-accounts to print the pre-funded accounts that are created automatically by Hardhat to std.out, along with their corresponding private keys.

  2. In metamask, connect to this node on http://localhost:8545 network after setting the chain id to match the hardhat network chain id (31337).

  3. In metamask, select the option to "Import Account" and paste the private keys of one of those accounts from the local hardhat node - to connect metamask to that account in order to view the account balance etc.

  4. In your hardhat config file, include the private key(s) for one or more of the pre-funded accounts to the account property of your localhost network. i.e

    localhost: {
      chainId: 31337, // Chain ID should match the hardhat network's chainid
      accounts: [`${PRE_FUNDED_PRIVATE_KEY_1}`, `${PRE_FUNDED_PRIVATE_KEY_2}`, `${OTHER_PRIVATE_KEY}`],
    }

You can then access these accounts in your deployment scripts. For instance, to send contract from ${PRE_FUNDED_PRIVATE_KEY_1} to ${OTHER_PRIVATE_KEY}

Solution 2:[2]

You can also use your own dev account like this. (Be careful with your private key )

https://hardhat.org/hardhat-network/reference/#accounts

networks: {
  hardhat: {
    accounts: [
      {
        privateKey: 'your-dev-account-private-key',
        balance: '10000000000000000000000'
      }
    ];
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Takeshi Kriang