6

I used the example from the Hardhat docs to impersonate an account on a forked network:

await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: ["0x...."]} )

However, I get "Error: Returned error: unknown account". How should this be done correctly? Thanks

3 Answers 3

8

try

import { network, ethers } from "hardhat";
await network.provider.request({
      method: "hardhat_impersonateAccount",
      params: ["0x..........................."],
    });

if you use hardhat-ethers

 const signer = await ethers.provider.getSigner(
      "0x..........................."
    );
7

Update Oct 2022

Impersonating Accounts with Hardhat has a new, simplified API. According to their new docs, you can impersonate an account using the getImpersonatedSigner method

const impersonatedSigner = await ethers.getImpersonatedSigner("0x1234567890123456789012345678901234567890");
await impersonatedSigner.sendTransaction(...);
0

viem solution 2024

Import

import hre from "hardhat";
import { impersonateAccount } from "@nomicfoundation/hardhat-network-helpers"

impersonate

const whaleWalletAddress = '0xbccA1f5d4dB5FB26310c38b99189C7d43586da46'
await impersonateAccount(whaleWalletAddress)
const [myUserBeingWhale] = await hre.viem.getWalletClients({account: whaleWalletAddress})

Should also works for ethers (untested)

const myUserSignerBeingWhale = await hre.ethers.getSigner(whaleWalletAddress)

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.