.env
. Here is where you will set up the environment variables for your Chainststack Avalanche Fuji endpoint and your wallet’s private key.
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:
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.contracts
with a sample contract in it. Rename this contract to FlashLoan.sol
and replace its code with the following:
scripts
directory inside the root of your project, you will find a file named deploy.js
. Replace its content with the following:
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.
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.