Welcome to MEV Arbitrage Bot, our first dive into the fascinating world of decentralized finance (DeFi), blockchain development, and Miner Extractable Value (MEV). This project aims to provide an automated arbitrage bot that identifies profitable opportunities across multiple decentralized exchanges (DEXs), executes trades, and integrates Flashbots for efficient transaction bundling. Built on Python with FastAPI, this project serves as a stepping stone into blockchain-powered arbitrage trading.
- Real-Time Arbitrage Detection: The bot scans token prices across DEXs (e.g., Uniswap, SushiSwap) to identify arbitrage opportunities.
- Flashbots Integration: Leverages Flashbots to submit private transaction bundles to miners, preventing frontrunning and reducing gas fees.
- Testnet Support: Fully functional on Ethereum testnets like Sepolia for safe experimentation.
- REST API: Expose endpoints for real-time interaction via a FastAPI-powered backend.
- Dynamic Logging: Track every step of the process, from token price fetching to transaction execution.
- Python: Core programming language.
- FastAPI: Framework for building REST APIs.
- Web3.py: For blockchain interaction.
- Flashbots: For transaction bundling and miner communication.
- Alchemy: Ethereum RPC URL for Sepolia Testnet.
- dotenv: Manage environment variables.
- requests: HTTP requests for price fetching.
- pydantic: Data validation.
- Uvicorn: ASGI server to run the API.
mev_arbitrage_bot/
├── contracts/ # Smart contracts for interacting with DEXs (optional for future enhancements)
├── strategies/ # Arbitrage strategies
│ ├── base_strategy.py # Base class for arbitrage strategies
│ └── uniswap_sushi_strategy.py # Example: Arbitrage between Uniswap and SushiSwap
├── utils/ # Utility functions
│ ├── logger.py # Custom logging setup
│ └── constants.py # Common constants
├── dex_service.py # Fetch prices from multiple DEXs
├── flashbots_service.py # Flashbots integration
├── main.py # Main FastAPI application
├── requirements.txt # Python dependencies
├── .env # Environment variables (add this to .gitignore!)
└── README.md # Documentation
Ensure you have the following installed:
- Python 3.11
- Node.js (optional for additional tools)
-
Clone the repository:
git clone https://github.com/your-username/mev-arbitrage-bot.git cd mev-arbitrage-bot
-
Create and activate a virtual environment:
python3.11 -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up your
.env
file:WEB3_PROVIDER_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY PRIVATE_KEY=0xYourPrivateKey FLASHBOTS_RELAY_URL=https://relay.flashbots.net
-
Run the server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
Description: Check if the bot is online.
Response:
{
"message": "MEV Arbitrage Bot (Testnet) is online!"
}
Description: Execute an arbitrage strategy.
Request Body:
{
"tokenA": "ETH",
"tokenB": "DAI",
"amount": 1
}
Response (Example):
{
"message": "Arbitrage executed successfully",
"tx_hashes": ["0x1234...", "0x5678..."],
"profit_estimate": 0.05
}
-
Test the
/
endpoint:curl -X GET http://127.0.0.1:8000/
-
Test the
/arbitrage
endpoint:curl -X POST http://127.0.0.1:8000/arbitrage \ -H "Content-Type: application/json" \ -d '{"tokenA": "ETH", "tokenB": "DAI", "amount": 1}'
-
Price Fetching:
- The bot fetches token prices from Uniswap and SushiSwap using the
dex_service.py
module.
- The bot fetches token prices from Uniswap and SushiSwap using the
-
Arbitrage Calculation:
- The bot identifies profitable opportunities using the logic in
uniswap_sushi_strategy.py
.
- The bot identifies profitable opportunities using the logic in
-
Transaction Submission:
- Profitable trades are submitted either as normal transactions or as Flashbots bundles to miners.
-
Logging:
- All key events (price fetching, profit calculations, transaction hashes) are logged in the console.
- More DEX Integrations: Expand support to Curve, Balancer, etc.
- Cross-Chain Arbitrage: Identify opportunities across different blockchains.
- AI-Driven Strategies: Use AI models to predict arbitrage opportunities.
- Frontend Integration: Build a React-based UI to monitor and control the bot.
- Sachin Bansal (PureBl00d): Developer and Blockchain enthusiast
- Anshuman Yadav (0xTokenSmith): Developer and Blockchain Enthusiast