---
# System prepended metadata

title: 'Hardhat Guide: How to Interact With Aave Using a Forked Mainnet'

---

# Hardhat Guide: How to Interact With Aave Using a Forked Mainnet

In this guide we will use Hardhat to fork the Ethereum main network. We will then use the forked version to interact with Aave smart contracts to:

- deposit DAI into the AAVE mainnet lending pool (on our forked version provided to us by Hardhat)
- actually test to make sure that interest accrues successfully

This guide will cover:

- how to impersonate an account on the main network
- how to use Hardhat helper methods for testing on a forked network
 
## Guide Requirements

- **[Hardhat](https://hardhat.org/)**: Hardhat is an Ethereum development platform that provides all the tools needed to build, debug and deploy smart contracts.
- **[Alchemy](https://www.alchemy.com/)**: Alchemy is a blockchain development platform from which we will use some APIs to help query the Ethereum blockchain.

## Step 1: Clone and Set Up the AAVE-DAI Escrow Repo

1. In a directory of your chocie, run `git clone git@github.com:ChainShot/AAVEDaiEscrow.git`
2. `cd` into the cloned repo and run `npm init -y`
3. Run `npm install`
4. Run `npm install dotenv hardhat chai ethers`
5. Run `touch .env`
6. In your `.env` file, copy-paste an Alchemy **mainnet** key as a variable called `FORKING_URL`
7. Open the `hardhat.config.js` file and copy-paste the following:

```javascript
require("@nomiclabs/hardhat-waffle");
require('dotenv').config();

module.exports = {
  solidity: "0.7.5",
  networks: {
    hardhat: {
      forking: {
        url: process.env.FORKING_URL,
        blockNumber: 11395144
      }
    }
  },
  paths: {
    artifacts: "./app/artifacts",
  }
};
```

> Notice the `FORKING_URL` is used in the `hardhat.config.js` file!

## Step 2: Create Test: Impersonate Account to Acquire DAI

Since we are looking to test that our `Escrow.sol` contract is able to deposit DAI into the AAVE lending pool, we need some DAI!

Let's use the `hardhat_impersonateAccount` helper function to use an account that has DAI in our forked version of the blockchain.

We are working with a blockchain forked at block #11395144 (check the `hardhat.config.js` to see this!). So we need any EOA that had some DAI during that block.

Here is one: https://etherscan.io/address/0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503 - this person has $191,000,000+ DAI woah! Let's impersonate them :)

Now, let's add our first test. You will see a `test.js` file already in there, we will use it for reference as we build out more specific tests!

1. In your `test` folder, run `touch testDeposit.js`
2. In the `testDeposit.js`, copy-paste the following:

```javascript
const { assert } = require("chai");
const { network } = require("hardhat");

const poolAddress = "0x7d2768dE32b0b80b7a3454c06BdAc94A69DDc7A9";
const aDaiAddress = "0x028171bCA77440897B824Ca71D1c56caC55b68A3";
const daiAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";

describe("Escrow", function () {
  let signer0, address0, daiContract;
  let daiAmount = ethers.utils.parseEther("1000");
  let depositorAddr = "0x47ac0Fb4F2D84898e4D9E7b4DaB3C24507a6D503";
  before(async () => {
    
    await hre.network.provider.request({
      method: "hardhat_impersonateAccount",
      params: [depositorAddr]
    });

    await network.provider.send("hardhat_setBalance", [
      depositorAddr,
      "0x56bc75e2d63100000"
    ]);

    let richGuySigner = await ethers.provider.getSigner(depositorAddr);

    daiContract = await ethers.getContractAt("IERC20", daiAddress);

    signer0 = await ethers.provider.getSigner(0);
    address0 = await signer0.getAddress();

    daiContract.connect(richGuySigner).transfer(address0, daiAmount);
    
    });

    it("should hold dai in our account", async function () {
      const balance = await daiContract.balanceOf(address0);

      assert.equal(daiAmount.toString(), balance.toString());
    });
});
```

> Notice we are using the helper methods from Hardhat? Check out the [Hardhat Helper Methods](https://hardhat.org/hardhat-network/reference/#hardhat-network-methods).

3. Save the file.
4. Run `npx hardhat test` in order to run your tests using the forked mainnet!

## Step 3: Create Test: Deposit DAI to (Forked) Main Network Lending Pool

1. Add the following `describe` scope to your file:

```javascript

```