Skip to content

RCrobotcat/Aave-FlashLoan

Repository files navigation

Aave V3 Flash Loan & Arbitrage Project

This project demonstrates how to execute Flash Loans on the Aave V3 protocol using Hardhat. It includes two implementation scenarios:

  1. Mainnet Fork Arbitrage: A "real-world" simulation that performs arbitrage between Uniswap V2 and Sushiswap using a Mainnet fork.
  2. Simple Flash Loan (Sepolia/Local): A basic implementation interacting with a mock DEX contract for learning and testing purposes.

Project Overview

  • FlashLoan.sol: A basic receiver contract to demonstrate borrowing and repaying a flash loan.
  • FlashLoanArbitrage.sol (Sepolia): Demonstrates arbitrage logic by interacting with a custom deployed Dex.sol mock contract.
  • contracts/mainnet/FlashLoanArbitrage.sol: A production-ready style contract designed to spot price differences between Uniswap and Sushiswap and execute a profitable trade using flash-loaned capital.

Prerequisites

Installation

  1. Clone the repository:

    git clone <repository_url>
    cd AaveFlashLoan
  2. Install dependencies:

    npm install
  3. Configure Environment Variables: Create a .env file in the root directory and add the following:

    # For Mainnet Forking (Required for Arbitrage Simulation)
    MAINNET_RPC_URL="https://mainnet.infura.io/v3/YOUR_INFURA_KEY"
    
    # For Sepolia Deployment (Optional)
    INFURA_SEPOLIA_ENDPOINT="https://sepolia.infura.io/v3/YOUR_INFURA_KEY"
    PRIVATE_KEY="your_wallet_private_key"

Usage Scenario 1: Mainnet Fork Arbitrage

This is the most interesting part. We fork the Ethereum Mainnet locally to simulate a real arbitrage trade between Uniswap and Sushiswap without spending real money.

1. Run the Arbitrage Script

This script will:

  1. Fork the Mainnet.
  2. Deploy the FlashLoanArbitrage contract to the local fork.
  3. Request a 1 WETH Flash Loan from Aave.
  4. Execute an arbitrage trade (WETH -> DAI -> WETH) across DEXs.
  5. Repay the loan + premium.
npx hardhat run scripts/executeFlashLoan.js

Note: If the spread between Uniswap and Sushiswap is not profitable at the current block, the transaction may revert with "No profit made" or similar logic defined in the contract.


Usage Scenario 2: Basic Flash Loan (Sepolia/Local)

Demonstrates the mechanics of borrowing and calling a mock DEX.

1. Compile Contracts

npx hardhat compile

2. Run Tests

npx hardhat test

3. Deploy to Sepolia Testnet

To deploy the Flash Loan or Dex contract to a live testnet:

# Deploy the Mock DEX
npx hardhat run scripts/deployDex.js --network sepolia

# Deploy the Flash Loan Contract
npx hardhat run scripts/deployFlashLoanArbitrage.js --network sepolia

4. Verify Balance (Optional)

Check the balance of your deployed contracts:

npx hardhat run scripts/checkBalance.js --network sepolia

Project Structure

├── contracts/
│   ├── Dex.sol                     # Mock DEX for testing arbitrage logic locally
│   ├── FlashLoan.sol               # Simple Flash Loan implementation
│   ├── FlashLoanArbitrage.sol      # Arbitrage logic for Sepolia/Mock testing
│   └── mainnet/
│       └── FlashLoanArbitrage.sol  # Real-world Arbitrage logic (Uni/Sushi)
├── scripts/
│   ├── deployDex.js                # Deploys Mock DEX
│   ├── deployFlashLoan.js          # Deploys Basic Flash Loan
│   ├── executeFlashLoan.js         # MAIN SCRIPT: Runs the mainnet fork arbitrage
│   └── ...
├── test/                           # Hardhat tests
├── hardhat.config.js               # Hardhat configuration (Network, Compilers)
└── package.json                    # Dependencies (Aave core-v3, Hardhat, etc.)

About

Aave-FlashLoan

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors