AAVE FlashLoan Test Framework

In this tutorial, we’ll be playing with Aave’s ability to do flashloans. Flashloans are described on the Aave website here.

This project was initially created using Aave’s truffle box template:

truffle unbox aave/flashloan-box

The contracts we’ll going to be using will be mostly unchanged from the unboxing. We’re going to try and see if we call the “flashloan” method on the “Flashloan” contract. Nothing much will happen as this contract is trivial for now, but at least we’ll have a test framework for future flashloans.

The goals are:

  • Allow testing with the free version of Infura which only allows 30 minute ganache-cli forks.
  • Scripts that allow any account to be used or any ganache-cli url.

The source for this tutorial is https://github.com/cryptocamtech/aave-flashloan-tutorial.

  1. Create your .env (cp env .env) with the account, private key and Infura project id. You can obtain the private key from Metamask and the project id is found from your settings in your Infura account.
  2. Install the various node libraries:

npm install web3
npm install dotenv
npm install @truffle/hdwallet-provider
npm install @openzeppelin/contracts

  1. Compile the contracts:

truffle compile

  1. Using a technique described AAVE Deposit/Borrowing Tutorial Part I, we obtain some ether to pay for gas.
  1. We’ll be using Dai for our flashloan so we’ll need to get some to pay for the fee. In another tutorial here we obtained Dai via Uniswap. This time we’ll do something simpler and simply transfer it from one of the addresses found on Etherscan:


This address needs to be unlocked in order to transfer Dai from it and will appear in the ganache-cli command (shown below).

  1. Since Infura only allows 30 minute sessions, you’ll eventually see this error:

Error: Returned error: project ID does not have access to archive state

  1. A script to allow us to work without the annoying archive errors/ganache-cli restarts/redeploys is below in fork_main.sh:
source .env

for (( ; ; ))
    pkill -f "node /usr/local/bin/ganache-cli"
    ganache-cli --fork https://mainnet.infura.io/v3/$INFURA_PROJECT_ID -i 1 -v --unlock 0x742d35Cc6634C0532925a3b844Bc454e4438f44e --unlock 0x07bb41df8c1d275c4259cdd0dbf0189d6a9a5f32 &
    sleep 2
    node getSomeEth.js;
    truffle migrate
    truffle exec replace.js
    # wait for < 30 mins
    sleep 1750

This script will force mainnet, unlock the two accounts for transferring Eth/Dai, give us some Eth spending money and automatically deploy the contracts every 30 minutes.

The other thing it does is call replace.js which updates .env to automatically update FLASHLOAN_CONTRACT_ADDR which is used by our web3 scripts to access our contract.

  1. The flashloan should now be ready to receive some Dai:

node sendDaiToContract

If all goes well, the contract will now have some Dai for fees. This call could be put into fork_main.sh so save making this call every 30 minutes.

  1. The contract can now be called:

node flashloan

This loan “borrows” 100 Dai, so the fee at 0.09 percent will 0.09 Dai. The 10 Dai initially transferred will show 9.91 Dai after the operation. Note that this is the final balance of the flashloan contract – not the amount of Dai in our account.

So now we can mess with Flashloan contract, which of course will be the subject of another tutorial…

Published by Crypto Cam Tech

Professional developer for 30 years + with the passion for helping others in their journey.

2 thoughts on “AAVE FlashLoan Test Framework

    1. No real Eth required, its all done on a Eth mainnet clone. I’ve noticed recently the gas fee isn’t quite high, so if it doesn’t work, set the gas fees to a higher number (which I really should update).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: