# Uniswap Integration Plan ## Overview This document outlines the implementation plan for Uniswap exchange support in the MEV bot, covering both V2 and V3 versions. ## Uniswap V2 Implementation ### Core Contract Addresses - Router: `0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D` - Factory: `0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f` ### Required Functions 1. **Swap Functions** - `swapExactTokensForTokens` - `swapTokensForExactTokens` - `swapExactETHForTokens` - `swapTokensForExactETH` - `getAmountsOut` - `getAmountsIn` 2. **Liquidity Functions** - `addLiquidity` - `addLiquidityETH` - `removeLiquidity` - `removeLiquidityETH` - `getReserves` - `pairFor` ### Implementation Steps 1. Define UniswapV2 struct implementing the Exchange interface 2. Initialize connection to Uniswap V2 contracts 3. Implement swap functions with proper slippage handling 4. Implement liquidity functions 5. Implement pricing functions using reserves 6. Add error handling for common failure cases 7. Implement gas estimation for swap and liquidity operations ## Uniswap V3 Implementation ### Core Contract Addresses - Router: `0xE592427A0AEce92De3Edee1F18E0157C05861564` - Factory: `0x1F98431c8aD98523631AE4a59f267346ea31F984` ## Uniswap V4 Implementation ### Core Contract Addresses - Pool Manager: `0x000000000022D473030F116dDEE9F6B7653f39281251` (placeholder) - Position Manager: `0x000000000022D473030F116dDEE9F6B7653f39281252` (placeholder) ### Required Functions 1. **Swap Functions** - `exactInputSingle` - `exactOutputSingle` - `exactInput` - `exactOutput` 2. **Hook Integration** - Custom hook contracts - Flexible fee mechanisms - Post-swap hooks ### Implementation Steps 1. Define UniswapV4 struct implementing the Exchange interface 2. Initialize connection to Uniswap V4 contracts 3. Handle hook-based functionality 4. Implement swap functions with hook integration 5. Add liquidity functions with hook support 6. Implement pricing functions using concentrated liquidity 7. Add error handling for hook failures 8. Implement gas estimation for hook operations ### Required Functions 1. **Swap Functions** - `exactInputSingle` - `exactOutputSingle` - `exactInput` - `exactOutput` - `getPool` - `getPosition` 2. **Liquidity Functions** - `mint` - `increaseLiquidity` - `decreaseLiquidity` - `collect` - `burn` ### Implementation Steps 1. Define UniswapV3 struct implementing the Exchange interface 2. Initialize connection to Uniswap V3 contracts 3. Handle fee tiers (500, 3000, 10000) 4. Implement multi-hop swap routing 5. Implement concentrated liquidity operations 6. Implement tick-based pricing functions 7. Add support for range orders ## Pricing Mechanisms ### Uniswap V2 Pricing - Use `getAmountsOut` to calculate expected output - Consider slippage based on trade size relative to pool reserves - Implement fee calculations (0.3% standard fee) ### Uniswap V3 Pricing - Calculate price based on current tick and liquidity - Consider tick range and liquidity distribution - Implement dynamic fee calculations based on pool configuration ## Testing Plan 1. Unit tests for all swap functions 2. Integration tests with mainnet fork 3. Edge case testing (zero amounts, max amounts) 4. Gas optimization verification 5. Slippage tolerance testing 6. Price accuracy verification ## Performance Considerations - Batch operations where possible to reduce gas costs - Cache pool information to reduce external calls - Optimize route selection for multi-hop swaps - Implement efficient price impact calculations ## Security Considerations - Validate all input parameters - Implement proper slippage protection - Prevent sandwich attacks where possible - Sanitize all external data