Welcome to the FlashLoanSimple
repository. This project showcases a basic implementation of a FlashLoan contract for Aave v3, using the Hardhat framework. The primary goal of this project is to provide a simple example of how flash loans work within the Aave ecosystem.
- What is a Flash Loan?
- Usage
- Purpose and Utility
- Verification and Security
- Commented Code
- Getting Started
- Smart Contract Deployment
- Testing and Transfer
- Contributions
- License
- Project Updates
- Additional Resources
- Donations
A flash loan is a type of DeFi (Decentralized Finance) lending mechanism that allows users to borrow assets from a liquidity pool without the need for collateral, as long as the borrowed amount is returned within a single transaction block. Flash loans are typically used for arbitrage opportunities, liquidations, or other complex financial operations in the DeFi space.
Please refer to the code comments and documentation within the project for details on how to use the FlashLoan contract. Make sure to follow best practices for security and testing before deploying it in a production environment.
The purpose of this project is to demonstrate a basic flash loan contract built on the Aave protocol. While this implementation is simplified, it can serve as a starting point for developers looking to integrate flash loans into their projects or learn how flash loans work in practice.
Each modification to this project undergoes a meticulous verification process and subsequent signing. This stringent approach guarantees the authenticity and integrity of our codebase. In case you encounter any modifications that lack appropriate verification, we strongly advise against cloning or utilizing them, as they might harbor malicious code.
Please take note: Our codebase is meticulously documented with comprehensive comments, aimed at providing a clear understanding of the functionality of individual components.
To explore and interact with our FlashLoanSimple
project, follow these steps:
-
Clone this repository to your local machine.
git clone https://github.com/Innovation-Web-3-0-Blockchain/FlashLoanSimple.git
-
Ensure you have
node.js
andnpm
installed in your environment. -
Install the necessary dependencies by running the following command in your terminal:
npm install
For easier understanding of how flash loans work, we did not write the deployment script. We used Remix to deploy the smart contract on the Hardhat local blockchain.
To deploy the FlashLoanSimple.sol
contract, follow these steps:
-
Visit the Remix Ethereum website: Remix Ethereum
-
Import the
FlashLoanSimple.sol
contract from the./contracts
repository. -
Compile the contract on Remix with the compiler version 0.8.10.
-
Set your deployment environment by forking the network you want to use for the Hardhat node. We recommend using your own RPC URL for better reliability. You can create your own Web3 API keys on the Infura website: Infura
You can also use a public RPC URL. Visit the Chainlist website for any public URL on the network you want to use: Chainlist
-
If you use your own RPC URL, create a .env file and add the following:
API_KEY="" // Insert your Web3 API key between the quotation marks PRIVATE_KEY="" // Insert your wallet private key between the quotation marks
-
Command to fork Ethereum mainnet for your Hardhat node, using your own RPC URL:
npx hardhat node --fork https://mainnet.infura.io/v3/PASTE_YOUR_API_KEY_HERE
Command to fork Ethereum mainnet for your Hardhat node, using a public RPC URL:
npx hardhat node --fork https://ethereum.publicnode.com
-
Connect Remix with Hardhat by setting the environment tab with the "Dev - Hardhat Provider" option.
-
In the "address provider" tab, paste one of the PoolAddressesProvider addresses provided by Aave. Visit their website for the specific address of the network you want to use: Aave V3 Deployed Contracts
Ethereum mainnet:
0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e
-
Click "deploy", and if the deployment is successful, you should see the transaction hash on your local node.
IMPORTANT NOTE: Make sure that your .env file is private, and that you never share its contents.
Before proceeding, ensure that your local node is running in your command line.
To test the FlashLoan and transfer assets, open a new command line tab to follow these steps:
-
Initiate a JavaScript Console
npx hardhat console --network localhost
-
Copy lines 6 and 7 from the 1_FlashLoanSimpleTest.js file, and paste them into your console.
const DAI = "0x6b175474e89094c44da98b954eedeac495271d0f"; const DAI_WHALE = "0x8A610c1C93da88c59F51A6264A4c70927814B320"
-
Get the Token Contract Address
token = await ethers.getContractAt("IERC20", DAI)
Handling the Uncaught Hardhat Error
If you encounter an Uncaught Hardhat Error, follow these additional steps:
// Wrap the token address in a shell contract Lock = await ethers.getContractFactory("Lock") // Attach the DAI to the lock contract Lock.attach(DAI) // Assign DAI to a variable dai = Lock.attach(DAI) // Check if you can get a symbol back await dai.symbol()
-
Unlock the WHALE Address
// Fetch the WHALE address DAI_WHALE // Unlock the WHALE address await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [DAI_WHALE], })
-
Set the WHALE Address as Signer
signer = await ethers.getSigner(DAI_WHALE);
If have you encountered the Uncaught Hardhat Error, use the
dai
variable instead oftoken
for the steps below. -
Connect the Signer
token.connect(signer)
-
Verify the Balance of the
FlashLoanSimple
Addressawait token.balanceOf("") // Paste the address of the SimpleFlashLoan between quotation marks
This should return a balance of 0.
-
Transfer Assets to the
FlashLoanSimple
Addressawait token.connect(signer).transfer("", ethers.utils.parseEther("1000")) // Paste the address of the SimpleFlashLoan between quotation marks
-
Verify the Transfer
Recheck the balance of the
FlashLoanSimple
address by using the same command as in step 7. This time, it should return a value. These assets are used to pay for the FlashLoan fees. -
Test the FlashLoan with Remix
In Remix under the "Deployed Contract" section, you should see
createFlashLoan
with 2 tabs: "asset" & "amount".-
In the "asset" tab, paste the token contract address, DAI address on Ethereum mainnet:
0x6b175474e89094c44da98b954eedeac495271d0f
-
In the "amount" tab, input the
parseEther
amount you want to borrow. To get theparseEther
value, you can use the following command in your console:ethers.utils.parseEther("500")
-
Copy the return value and paste it in the "amount" tab. Then, click "transact".
-
If the transaction is successful, you should see a transaction hash.
-
All of these steps are intended for educational purposes only; do not try this on a mainnet.
Contributions to this project are welcome and encouraged. If you identify any bugs, have feature requests, or would like to improve the project, please open an issue or submit a pull request. We appreciate your interest and contributions.
This project is licensed under the MIT License. For details, please refer to the LICENSE file.
As the DeFi ecosystem continues to evolve, we will monitor and update this project to align with the latest developments and best practices. Stay tuned for updates and improvements!
For more advanced topics related to this project, including deployment scripts and deploying the SimpleFlashLoan contract on testnets, including Layer 2 solutions, please refer to the Aave-Simple-Flash-Loan-Pt-2 repository.
For more details on flash loans and how they work in Aave, please refer to the Aave Flash Loans Guide.
We do not use any form of social media or engage in marketing activities. Our principles are rooted in open source and privacy, and we do not receive compensation for our contributions to GitHub. Furthermore, we do not endorse or have affiliations with any other projects.
While we remain committed to providing valuable resources for aspiring blockchain developers, any donations are greatly appreciated. Your support will help us offset the time and effort we invest in these projects to facilitate access to accessible information.
We welcome contributions in Bitcoin and Monero, and you can send contributions by scanning one of the addresses in the QR codes at the following link: Donate to Innovation Web 3.0
Thank you for your support and for being part of our community!