11

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?

goonerify
  • 1,668
  • 25
  • 27

4 Answers4

16

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}

goonerify
  • 1,668
  • 25
  • 27
1

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'
      }
    ];
  }
}
1

You can use @nomicfoundation/hardhat-networks-helpers package.

@nomicfoundation/hardhat-network-helpers provides a convenient JavaScript interface to the JSON-RPC functionality of Hardhat Network.

You don't even need to store your private key to fund, however you will need one to deploy or make transactions:
await helpers.setBalance(address, balance);

Link to docs

woojiq
  • 71
  • 1
  • 9
0

Config your hardhat.config.js file

Manually add the chaindId:1337 to avoid the conflict with MetaMask

e.g

require("@nomicfoundation/hardhat-toolbox");

// The next line is part of the sample project, you don't need it in your
// project. It imports a Hardhat task definition, that can be used for
// testing the frontend.
require("./tasks/faucet");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.9",
  networks: {
    hardhat: {
      chainId: 1337 // We set 1337 to make interacting with MetaMask simpler
    }
  }
};

Boot up local development blockchain

$ npx hardhat node

Connect development blockchain accounts to MetaMask

Copy private key of the addresses and import to MetaMask Connect your MetaMask to hardhat blockchain, network 127.0.0.1:8545.

If you have not added hardhat to the list of networks on your MetaMask, open up a browser, click the fox icon, then click the top center drop down button that lists all the available networks then click add networks.

A form should pop up. For the "Network Name" field enter "Hardhat". For the "New RPC URL" field enter "http://127.0.0.1:8545". For the chain ID enter "1337". Then click save.

Finally

Once you have your test account on the hardhat node you will have 10000 ETH to deploy your contracts.

goonerify
  • 1,668
  • 25
  • 27