A Solidity-based flash loan arbitrage smart contract using Aave and Hardhat. Built to scan DEX price differences and execute profitable trades atomically.
Flash Loan Arbitrage Bot
A simplified arbitrage bot that checks for opportunities across multiple DEXes and executes flash loan arbitrage trades.
Updates
New Features
- Custom Profit Recipient: You can now specify which address will receive the profits from arbitrage trades
- Price Validation: Added validation to filter out abnormal prices, especially for the WETH/DAI pair in Uniswap V3 0.01% pool
- Enhanced Logging: Better transaction result logging with detailed profit confirmation
- Simplified Deployment: Single script to deploy the contract and set the profit recipient
- Live Quoter Results: Real-time price quotes from exchanges to find profitable arbitrage opportunities
- Multi-Network Support: Deploy to mainnet, testnets, or local networks with the same scripts
Setup
Prerequisites
- Node.js
- Hardhat
- Ethereum wallet with ETH for gas
- Access to RPC endpoints
Installation
- Clone the repository
- Install dependencies:
npm install
- Copy
.env.exampleto.envand configure your environment variables:
cp .env.example .env
- Update the
.envfile with your values:
# RPC URLs for different networks
MAINNETRPCURL=https://mainnet.infura.io/v3/yourinfurakey
SEPOLIARPCURL=https://sepolia.infura.io/v3/yourinfurakey
INFURAAPIKEY=yourinfuraapi_key
Local network
RPC_URL=http://127.0.0.1:8545
Private key for deployment - use a real funded key for mainnet/testnet deployments
PRIVATEKEY=yourprivatekeyhere
Etherscan API key for contract verification
ETHERSCANAPIKEY=youretherscanapi_key
Contract addresses - will be filled after deployment
ARBITRAGECONTRACTADDRESS=
DEX addresses - already filled with mainnet addresses
AAVEPOOLPROVIDER_ADDRESS=0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e
UNISWAPV2ROUTER_ADDRESS=0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
UNISWAPV3ROUTER_ADDRESS=0xE592427A0AEce92De3Edee1F18E0157C05861564
SUSHISWAPROUTERADDRESS=0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F
BALANCERVAULTADDRESS=0xBA12222222228d8Ba445958a75a0704d566BF2C8
Network Deployment Options
The project supports deployment to multiple networks:
- Local Hardhat Node (default): For development and testing
- Localhost: For a persistent local node
- Ethereum Mainnet: For production deployment
- Sepolia Testnet: For testing with test ETH before mainnet deployment
Deployment to Different Networks
Using the Hardhat Deployment Script
You can deploy to any configured network using the --network flag:
# Deploy to local Hardhat node (default)
npx hardhat run scripts/deploy.js
Deploy to localhost network
npx hardhat run scripts/deploy.js --network localhost
Deploy to Sepolia testnet
npx hardhat run scripts/deploy.js --network sepolia
Deploy to Ethereum mainnet (production)
npx hardhat run scripts/deploy.js --network mainnet
Using the Direct Deployment Script
For the direct deployment script, update the RPC_URL in your .env file:
# For localhost
RPC_URL=http://127.0.0.1:8545
For Sepolia testnet
RPCURL=https://sepolia.infura.io/v3/yourinfura_key
For Ethereum mainnet
RPCURL=https://mainnet.infura.io/v3/yourinfura_key
Then run:
node scripts/deploy-direct.js
Important Considerations for Mainnet/Testnet Deployment
- Use a Properly Funded Account:
- Configure API Keys:
- Contract Verification:
- Gas Settings:
Complete Guide to Running the Bot
Step 1: Environment Setup
- Ensure your
.envfile is properly configured with:
- Verify all DEX router addresses are correct for your target network
Step 2: Start a Forked Network (for testing)
Start a local Hardhat node that forks from mainnet:
npx hardhat node --fork https://mainnet.infura.io/v3/YOURINFURAKEY
For production use, set your .env file with a real mainnet RPC URL.
Step 3: Deploy the Arbitrage Contract
Compile the contracts first:
npx hardhat compile
Deploy the contract using the direct deployment script:
npx hardhat run scripts/deploy-direct.js --network localhost
This will:
- Deploy the Arbitrage contract
- Set the profit recipient to your wallet address (from your PRIVATE_KEY)
- Output the new contract address to the console
- Automatically update your
.envfile with the contract address
.env file.
Step 4: Run the Arbitrage Bot
Launch the bot with:
node arbitrageBot.js
The bot will:
- Initialize with settings from your
.envfile - Connect to the blockchain network
- Load the Arbitrage contract
- Begin checking for arbitrage opportunities
- Execute trades when profitable opportunities are found
- Log all activities and results
Step 5: Monitor the Logs
The bot logs all activities to the console and to the log file specified in your .env file.
To watch the log file in real-time:
# On Linux/Mac
tail -f arbitrage.log
On Windows PowerShell
Get-Content arbitrage.log -Wait
The logs will show:
- Route checking between different exchanges
- Price differences found
- Profitable opportunities with expected profit
- Transaction execution results
- Any errors or issues encountered
Troubleshooting
Deployment Issues
- Compile errors: Make sure you've run
npx hardhat compilebefore deployment - Network connection: Verify your RPC_URL is accessible and responding
- Gas errors: Ensure your wallet has enough ETH for deployment
- Contract verification: If the contract address isn't updated in your
.env, manually update it
Bot Execution Issues
- Contract connection: Verify the ARBITRAGECONTRACTADDRESS in your
.envis correct - Pricing anomalies: The bot has validation to filter out abnormal prices
- Transaction failures: Check logs for details on why transactions are reverting
- Profit threshold: You may need to adjust slippage settings if opportunities are being missed
Performance Optimization
- Adjust slippage: Lower values (0.1-0.3%) for more conservative trades, higher (0.5-1%) for more opportunities
- Network congestion: During high gas periods, increase your gas price for faster execution
- Token pairs: Focus on high-liquidity pairs for more stable arbitrage opportunities