TLDR:
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:
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:
See Create a project.
See View node access and credentials.
Before diving into the flash loan project, make sure to top up your wallet with testnet AVAX and USDC tokens. Use the following faucets:
See Installing Hardhat.
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:
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:
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.
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 the dotenv
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 called fuji
that connects to the Avalanche Fuji blockchain network.fuji: { ... }
defines the configuration for the fuji
network.url: process.env.FUJI_CHAINSTACK,
sets the URL for the Fuji network using the FUJI_CHAINSTACK
environment variable.accounts: [process.env.PRIVATE_KEY],
sets the accounts for the fuji
network using the PRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the Fuji network using the specified private key.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:
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.
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.
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:
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:
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.
TLDR:
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:
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:
See Create a project.
See View node access and credentials.
Before diving into the flash loan project, make sure to top up your wallet with testnet AVAX and USDC tokens. Use the following faucets:
See Installing Hardhat.
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:
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:
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.
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 the dotenv
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 called fuji
that connects to the Avalanche Fuji blockchain network.fuji: { ... }
defines the configuration for the fuji
network.url: process.env.FUJI_CHAINSTACK,
sets the URL for the Fuji network using the FUJI_CHAINSTACK
environment variable.accounts: [process.env.PRIVATE_KEY],
sets the accounts for the fuji
network using the PRIVATE_KEY
environment variable. This will allow the Hardhat project to deploy contracts and interact with the Fuji network using the specified private key.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:
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.
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.
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:
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:
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.