Files
mev-beta/pkg/exchanges
2025-11-08 10:37:52 -06:00
..
2025-11-08 10:37:52 -06:00

MEV Bot Exchange Support

Overview

This directory contains implementations for various decentralized exchange (DEX) protocols in the MEV Bot. Each exchange protocol is implemented with a consistent interface to allow for cross-exchange arbitrage opportunities.

Supported Exchanges

Core V2-style AMMs

  • Uniswap V2: Standard constant product AMM
  • SushiSwap: Fork of Uniswap V2 with additional features
  • PancakeSwap: Binance Smart Chain implementation similar to Uniswap V2

V3-style Concentrated Liquidity

  • Uniswap V3: Concentrated liquidity with ticks and fee tiers
  • Uniswap V4: Next-generation AMM with hooks and more flexibility
  • Kyber: Elastic AMM with concentrated liquidity and custom hooks
  • Camelot: Arbitrum-focused AMM with concentrated liquidity

StableSwap AMMs

  • Curve: Stableswap algorithm for similar-asset swaps
  • Balancer: Weighted and stable pool implementations

Multi-Asset AMMs

  • Balancer: Weighted, stable, and composable stable pools
  • Dex Aggregators: Integration with 1inch, ParaSwap, etc.

Architecture

Each exchange implementation follows the same interface pattern with three primary components:

1. PoolDetector

  • Discovers pools for token pairs
  • Handles multiple fee tiers
  • Identifies pool types

2. LiquidityFetcher

  • Fetches pool reserves and data
  • Calculates spot prices
  • Estimates liquidity depth

3. SwapRouter

  • Calculates swap amounts
  • Generates transaction data
  • Validates swaps

Exchange-Specific Features

Uniswap V4

  • Hooks support for custom logic
  • Concentrated liquidity model
  • Flexible fees and parameters

Kyber

  • Elastic AMM architecture
  • Concentrated liquidity
  • Customizable parameters

Curve

  • Stableswap invariant
  • Multi-asset support
  • Amplification coefficients

Balancer

  • Weighted pools
  • Stable pools
  • Composable stable pools
  • Managed pools

Aggregators

  • Multi-DEX routing
  • Gas optimization
  • Price comparison across exchanges

Common Interface Usage

All exchanges implement the following interfaces:

type PoolDetector interface {
    GetAllPools(token0, token1 common.Address) ([]common.Address, error)
    GetPoolForPair(token0, token1 common.Address) (common.Address, error)
    GetSupportedFeeTiers() []int64
    GetPoolType() string
}

type LiquidityFetcher interface {
    GetPoolData(poolAddress common.Address) (*math.PoolData, error)
    GetTokenReserves(poolAddress, token0, token1 common.Address) (*big.Int, *big.Int, error)
    GetPoolPrice(poolAddress common.Address) (*big.Float, error)
    GetLiquidityDepth(poolAddress, tokenIn common.Address, amount *big.Int) (*big.Int, error)
}

type SwapRouter interface {
    CalculateSwap(tokenIn, tokenOut common.Address, amountIn *big.Int) (*big.Int, error)
    GenerateSwapData(tokenIn, tokenOut common.Address, amountIn, minAmountOut *big.Int, deadline *big.Int) ([]byte, error)
    GetSwapRoute(tokenIn, tokenOut common.Address) ([]common.Address, error)
    ValidateSwap(tokenIn, tokenOut common.Address, amountIn *big.Int) error
}

Cross-Exchange Arbitrage

The system supports identifying and executing arbitrage opportunities across all supported exchanges. The CrossExchangeArbitrageFinder component:

  • Monitors prices across all exchanges
  • Identifies profitable opportunities
  • Considers gas costs and slippage
  • Executes arbitrage when profitable

Configuration

Each exchange has specific configuration parameters including:

  • Contract addresses (factory, router)
  • Chain ID
  • Default slippage tolerance
  • Gas estimation parameters
  • Pool initialization code hash
  • Supported fee tiers

Math Support

The system includes exchange-specific pricing models via the ExchangePricingEngine:

  • Uniswap V2: Constant product formula
  • Uniswap V3/V4: Concentrated liquidity with ticks
  • Curve: Stableswap invariant
  • Balancer: Weighted and stable pool formulas
  • Kyber: Elastic concentrated liquidity

Performance Considerations

  • Efficient pool discovery mechanisms
  • Cached price calculations
  • Optimized route finding
  • Gas cost estimation
  • Slippage protection