This project demonstrates how to execute Flash Loans on the Aave V3 protocol using Hardhat. It includes two implementation scenarios:
- Mainnet Fork Arbitrage: A "real-world" simulation that performs arbitrage between Uniswap V2 and Sushiswap using a Mainnet fork.
- Simple Flash Loan (Sepolia/Local): A basic implementation interacting with a mock DEX contract for learning and testing purposes.
- 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.solmock 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.
-
Clone the repository:
git clone <repository_url> cd AaveFlashLoan
-
Install dependencies:
npm install
-
Configure Environment Variables: Create a
.envfile 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"
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.
This script will:
- Fork the Mainnet.
- Deploy the
FlashLoanArbitragecontract to the local fork. - Request a 1 WETH Flash Loan from Aave.
- Execute an arbitrage trade (WETH -> DAI -> WETH) across DEXs.
- Repay the loan + premium.
npx hardhat run scripts/executeFlashLoan.jsNote: 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.
Demonstrates the mechanics of borrowing and calling a mock DEX.
npx hardhat compilenpx hardhat testTo 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 sepoliaCheck the balance of your deployed contracts:
npx hardhat run scripts/checkBalance.js --network sepolia├── 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.)