Skip to content

Niceural/flash-loan-arbitrage-bot

Repository files navigation


Logo

Flash Loan and Arbitrage Crypto Trading Bot

View Demo Video · Report Bug · Fork this Repository




About The Project

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
  1. Getting Started
  2. DeFi Protocols
  3. Smart Contracts
  4. Mocking and Testing

Getting Started

Prerequisites

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

Installation

Follow these steps to install the repo on your machine:
  1. In the folder where you want to clone the repo type:
    git clone https://github.com/Niceural/flash-loan-arbitrage-bot.git
  2. Create a .env file and paste your account private key, your RPC URLs, and your Etherscan API key, following the .env.example template
  3. Install the dependencies by running:
    yarn install
  4. Check your installation by running:
    hh compile

DeFi Protocols

Aave V3 Protocol

The Aave

Flash Loans

A contract can call the function `flashLoan()` or `flashLoanSimple()` on the `Pool` contract. The contract calling the function can then access the liquidity of the pool (i.e. the money owned by it) and use it as long as the amount borrowed plus fee is returned to the pool in the same transaction. Contrarily to `flashLoanSimple()`, `flashLoan()` can access to liquidity of multiple reserves. The contract calling `flashLoan()` on the pool to execute an arbitrage operation should follow the procedure described below:
  1. 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).
  2. `flashLoan()` from the `Pool` contract is called and being passed the parameters described above.
  3. 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.
  4. Our contract allows the pool to transfer the amount borrowed plus fees. If this amount is not available, this whole process reverts.

Flash loan contract execution flow

On a contract level, the execution flow of an Aave flash loan for multiple assets is described below:
  1. The flashLoan() function of the Pool contract is called by our contract
  2. The parameters passed by our contract as well as member variables of the Pool contract are converted into a variable of type FlashloanParams
  3. The executeFlashLoan() function is called and passed the variable created above
  4. The validateFlashloan() function is called. This function performs three checks:
    1. it reverts with error Errors.INCONSISTENT_FLASHLOAN_PARAMS if the length of the assets array is different from the length of the amounts array
    2. it reverts with error Errors.RESERVE_PAUSED if the state of one of the reserves is paused
    3. it reverts with error Errors.RESERVE_INACTIVE if the state of the reserve is inactive
  5. A variable of type FlashLoanLocalVars is constructed from the executeFlashLoan() function

Smart Contracts

AaveV3FlashLoan

UniswapSushiswapArbitrage

Mocking and Testing

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages