This repo contains the code for my cryptocurrency trading bot. The bot borrows some amount of some cryptocurrency from Aave V3 protocol flash loans and executes arbitrage by swapping on Uniswap V3 and Sushiswap protocols.
Table of Contents
To use download and use this project on your machine you need the following installed:
- Git version control system which you can download from here. To check git installation, run:
git --version
- Node.js JavaScript runtime which you can download from here. To check Node.js installation, run:
node --version
- Yarn dependency management package which you can download from here. To check yarn installation, run:
yarn --version
-
In the folder where you want to clone the repo type:
git clone https://github.com/Niceural/flash-loan-arbitrage-bot.git
- Create a .env file and paste your account private key, your RPC URLs, and your Etherscan API key, following the .env.example template
-
Install the dependencies by running:
yarn install
-
Check your installation by running:
hh compile
Aave V3 Protocol
- The program fetching price discrepancies running on our computer calls the function on our contract responsible for initiating the flash loan. This function is being passed the address and amount of the assets to borrow, and the parameters describing the arbitrage logic (i.e. which asset to swap first, with which protocol, etc).
- `flashLoan()` from the `Pool` contract is called and being passed the parameters described above.
- After some sanity checks the amount borrowed is transferred to our contract and the `executeOperation()` function of our contract is called. It is being passed the address and amount of each assets and the parameters describing the arbitrage logic.
- Our contract allows the pool to transfer the amount borrowed plus fees. If this amount is not available, this whole process reverts.
- The flashLoan() function of the Pool contract is called by our contract
- The parameters passed by our contract as well as member variables of the Pool contract are converted into a variable of type FlashloanParams
- The executeFlashLoan() function is called and passed the variable created above
- The validateFlashloan() function is called. This function performs three checks:
- it reverts with error Errors.INCONSISTENT_FLASHLOAN_PARAMS if the length of the assets array is different from the length of the amounts array
- it reverts with error Errors.RESERVE_PAUSED if the state of one of the reserves is paused
- it reverts with error Errors.RESERVE_INACTIVE if the state of the reserve is inactive
- A variable of type FlashLoanLocalVars is constructed from the executeFlashLoan() function