Abitrage bot - kanuku/misc GitHub Wiki

Multi-DEX Arbitrage Bot

A smart contract-based arbitrage bot that executes trades across different decentralized exchanges (DEXes) to capture price differences. The bot is designed to be gas-efficient and secure, with a focus on automated arbitrage detection and execution.

Architecture

The system consists of two main components:

  1. Smart Contract: Handles the execution of arbitrage trades
  2. Bot (EOA): Monitors prices and triggers the contract when opportunities arise

Contract Flow

sequenceDiagram
    participant Bot as Bot (EOA)
    participant Contract as Arbitrage Contract
    participant TokenA as Token A (e.g., WETH)
    participant TokenB as Token B (e.g., DAI)
    participant DEX1 as Uniswap V2
    participant DEX2 as PancakeSwap V2

    Note over Bot,Contract: Initial Setup
    Bot->>TokenA: approve(Contract, amount)
    Bot->>Contract: transferFrom(Bot, Contract, amount)

    Note over Contract,DEX2: First Swap (DEX1)
    Contract->>TokenA: approve(DEX1, amount)
    Contract->>DEX1: swapExactTokensForTokens(amount, 1, [TokenA, TokenB], Contract, deadline)
    DEX1->>TokenA: transferFrom(Contract, DEX1, amount)
    DEX1->>TokenB: transfer(Contract, amountOut)
    DEX1-->>Contract: return amounts[]

    Note over Contract,DEX2: Second Swap (DEX2)
    Contract->>TokenB: approve(DEX2, token1Amount)
    Contract->>DEX2: swapExactTokensForTokens(token1Amount, 1, [TokenB, TokenA], Bot, deadline)
    DEX2->>TokenB: transferFrom(Contract, DEX2, token1Amount)
    DEX2->>TokenA: transfer(Bot, finalAmount)
    DEX2-->>Contract: return amounts[]

    Note over Bot,Contract: Optional: Withdraw Remaining Tokens
    Contract->>TokenB: approve(Contract, balance)
    Contract->>TokenB: transferFrom(Contract, Bot, balance)

    Note over Bot,Contract: Profit/Loss
    Note right of Bot: Final Balance = Initial - amount + finalAmount

Features

Current

  • ✅ Direct token swaps between two DEXes
  • ✅ Support for Uniswap V2 and PancakeSwap V2
  • ✅ Owner-only access control
  • ✅ Token withdrawal functionality
  • ✅ Gas-efficient execution path

Planned (TODO)

  1. Multi-DEX Support

    • Add support for more DEXes (SushiSwap, Curve, etc.)
    • Implement DEX router factory for easy addition of new DEXes
    • Add support for different DEX versions (V3, etc.)
  2. Automated Arbitrage Detection

    • Implement off-chain price monitoring
    • Add support for multi-hop arbitrage paths
    • Create profit calculation module
    • Add gas cost estimation
  3. Gas Optimization

    • Implement batch processing for multiple opportunities
    • Optimize token approvals
    • Add gas price monitoring
    • Implement gas-efficient token transfers
  4. Advanced Slippage Protection

    • Add dynamic slippage calculation
    • Implement price impact checks
    • Add support for flash loan arbitrage
    • Implement MEV protection

Technical Details

Contract Addresses

  • Uniswap V2 Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
  • PancakeSwap V2 Router: 0x10ED43C718714eb63d5aA57B78B54704E256024E (BSC)
  • WETH: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
  • USDC: 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

Key Functions

executeArbitrageTransaction

function executeArbitrageTransaction(
    address token0,      // Starting token (e.g., WETH)
    address token1,      // Intermediate token (e.g., DAI)
    uint256 amountIn,    // Amount of token0 to use
    bool directionDex1ToDex2  // true = DEX1->DEX2, false = DEX2->DEX1
) external onlyOwner

withdrawToken

function withdrawToken(address token) external onlyOwner

Setup and Installation

  1. Clone the repository:
git clone https://github.com/yourusername/arbitrage-bot.git
cd arbitrage-bot
  1. Install dependencies:
npm install
  1. Create a .env file:
PRIVATE_KEY=your_private_key
INFURA_URL=your_infura_url
  1. Compile the contracts:
npx hardhat compile
  1. Run tests:
npx hardhat test

Security Considerations

  • The contract uses OpenZeppelin's audited IERC20 interface
  • All critical functions are restricted to the owner
  • Token approvals are managed carefully to prevent over-approval
  • Contract includes emergency withdrawal functionality

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the GPL-3.0 License - see the LICENSE file for details.

Acknowledgments

// ... existing code ...

Technical Details

Official Specifications & Interfaces

Contract Addresses

  • Uniswap V2 Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D