Avalanche: Aave V3 flash loan with Hardhat
TLDR
- You’ll configure a Hardhat project to interact with Aave V3 flash loans on Avalanche’s Fuji testnet.
- You’ll use Chainstack for your Avalanche node endpoint and deploy a custom FlashLoan contract.
- You’ll borrow USDC, then repay it plus fees in a single transaction, demonstrating a flash loan’s instant, collateral-free mechanics.
- By the end, you’ll have a working Aave flash loan flow on a testnet environment ready for deeper custom logic.
Flash loans
A flash loan is a type of loan that can be obtained instantly and without any collateral, unlike traditional loans that require time-consuming application processes and collateral such as property or assets. This type of loan is available through Aave, a decentralized lending platform, where borrowers can borrow any amount they need and repay it within a single transaction. The loan is secured by the borrower’s smart contract and is only valid for the duration of that transaction. If the borrower cannot repay the loan and the associated fees within the same transaction, the loan is automatically canceled, and the transaction is reverted. Flash loans are often used in the context of cryptocurrency trading and arbitrage, as they enable traders to obtain funds quickly and cheaply to take advantage of market opportunities.
For detailed documentation, see Aave Developers: Flash Loans.
The objective of this tutorial is to make you familiar with the Avalanche C-Chain, the Hardhat framework, and the Aave flash loans.
Specifically, in this tutorial, you will:
- Deploy an Avalanche node on the Fuji testnet.
- Create a flash loan project using Hardhat.
- Run a flash loan on the Fuji testnet through an Avalanche node deployed with Chainstack.
Prerequisites
- Chainstack account to deploy an Avalanche node.
- Node.js as the JavaScript framework.
- Hardhat to create, deploy, and interact with contracts.
Dependencies
- Hardhat: ^2.12.7
- @aave/core-v3: ^1.17.2
- dotenv: ^16.0.3
Overview
This tutorial shows you how to request a flash loan on the Fuji testnet to borrow USDC. To get from zero to an executed Aave V3 flash loan on the Avalanche Fuji C-Chain testnet, do the following:
- With Chainstack, create a public chain project.
- With Chainstack, join the Avalanche Fuji testnet.
- With Chainstack, access your Avalanche node endpoint.
- With Hardhat, create and set up an Aave flash loan project.
- With Hardhat, execute the flash loan through your Avalanche node.
Step-by-step
Create a public chain project
See Create a project.
Join the Avalanche Fuji testnet
Get your Avalanche node endpoint
See View node access and credentials.
Fund your wallet
Before diving into the flash loan project, make sure to top up your wallet with testnet AVAX and USDC tokens. Use the following faucets:
- Aave faucet for USDC. Make sure you are on the Avalanche Market.
- Fuji faucet for AVAX.
Install HardHat
See Installing Hardhat.
Create a Hardhat project
Create a new directory for your project, then run the following from a terminal:
This will launch the Hardhat CLI, which will prompt you to choose a starter project. For this project, answer yes to the following:
- Create a JavaScript project
- Do you want to install this sample project’s dependencies with npm (hardhat @nomicfoundation/hardhat-toolbox)?
Install the required dependencies
This project uses the aave/core-v3 package for the smart contracts and the dotenv package to safely use environment variables.
Run the following command in your root directory to install:
Create a .env file
In your project’s root directory, create a new file and name it .env
. Here is where you will set up the environment variables for your Chainststack Avalanche Fuji endpoint and your wallet’s private key.
Save it after you added your information.
Edit the Hardhat configuration file
You will find a file named hardhat.config.js
in the root directory. This file is used to configure various settings for your Hardhat projects, such as the network you want to deploy your contracts on, the compilers you want to use, and the plugins you want to enable.
Delete the default code in the file and replace it with the following:
Let’s break down what each part of the file does:
require("@nomicfoundation/hardhat-toolbox");
imports the Hardhat Toolbox plugin, which provides several useful tools and utilities for Hardhat projects.require("dotenv").config();
loads environment variables from a.env
file using thedotenv
package.module.exports = { ... }
exports a JavaScript object containing the configuration for the Hardhat project.solidity: "0.8.10",
sets the Solidity compiler version to 0.8.10.networks: { ... }
defines the network configurations for the Hardhat project. In this case, it defines a network calledfuji
that connects to the Avalanche Fuji blockchain network.fuji: { ... }
defines the configuration for thefuji
network.url: process.env.FUJI_CHAINSTACK,
sets the URL for the Fuji network using theFUJI_CHAINSTACK
environment variable.accounts: [process.env.PRIVATE_KEY],
sets the accounts for thefuji
network using thePRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the Fuji network using the specified private key.
Create the flash loan smart contract
In the root directory, you will find a directory named contracts
with a sample contract in it. Rename this contract to FlashLoan.sol
and replace its code with the following:
Default flash loan logic
This smart contract receives the flash loan but performs no further actions on it. You will need to add your own logic.
This smart contract is heavily commented on to explain its inner workings, but you can find more details on the Aave docs.
Create the deploying and interacting script
In the scripts
directory inside the root of your project, you will find a file named deploy.js
. Replace its content with the following:
This code is a script that deploys a FlashLoan
smart contract and uses it to request a flash loan of 1,000 USDC tokens.
The script first sets some constants, including the addresses of the Aave pool provider and the USDC token on the Fuji testnet. Verify that the addresses are up to date on the Aave docs by finding the addresses for PoolAddressesProvider-Avalanche
and USDC-TestnetMintableERC20-Avalanche
. The amount of USDC to be borrowed is also declared here.
It then deploys the FlashLoan
contract and transfers 5 USDC tokens to the contract from the deployer’s account. To request flash loans, the smart contract must hold some of the tokens that you are planning to borrow; these tokens are used to repay the fee. On the V3 version, the fee is a fixed percentage, and you can find the updated fee value on the Aave docs.
Next, the script checks the USDC balance of the FlashLoan
contract, this is only for displaying it to the user, but you can easily implement some logic to stop the process if the funds to repay the borrowing fee are too low. It then requests a flash loan of 1,000 USDC tokens. Once the loan is executed, the remaining USDC tokens in the contract are withdrawn. The Aave documentation recommends not leaving any funds in the smart contract to avoid possible misuse by an attacker.
The script uses the Hardhat development framework and the ethers.js
library to interact with the blockchain network and the FlashLoan
contract. It also prints out messages to the console at various points in the script’s execution to provide information about the progress of the FlashLoan
operation.
Run the flash loan
To run the flash loan on the Fuji network, execute the following command in the console from your root directory:
This command will compile the smart contracts, deploy the FlashLoan contract and execute the operation
The result in the console will look like the following:
Possible compiler warnings
Note that you might receive two warnings from the Solidity compiler about two Unused function parameter
. You can ignore the warnings as they do not stop the compiler or the execution of the flash loan. This is happening because the function is being overridden and the parameters are needed to keep the same function’s signature.
You can see how a completed deployment and flash loan looks like on the Fuji explorer by checking the following transactions:
Conclusion
This tutorial guided you through setting up Hardhat to work with Chainstack nodes and creating a project to run your own flash loan transaction on the Avalanche network.
This tutorial uses a testnet; however, the exact same instructions and sequence will work on the mainnet as well.