Professional-grade arbitrage bot for detecting and executing profitable trading opportunities between PancakeSwap and BakerySwap using flash loans
This arbitrage bot is designed to automatically detect and execute profitable trading opportunities between PancakeSwap and BakerySwap on the Binance Smart Chain (BSC). It leverages flash loans to execute risk-free arbitrage trades without requiring upfront capital, making it an ideal solution for DeFi traders and yield farmers.
- 🔍 Real-time Price Monitoring: Continuously monitors price differences between PancakeSwap and BakerySwap
- ⚡ Flash Loan Integration: Uses flash loans to execute arbitrage without capital requirements
- 💰 Profit Optimization: Calculates optimal trade sizes and gas costs for maximum profitability
- 🛡️ Risk Management: Built-in safety checks and gas cost analysis
- 📊 Live Trading Analytics: Real-time logging of prices, spreads, and trade execution
- 🔧 Configurable Parameters: Easily adjustable trading thresholds and token pairs
The bot consists of two main components:
- TypeScript Bot (
index.ts): Monitors prices and triggers arbitrage opportunities - Smart Contract (
FlashLoaner.sol): Executes flash loan arbitrage trades
- Node.js (v14 or higher)
- Yarn or npm
- BSC wallet with BNB for gas fees
- Environment variables configured
# Clone the repository
git clone https://github.com/Igamingdev1/arbitrage-bot.git
cd arbitrage-bot
# Install dependencies
yarn install
# Configure environment variables
cp .env.example .env
# Edit .env with your configurationCreate a .env file with the following variables:
PRIVATE_KEY=your_wallet_private_key
FLASH_LOANER=deployed_flash_loaner_contract_address
RPC_LINK=https://bsc-dataseed.binance.org/# Start the arbitrage bot
yarn startThe bot continuously monitors ETH/DAI (or other token pair) prices on both PancakeSwap and BakerySwap by listening to new blocks on the BSC network.
When a price discrepancy is detected, the bot calculates:
- Price spread between exchanges
- Potential profit after gas costs
- Optimal trade direction (ETH → DAI or DAI → ETH)
If profitable, the bot triggers a flash loan through the smart contract:
- Borrows tokens from PancakeSwap
- Swaps on BakerySwap at better price
- Repays the flash loan
- Keeps the profit
const spread = Math.abs((priceBakerySwap / pricePancakeSwap - 1) * 100) - 0.6
const shouldTrade = spread > (tradeAmount / reserveAmount)- Flash Loan Integration: Leverages PancakeSwap's flash loan functionality
- Gas Optimization: Minimizes transaction costs for maximum profitability
- Safety Checks: Ensures proper token approvals and transfers
- Error Handling: Graceful handling of failed transactions
- Real-time Block Monitoring: Listens to new BSC blocks for price updates
- Dynamic Gas Pricing: Adjusts gas costs based on network conditions
- Configurable Thresholds: Adjustable profit margins and trade sizes
- Comprehensive Logging: Detailed transaction and profit tracking
The bot is designed to capture arbitrage opportunities with:
- Minimum Spread: 0.6% + gas costs
- Trade Sizes: Configurable ETH/DAI amounts
- Execution Speed: Sub-second response to price changes
- Success Rate: High success rate with proper gas management
- Update token addresses in
index.ts - Modify factory addresses for new DEXes
- Adjust trade amounts and thresholds
const ETH_TRADE = 10; // ETH trade amount
const DAI_TRADE = 3500; // DAI trade amount
const spread = Math.abs((priceBakerySwap / pricePancakeSwap - 1) * 100) - 0.6;- Private Key Security: Never commit private keys to version control
- Gas Limits: Set appropriate gas limits to prevent failed transactions
- Slippage Protection: Built-in protection against price slippage
- Contract Auditing: Smart contract should be audited before mainnet deployment
Contributions are welcome! Please feel free to submit a Pull Request.
This software is for educational and research purposes. Trading cryptocurrencies involves significant risk. Use at your own risk and never invest more than you can afford to lose.
For support, You can find my contact in github profile.
Built with ❤️ for the DeFi community