2

last time, in truffle with ganache-cli, I fork the mainnet with the following command and tutorial...

ganache-cli -f https://mainnet.infura.io/v3/my_infura_project_id -u 0x742d35Cc6634C0532925a3b844Bc454e4438f44e 

0x742d35Cc6634C0532925a3b844Bc454e4438f44e, was the account from where I was borrowing the test ethers to my metamask localhost wallet. After typing the above command, I run a script that has the following code...

const Web3 = require('web3'); const web3 = new Web3('http://127.0.0.1:8545');

const myAccount = "0x6d5e2C47c58B5DF79164B0E618f74BA542E5aEd3";
const ethBorrowAccount = "0x742d35Cc6634C0532925a3b844Bc454e4438f44e";
 
web3.eth.sendTransaction({from: ethBorrowAccount, to: myAccount, value: web3.utils.toWei('100', 'ether')}, (err, hash) => {
    console.log(hash);
});

by doing this I was able to get 100 ethers in my metamask wallet. Like below...

enter image description here

My question is, how can I do the same with Hardhat???

2 Answers 2

2

I think the easiest way is to add your account to Hardhat config file:

In hardhat.config.js file:

module.exports = {
  networks: {
    hardhat: {
      accounts: [
        {
          privateKey: "PRIVATE_KEY_OF_YOUR_METAMASK_ACCOUNT",
          balance: "100000000000000000000"
        }
      ]
    }
  }
}

To run hardhat as a node and see the amount in Metamask:

npx hardhat node
0

In addition to setting the accounts and balance in the hardhat config you can also transfer just as you did with web3.js. Hardhat uses by default ethers.js and the method is very similar.

Here is an example how you can easily transfer ethers with hardhat/ethers:

    const recipient = '0xd3DBd06FAc1548325c2F33B6e2696C8972876D6B';
    const wallet = new ethers.Wallet(process.env.HARDHAT_PRIVATE_KEY, ethers.provider);
    const txData = {
        from: wallet.address,
        to: recipient,
        value: ethers.utils.parseEther("10"),
    };
    const tx = await wallet.sendTransaction(txData);
    const balance = await ethers.provider.getBalance(recipient);
    console.log(balance);

console output

% npx hardhat run scripts/sendTx.js
BigNumber { value: "10000000000000000000" }
1
  • You aren't answering the original question. It is useful, but not a valid answer.
    – Ismael
    Nov 20, 2022 at 3:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.