Mainnet Forking and Crypto Wallet Impersonation:

venkatesh_rajendran

Venkatesh R

Posted on October 22, 2022

Mainnet Forking and Crypto Wallet Impersonation:

Mainnet Forking and Wallet Impersonation:

Do you want to fork the Mainnet Blockchain into your local development network? Or Do you want to simulate mainnet blockchain and build your own Defi projects? Or Do you want to become a Senior Web3 Developer by learning complex blockchain concepts?

Well, good news! you're in the right place.

Mainnet Forking:

Mainnet Forking means we simulate mainnet blockchain to have the same state as mainnet, but it will work as a local development network. You can simulate the Ethereum blockchain into your local network and build your next Defi project.

Wallet Impersonation:

Wallet Impersonation is a process of unlocking an access to any wallet which exists in the blockchain network by wallet address. This lets you send transactions from that account even if you don't have access to its private key.

Benefits:

Benefits of forking the mainnet and wallet impersonation are

  1. Write real-time unit tests for your smart contracts
  2. Build your own Defi smart contracts based on other complex protocols such as Uniswap, Compound, Curve, etc.
  3. Learn and get comfortable with your own cryptocurrency arbitrage strategies by mocking the Mainnet protocols
  4. Test your new protocols against your previously deployed smart contracts.

Getting Started:

Before starting you need to setup your Hardhat Smart contract development environment. Please refer to this hardhat docs

How to fork the blockchain network:

To fork the mainnet blockchain you need a RPC node provider.

You can use these RPC providers such as

  1. Infura - Follow this docs to get a RPC endpoint forking the Mainnet
  2. Alchemy - Follow this docs to get a RPC endpoint forking the Mainnet

After completing the RPC endpoint setup process, you need to goto your Hardhat development environment and check for the Hardhat config file.

Inside the hardhat.config.js file, paste your RPC endpoint to setup the mainnet forking.

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

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.7.6",
  networks: {
    hardhat: {
      forking: {
        url: "https://eth-mainnet.g.alchemy.com/v2/<API_KEY>"
      }
    }
  }
};
Enter fullscreen mode Exit fullscreen mode

Tada! From now whenever you start your hardhat local network / run your unit tests, hardhat environement will automatically fork the mainnet into your local network.

Wallet Impersonation:

We can use the hardhat built-in features to impersonate any wallet account in the mainnet blockchain using the address.

Use this code snippet to impersonate any wallet

await network.provider.request({
   method: "hardhat_impersonateAccount",
   params: [WALLET_ADDRESS],
});
Enter fullscreen mode Exit fullscreen mode

Example:

Do you want to impersonate and gain access to one of the top bitcoin holder's wallet and transfer all the bitcoin to your own wallet in your local network?

Steps:

  • Go to etherscan and pick your favourite account which you want to impersonate. Let's say we impersonate the bitcoin holder's wallet,
  • Goto WBTC contract in etherscan, navigate to holders tab and pick one of the account from there

WBTC Address: 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599
WBTC Whale Address: 0x28c6c06298d514db089934071355e5743bf21d60

Since bitcoin is a cryptocurrency of different blockchain, so we use a Wrapped BTC (ERC20 token) to represent the bitcoin in Ethereum blockchain

  • Use the impersonate built in feature provided by hardhat to unlock the account.
  • Get the access to the whale's wallet account in your local network by getting the signer.
await network.provider.request({
   method: "hardhat_impersonateAccount",
   params: [WBTC_WHALE],
});

const whaleAccount = await ethers.getSigner(WBTC_WHALE);
Enter fullscreen mode Exit fullscreen mode
  • You can transfer the WBTC from whale's wallet account to your account with the help of ERC20 utility methods
const whaleWalletBalance = await wbtc.balanceOf(whaleAccount.address);

await wbtc.connect(whaleAccount).transfer(yourWallet.address, whaleWalletBalance);

const yourWalletBalance = await wbtc.balanceOf(yourWallet.address);
Enter fullscreen mode Exit fullscreen mode

Full Sample Code Snippet:

const { ethers, network } = require("hardhat");

const WBTC_WHALE = "0x28c6c06298d514db089934071355e5743bf21d60";
const WBTC_TOKEN = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599";


describe("Wallet Impersonation code snippet", () => {
   let wbtc;
   let accounts;

   before(async ()=> {
      accounts = await ethers.getSigners();
      wbtc = await ethers.getContractAt('IERC20', WBTC_TOKEN);
   })  

   it("Impersonate any wallet", async () => {
      const userAccount = accounts[0];

      await network.provider.request({
         method: "hardhat_impersonateAccount",
         params: [WBTC_WHALE],
      });

      const whaleAccount = await ethers.getSigner(WBTC_WHALE);

      const whaleTokenBalance = await wbtc.balanceOf(whaleAccount.address);
      await wbtc.connect(whaleAccount).transfer(userAccount.address, whaleTokenBalance);

      const userTokenBalance = await wbtc.balanceOf(userAccount.address);
        console.log("User Token Balance", userTokenBalance);
   })
}) 
Enter fullscreen mode Exit fullscreen mode

Hope you guys have learnt something new today, do follow me to get more exiting contents on #blockchain #Web3 #SmartContracts #Solidity #Rust #Solana

You can reach out to me via LinkedIn, Github

💖 💪 🙅 🚩
venkatesh_rajendran
Venkatesh R

Posted on October 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related