[Ethereum] hardhat mainnet forking and impersonating an account isnt working, help!

daiethers.jsforkshardhatmainnet

So I'm trying to fork the mainnet using hardhat, impersonate a Dai whale, and transfer its Dai to a hardhat wallet for testing a smart contract, but I'm running into problems.

My code looks like this:

hardhat.config network section:

hardhat: {
      forking: {
        url: process.env.MAINNET_URL || "",
      }
    },

(same as hardhat docs) I tried infura and alchemy urls, same result. I use npx hardhat node to fork the mainnet

I have a script called fundAccount thats supposed to impersonate an account and transfer its Dai balance to a hardhat wallet that looks like this: (I run it with node scripts/fundAccount.js)

const DaiABI = require("../abi/DaiABI.json")
const { ethers } = require("hardhat");
const hre = require("hardhat");

const daiAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"

async function main() {
  const accountToInpersonate = "0x6F6C07d80D0D433ca389D336e6D1feBEA2489264"
  const accountToFund = "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"

  await hre.network.provider.request({
    method: "hardhat_impersonateAccount",
    params: [accountToInpersonate],
  });
  const signer = await ethers.getSigner(accountToInpersonate)

  const daiContract = new ethers.Contract(daiAddress, DaiABI, signer)
  const daiBalance = await daiContract.balanceOf(accountToInpersonate)
  console.log("whale dai balance", daiBalance / 1e18)

  console.log("transfering to", accountToFund)

  
  await daiContract.connect(signer).transfer(accountToFund, daiBalance)
  const accountBalance = await daiContract.balanceOf(accountToFund)

  console.log("transfer complete")
  console.log("funded account balance", accountBalance / 1e18)

  const whaleBalanceAfter = await daiContract.balanceOf(accountToInpersonate)
  console.log("whale dai balance after", whaleBalanceAfter / 1e18)


}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

My console output looks like this:

enter image description here

so, it looks like it worked, but it doesn't. I know this because I connected the account to my metamask and I can see the 10000 ether (from hardhat) but no Dai shows up. I also tried removing the line that triggers the transfer in my script so it just reads balances and it looks like this:

enter image description here

another thing is I can run the script over and over and get the same result, meaning the whale balance is never emptied, right? Any ideas what I'm doing wrong?

Thanks!!

Best Answer

You're probably not connected to your local node fork.

In hardhat tests, unless you explicitly specify --network localhost, it won't connect to your local fork and will launch a clean test env each run.

Make sure you follow these steps -

  1. Configure hardhat config to fork from a live node URL.
  2. Run npx hardhat node from a shell terminal.
  3. Now to run your hardhat tests npx hardhat test --network localhost.
  4. If you want to use Metamask as well, make sure to connect it to RPC URL http://localhost:8545
Related Topic