Is it possible to impersonate an account using ether js only? I don't want to use Hardhat. Though I have forked mainnet using foundry. Now wants to transfer eth of let's say vitalik to my own account locally 😁. Is not it possible just use ether js only

  • 1
    I think foundry's tooling provides you with all you need. But I'm not super familiar with it. Commented May 1, 2023 at 18:55

1 Answer 1

  • here is the solution to impersonate account when we forked the mainnet using foundry. then we can use ether js to transfer eth from any whale account. why is it important because we always have problem to get testnet eth. even goerli testnet eth are not free. so its better to fork mainnet locally and transfer eth from whale address to your address*
    import { ethers } from "ethers";
    let url = "http://localhost:8545";
    const provider = new ethers.providers.JsonRpcProvider(url);
    async function impersonateAccount(address, targetAddress, amount) {
      // Impersonate the account
      await provider.send("anvil_impersonateAccount", [address]);
      // Create a signer for the impersonated account
      const signer = provider.getSigner(address);
      // Send a transaction from the impersonated account
      const tx = await signer.sendTransaction({
        to: targetAddress,
        value: ethers.utils.parseEther(amount),
      await tx.wait();
      console.log(`Sent ${amount} ETH from ${address} to ${targetAddress}`);
    const addressToImpersonate = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045";
    const targetAddress = "0x840E0f2e07A52F335e956947fd2AC0f60496f891";
    const amount = "1000";
    // impersonateAccount(addressToImpersonate, targetAddress, amount);
        await provider.getBalance("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")

here is the code. even i have transferred eth from Vitalik account to mine. but wants to understand what is the params "anvil_impersonateAccount".

  • Please edit your answer so the code is more legible, using the right formatting. Thanks. Commented May 2, 2023 at 1:29
  • 1
    done please check Commented May 3, 2023 at 3:10

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.