feat(core): implement core MEV bot functionality with market scanning and Uniswap V3 pricing
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
This commit is contained in:
@@ -6,13 +6,13 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/fraktal/mev-beta/internal/config"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
"github.com/fraktal/mev-beta/pkg/circuit"
|
||||
"github.com/fraktal/mev-beta/pkg/events"
|
||||
"github.com/fraktal/mev-beta/pkg/trading"
|
||||
"github.com/fraktal/mev-beta/pkg/uniswap"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/holiman/uint256"
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
@@ -51,12 +51,12 @@ func NewMarketScanner(cfg *config.BotConfig, logger *logger.Logger) *MarketScann
|
||||
cache: make(map[string]*CachedData),
|
||||
cacheTTL: time.Duration(cfg.RPCTimeout) * time.Second,
|
||||
slippageProtector: trading.NewSlippageProtection(logger),
|
||||
circuitBreaker: circuit.NewCircuitBreaker(&circuit.Config{
|
||||
circuitBreaker: circuit.NewCircuitBreaker(&circuit.Config{
|
||||
Logger: logger,
|
||||
Name: "market_scanner",
|
||||
MaxFailures: 10,
|
||||
ResetTimeout: time.Minute * 5,
|
||||
MaxRequests: 3,
|
||||
Name: "market_scanner",
|
||||
MaxFailures: 10,
|
||||
ResetTimeout: time.Minute * 5,
|
||||
MaxRequests: 3,
|
||||
SuccessThreshold: 2,
|
||||
}),
|
||||
}
|
||||
@@ -210,12 +210,12 @@ func (s *MarketScanner) analyzeNewPoolEvent(event events.Event) {
|
||||
func (s *MarketScanner) calculatePriceMovement(event events.Event, poolData *CachedData) (*PriceMovement, error) {
|
||||
// Calculate the price before the swap using Uniswap V3 math
|
||||
priceBefore := uniswap.SqrtPriceX96ToPrice(poolData.SqrtPriceX96.ToBig())
|
||||
|
||||
|
||||
// For a more accurate calculation, we would need to:
|
||||
// 1. Calculate the price after the swap using Uniswap V3 math
|
||||
// 2. Account for liquidity changes
|
||||
// 3. Consider the tick spacing and fee
|
||||
|
||||
|
||||
priceMovement := &PriceMovement{
|
||||
Token0: event.Token0.Hex(),
|
||||
Token1: event.Token1.Hex(),
|
||||
@@ -227,13 +227,13 @@ func (s *MarketScanner) calculatePriceMovement(event events.Event, poolData *Cac
|
||||
TickBefore: event.Tick,
|
||||
Timestamp: time.Now(), // In a real implementation, use the actual event timestamp
|
||||
}
|
||||
|
||||
|
||||
// Calculate price impact using a more realistic approach
|
||||
// For Uniswap V3, price impact is roughly amountIn / liquidity
|
||||
if event.Liquidity != nil && event.Liquidity.Sign() > 0 && event.Amount0 != nil && event.Amount0.Sign() > 0 {
|
||||
liquidityFloat := new(big.Float).SetInt(event.Liquidity.ToBig())
|
||||
amountInFloat := new(big.Float).SetInt(event.Amount0)
|
||||
|
||||
|
||||
// Price impact ≈ amountIn / liquidity
|
||||
priceImpact := new(big.Float).Quo(amountInFloat, liquidityFloat)
|
||||
priceImpactFloat, _ := priceImpact.Float64()
|
||||
@@ -247,7 +247,7 @@ func (s *MarketScanner) calculatePriceMovement(event events.Event, poolData *Cac
|
||||
priceImpact, _ := impact.Float64()
|
||||
priceMovement.PriceImpact = priceImpact
|
||||
}
|
||||
|
||||
|
||||
return priceMovement, nil
|
||||
}
|
||||
|
||||
@@ -257,50 +257,50 @@ func (s *MarketScanner) isSignificantMovement(movement *PriceMovement, threshold
|
||||
if movement.PriceImpact > threshold {
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// Also check if the absolute amount is significant
|
||||
if movement.AmountIn != nil && movement.AmountIn.Cmp(big.NewInt(1000000000000000000)) > 0 { // 1 ETH
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
// For smaller amounts, we need a higher price impact to be significant
|
||||
if movement.AmountIn != nil && movement.AmountIn.Cmp(big.NewInt(100000000000000000)) > 0 { // 0.1 ETH
|
||||
return movement.PriceImpact > threshold/2
|
||||
}
|
||||
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// findRelatedPools finds pools that trade the same token pair
|
||||
func (s *MarketScanner) findRelatedPools(token0, token1 common.Address) []*CachedData {
|
||||
s.logger.Debug(fmt.Sprintf("Finding related pools for token pair %s-%s", token0.Hex(), token1.Hex()))
|
||||
|
||||
|
||||
relatedPools := make([]*CachedData, 0)
|
||||
|
||||
|
||||
// In a real implementation, this would query a pool registry or
|
||||
// search through known pools for pools with the same token pair
|
||||
// For now, we'll return some mock data
|
||||
|
||||
|
||||
// Check if we have cached data for common pools
|
||||
commonPools := []string{
|
||||
"0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640", // USDC/WETH Uniswap V3 0.05%
|
||||
"0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc", // USDC/WETH Uniswap V2 0.3%
|
||||
}
|
||||
|
||||
|
||||
for _, poolAddr := range commonPools {
|
||||
poolData, err := s.getPoolData(poolAddr)
|
||||
if err != nil {
|
||||
s.logger.Debug(fmt.Sprintf("No data for pool %s: %v", poolAddr, err))
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Check if this pool trades the same token pair (in either direction)
|
||||
if (poolData.Token0 == token0 && poolData.Token1 == token1) ||
|
||||
(poolData.Token0 == token1 && poolData.Token1 == token0) {
|
||||
(poolData.Token0 == token1 && poolData.Token1 == token0) {
|
||||
relatedPools = append(relatedPools, poolData)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
s.logger.Debug(fmt.Sprintf("Found %d related pools", len(relatedPools)))
|
||||
return relatedPools
|
||||
}
|
||||
@@ -313,44 +313,44 @@ func (s *MarketScanner) estimateProfit(event events.Event, pool *CachedData, pri
|
||||
// - Gas cost estimation
|
||||
// - Slippage calculations
|
||||
// - Path optimization
|
||||
|
||||
|
||||
// For now, we'll use a simplified calculation
|
||||
amountIn := new(big.Int).Set(event.Amount0)
|
||||
priceDiffInt := big.NewInt(int64(priceDiff * 1000000)) // Scale for integer math
|
||||
|
||||
|
||||
// Estimated profit = amount * price difference
|
||||
profit := new(big.Int).Mul(amountIn, priceDiffInt)
|
||||
profit = profit.Div(profit, big.NewInt(1000000))
|
||||
|
||||
|
||||
// Subtract estimated gas costs
|
||||
gasCost := big.NewInt(300000) // Rough estimate
|
||||
profit = profit.Sub(profit, gasCost)
|
||||
|
||||
|
||||
// Ensure profit is positive
|
||||
if profit.Sign() <= 0 {
|
||||
return big.NewInt(0)
|
||||
}
|
||||
|
||||
|
||||
return profit
|
||||
}
|
||||
|
||||
// findTriangularArbitrageOpportunities looks for triangular arbitrage opportunities
|
||||
func (s *MarketScanner) findTriangularArbitrageOpportunities(event events.Event) []ArbitrageOpportunity {
|
||||
s.logger.Debug(fmt.Sprintf("Searching for triangular arbitrage opportunities involving pool %s", event.PoolAddress.Hex()))
|
||||
|
||||
|
||||
opportunities := make([]ArbitrageOpportunity, 0)
|
||||
|
||||
|
||||
// This would implement logic to find triangular arbitrage paths like:
|
||||
// TokenA -> TokenB -> TokenC -> TokenA
|
||||
// where the end balance of TokenA is greater than the starting balance
|
||||
|
||||
|
||||
// For now, we'll return an empty slice
|
||||
// A full implementation would:
|
||||
// 1. Identify common triangular paths (e.g., USDC -> WETH -> WBTC -> USDC)
|
||||
// 2. Calculate the output of each leg of the trade
|
||||
// 3. Account for all fees and slippage
|
||||
// 4. Compare the final amount with the initial amount
|
||||
|
||||
|
||||
return opportunities
|
||||
}
|
||||
|
||||
@@ -362,57 +362,57 @@ func (s *MarketScanner) findArbitrageOpportunities(event events.Event, movement
|
||||
|
||||
// Get related pools for the same token pair
|
||||
relatedPools := s.findRelatedPools(event.Token0, event.Token1)
|
||||
|
||||
|
||||
// If we have related pools, compare prices
|
||||
if len(relatedPools) > 0 {
|
||||
// Get the current price in this pool
|
||||
currentPrice := movement.PriceBefore
|
||||
|
||||
|
||||
// Compare with prices in related pools
|
||||
for _, pool := range relatedPools {
|
||||
// Skip the same pool
|
||||
if pool.Address == event.PoolAddress {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Get pool data
|
||||
poolData, err := s.getPoolData(pool.Address.Hex())
|
||||
if err != nil {
|
||||
s.logger.Error(fmt.Sprintf("Error getting pool data for related pool %s: %v", pool.Address.Hex(), err))
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Check if poolData.SqrtPriceX96 is nil to prevent panic
|
||||
if poolData.SqrtPriceX96 == nil {
|
||||
s.logger.Error(fmt.Sprintf("Pool data for %s has nil SqrtPriceX96", pool.Address.Hex()))
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Calculate price in the related pool
|
||||
relatedPrice := uniswap.SqrtPriceX96ToPrice(poolData.SqrtPriceX96.ToBig())
|
||||
|
||||
|
||||
// Check if currentPrice or relatedPrice is nil to prevent panic
|
||||
if currentPrice == nil || relatedPrice == nil {
|
||||
s.logger.Error(fmt.Sprintf("Nil price detected for pool comparison"))
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
// Calculate price difference
|
||||
priceDiff := new(big.Float).Sub(currentPrice, relatedPrice)
|
||||
priceDiffRatio := new(big.Float).Quo(priceDiff, relatedPrice)
|
||||
|
||||
|
||||
// If there's a significant price difference, we might have an arbitrage opportunity
|
||||
priceDiffFloat, _ := priceDiffRatio.Float64()
|
||||
if priceDiffFloat > 0.005 { // 0.5% threshold
|
||||
// Estimate potential profit
|
||||
estimatedProfit := s.estimateProfit(event, pool, priceDiffFloat)
|
||||
|
||||
|
||||
if estimatedProfit != nil && estimatedProfit.Sign() > 0 {
|
||||
opp := ArbitrageOpportunity{
|
||||
Path: []string{event.Token0.Hex(), event.Token1.Hex()},
|
||||
Pools: []string{event.PoolAddress.Hex(), pool.Address.Hex()},
|
||||
Profit: estimatedProfit,
|
||||
GasEstimate: big.NewInt(300000), // Estimated gas cost
|
||||
GasEstimate: big.NewInt(300000), // Estimated gas cost
|
||||
ROI: priceDiffFloat * 100, // Convert to percentage
|
||||
Protocol: fmt.Sprintf("%s->%s", event.Protocol, pool.Protocol),
|
||||
}
|
||||
@@ -422,7 +422,7 @@ func (s *MarketScanner) findArbitrageOpportunities(event events.Event, movement
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Also look for triangular arbitrage opportunities
|
||||
triangularOpps := s.findTriangularArbitrageOpportunities(event)
|
||||
opportunities = append(opportunities, triangularOpps...)
|
||||
@@ -469,16 +469,16 @@ type PriceMovement struct {
|
||||
|
||||
// CachedData represents cached pool data
|
||||
type CachedData struct {
|
||||
Address common.Address
|
||||
Token0 common.Address
|
||||
Token1 common.Address
|
||||
Fee int64
|
||||
Liquidity *uint256.Int
|
||||
SqrtPriceX96 *uint256.Int
|
||||
Tick int
|
||||
TickSpacing int
|
||||
LastUpdated time.Time
|
||||
Protocol string
|
||||
Address common.Address
|
||||
Token0 common.Address
|
||||
Token1 common.Address
|
||||
Fee int64
|
||||
Liquidity *uint256.Int
|
||||
SqrtPriceX96 *uint256.Int
|
||||
Tick int
|
||||
TickSpacing int
|
||||
LastUpdated time.Time
|
||||
Protocol string
|
||||
}
|
||||
|
||||
// getPoolData retrieves pool data with caching
|
||||
@@ -527,12 +527,12 @@ func (s *MarketScanner) fetchPoolData(poolAddress string) (*CachedData, error) {
|
||||
Address: address,
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), // WETH
|
||||
Fee: 3000, // 0.3%
|
||||
Liquidity: uint256.NewInt(1000000000000000000), // 1 ETH equivalent
|
||||
SqrtPriceX96: uint256.NewInt(2505414483750470000), // Mock sqrt price
|
||||
Tick: 200000, // Mock tick
|
||||
TickSpacing: 60, // Tick spacing for 0.3% fee
|
||||
Protocol: "UniswapV3", // Mock protocol
|
||||
Fee: 3000, // 0.3%
|
||||
Liquidity: uint256.NewInt(1000000000000000000), // 1 ETH equivalent
|
||||
SqrtPriceX96: uint256.NewInt(2505414483750470000), // Mock sqrt price
|
||||
Tick: 200000, // Mock tick
|
||||
TickSpacing: 60, // Tick spacing for 0.3% fee
|
||||
Protocol: "UniswapV3", // Mock protocol
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
@@ -581,4 +581,4 @@ func (s *MarketScanner) cleanupCache() {
|
||||
s.cacheMutex.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/fraktal/mev-beta/internal/config"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
"github.com/fraktal/mev-beta/pkg/events"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/holiman/uint256"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
func TestNewMarketScanner(t *testing.T) {
|
||||
// Create test config
|
||||
cfg := &config.BotConfig{
|
||||
MaxWorkers: 5,
|
||||
RPCTimeout: 30,
|
||||
MaxWorkers: 5,
|
||||
RPCTimeout: 30,
|
||||
}
|
||||
|
||||
// Create test logger
|
||||
@@ -76,12 +76,12 @@ func TestCalculatePriceMovement(t *testing.T) {
|
||||
|
||||
// Create test event
|
||||
event := events.Event{
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
|
||||
Amount0: big.NewInt(1000000000), // 1000 tokens
|
||||
Amount1: big.NewInt(500000000000000000), // 0.5 ETH
|
||||
Tick: 200000,
|
||||
Timestamp: uint64(time.Now().Unix()),
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
|
||||
Amount0: big.NewInt(1000000000), // 1000 tokens
|
||||
Amount1: big.NewInt(500000000000000000), // 0.5 ETH
|
||||
Tick: 200000,
|
||||
Timestamp: uint64(time.Now().Unix()),
|
||||
}
|
||||
|
||||
// Create test pool data
|
||||
@@ -117,7 +117,7 @@ func TestFindArbitrageOpportunities(t *testing.T) {
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
|
||||
Protocol: "UniswapV3",
|
||||
Amount0: big.NewInt(1000000000), // 1000 tokens
|
||||
Amount0: big.NewInt(1000000000), // 1000 tokens
|
||||
Amount1: big.NewInt(500000000000000000), // 0.5 ETH
|
||||
}
|
||||
|
||||
@@ -183,13 +183,13 @@ func TestUpdatePoolData(t *testing.T) {
|
||||
|
||||
// Create test event
|
||||
event := events.Event{
|
||||
PoolAddress: common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
|
||||
Liquidity: uint256.NewInt(1000000000000000000),
|
||||
PoolAddress: common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
|
||||
Liquidity: uint256.NewInt(1000000000000000000),
|
||||
SqrtPriceX96: uint256.NewInt(2505414483750470000),
|
||||
Tick: 200000,
|
||||
Timestamp: uint64(time.Now().Unix()),
|
||||
Tick: 200000,
|
||||
Timestamp: uint64(time.Now().Unix()),
|
||||
}
|
||||
|
||||
// Update pool data
|
||||
@@ -208,4 +208,4 @@ func TestUpdatePoolData(t *testing.T) {
|
||||
assert.Equal(t, event.Liquidity, poolData.Liquidity)
|
||||
assert.Equal(t, event.SqrtPriceX96, poolData.SqrtPriceX96)
|
||||
assert.Equal(t, event.Tick, poolData.Tick)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user