1064 lines
34 KiB
Go
1064 lines
34 KiB
Go
package scanner
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/big"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
"github.com/fraktal/mev-beta/internal/config"
|
|
"github.com/fraktal/mev-beta/internal/logger"
|
|
"github.com/fraktal/mev-beta/internal/tokens"
|
|
"github.com/fraktal/mev-beta/pkg/circuit"
|
|
"github.com/fraktal/mev-beta/pkg/contracts"
|
|
"github.com/fraktal/mev-beta/pkg/database"
|
|
"github.com/fraktal/mev-beta/pkg/events"
|
|
"github.com/fraktal/mev-beta/pkg/pools"
|
|
"github.com/fraktal/mev-beta/pkg/slippage"
|
|
"github.com/fraktal/mev-beta/pkg/trading"
|
|
"github.com/fraktal/mev-beta/pkg/types"
|
|
"github.com/fraktal/mev-beta/pkg/uniswap"
|
|
"github.com/holiman/uint256"
|
|
"golang.org/x/sync/singleflight"
|
|
)
|
|
|
|
// MarketScanner scans markets for price movement opportunities with concurrency
|
|
type MarketScanner struct {
|
|
config *config.BotConfig
|
|
logger *logger.Logger
|
|
workerPool chan chan events.Event
|
|
workers []*EventWorker
|
|
wg sync.WaitGroup
|
|
cacheGroup singleflight.Group
|
|
cache map[string]*CachedData
|
|
cacheMutex sync.RWMutex
|
|
cacheTTL time.Duration
|
|
slippageProtector *trading.SlippageProtection
|
|
circuitBreaker *circuit.CircuitBreaker
|
|
contractExecutor *contracts.ContractExecutor
|
|
create2Calculator *pools.CREATE2Calculator
|
|
database *database.Database
|
|
}
|
|
|
|
// EventWorker represents a worker that processes event details
|
|
type EventWorker struct {
|
|
ID int
|
|
WorkerPool chan chan events.Event
|
|
JobChannel chan events.Event
|
|
QuitChan chan bool
|
|
scanner *MarketScanner
|
|
}
|
|
|
|
// NewMarketScanner creates a new market scanner with concurrency support
|
|
func NewMarketScanner(cfg *config.BotConfig, logger *logger.Logger, contractExecutor *contracts.ContractExecutor, db *database.Database) *MarketScanner {
|
|
scanner := &MarketScanner{
|
|
config: cfg,
|
|
logger: logger,
|
|
workerPool: make(chan chan events.Event, cfg.MaxWorkers),
|
|
workers: make([]*EventWorker, 0, cfg.MaxWorkers),
|
|
cache: make(map[string]*CachedData),
|
|
cacheTTL: time.Duration(cfg.RPCTimeout) * time.Second,
|
|
slippageProtector: trading.NewSlippageProtection(logger),
|
|
circuitBreaker: circuit.NewCircuitBreaker(&circuit.Config{
|
|
Logger: logger,
|
|
Name: "market_scanner",
|
|
MaxFailures: 10,
|
|
ResetTimeout: time.Minute * 5,
|
|
MaxRequests: 3,
|
|
SuccessThreshold: 2,
|
|
}),
|
|
contractExecutor: contractExecutor,
|
|
create2Calculator: pools.NewCREATE2Calculator(logger),
|
|
database: db,
|
|
}
|
|
|
|
// Create workers
|
|
for i := 0; i < cfg.MaxWorkers; i++ {
|
|
worker := NewEventWorker(i, scanner.workerPool, scanner)
|
|
scanner.workers = append(scanner.workers, worker)
|
|
worker.Start()
|
|
}
|
|
|
|
// Start cache cleanup routine
|
|
go scanner.cleanupCache()
|
|
|
|
return scanner
|
|
}
|
|
|
|
// NewEventWorker creates a new event worker
|
|
func NewEventWorker(id int, workerPool chan chan events.Event, scanner *MarketScanner) *EventWorker {
|
|
return &EventWorker{
|
|
ID: id,
|
|
WorkerPool: workerPool,
|
|
JobChannel: make(chan events.Event),
|
|
QuitChan: make(chan bool),
|
|
scanner: scanner,
|
|
}
|
|
}
|
|
|
|
// Start begins the worker
|
|
func (w *EventWorker) Start() {
|
|
go func() {
|
|
for {
|
|
// Register the worker in the worker pool
|
|
w.WorkerPool <- w.JobChannel
|
|
|
|
select {
|
|
case job := <-w.JobChannel:
|
|
// Process the job
|
|
w.Process(job)
|
|
case <-w.QuitChan:
|
|
// Stop the worker
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
}
|
|
|
|
// Stop terminates the worker
|
|
func (w *EventWorker) Stop() {
|
|
go func() {
|
|
w.QuitChan <- true
|
|
}()
|
|
}
|
|
|
|
// Process handles an event detail
|
|
func (w *EventWorker) Process(event events.Event) {
|
|
// Analyze the event in a separate goroutine to maintain throughput
|
|
go func() {
|
|
defer w.scanner.wg.Done()
|
|
|
|
// Log the processing
|
|
w.scanner.logger.Debug(fmt.Sprintf("Worker %d processing %s event in pool %s from protocol %s",
|
|
w.ID, event.Type.String(), event.PoolAddress, event.Protocol))
|
|
|
|
// Analyze based on event type
|
|
switch event.Type {
|
|
case events.Swap:
|
|
w.scanner.analyzeSwapEvent(event)
|
|
case events.AddLiquidity:
|
|
w.scanner.analyzeLiquidityEvent(event, true)
|
|
case events.RemoveLiquidity:
|
|
w.scanner.analyzeLiquidityEvent(event, false)
|
|
case events.NewPool:
|
|
w.scanner.analyzeNewPoolEvent(event)
|
|
default:
|
|
w.scanner.logger.Debug(fmt.Sprintf("Worker %d received unknown event type: %d", w.ID, event.Type))
|
|
}
|
|
}()
|
|
}
|
|
|
|
// SubmitEvent submits an event for processing by the worker pool
|
|
func (s *MarketScanner) SubmitEvent(event events.Event) {
|
|
s.wg.Add(1)
|
|
|
|
// Get an available worker job channel
|
|
jobChannel := <-s.workerPool
|
|
|
|
// Send the job to the worker
|
|
jobChannel <- event
|
|
}
|
|
|
|
// analyzeSwapEvent analyzes a swap event for arbitrage opportunities
|
|
func (s *MarketScanner) analyzeSwapEvent(event events.Event) {
|
|
s.logger.Debug(fmt.Sprintf("Analyzing swap event in pool %s", event.PoolAddress))
|
|
|
|
// Log the swap event to database
|
|
s.logSwapEvent(event)
|
|
|
|
// Get pool data with caching
|
|
poolData, err := s.getPoolData(event.PoolAddress.Hex())
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("Error getting pool data for %s: %v", event.PoolAddress, err))
|
|
return
|
|
}
|
|
|
|
// Calculate price impact
|
|
priceMovement, err := s.calculatePriceMovement(event, poolData)
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("Error calculating price movement for pool %s: %v", event.PoolAddress, err))
|
|
return
|
|
}
|
|
|
|
// Check if the movement is significant
|
|
if s.isSignificantMovement(priceMovement, s.config.MinProfitThreshold) {
|
|
s.logger.Info(fmt.Sprintf("Significant price movement detected in pool %s: %+v", event.PoolAddress, priceMovement))
|
|
|
|
// Look for arbitrage opportunities
|
|
opportunities := s.findArbitrageOpportunities(event, priceMovement)
|
|
if len(opportunities) > 0 {
|
|
s.logger.Info(fmt.Sprintf("Found %d arbitrage opportunities for pool %s", len(opportunities), event.PoolAddress))
|
|
for _, opp := range opportunities {
|
|
s.logger.Info(fmt.Sprintf("Arbitrage opportunity: %+v", opp))
|
|
// Execute the arbitrage opportunity
|
|
s.executeArbitrageOpportunity(opp)
|
|
}
|
|
}
|
|
} else {
|
|
s.logger.Debug(fmt.Sprintf("Price movement in pool %s is not significant: %f", event.PoolAddress, priceMovement.PriceImpact))
|
|
}
|
|
}
|
|
|
|
// analyzeLiquidityEvent analyzes liquidity events (add/remove)
|
|
func (s *MarketScanner) analyzeLiquidityEvent(event events.Event, isAdd bool) {
|
|
action := "adding"
|
|
if !isAdd {
|
|
action = "removing"
|
|
}
|
|
s.logger.Debug(fmt.Sprintf("Analyzing liquidity event (%s) in pool %s", action, event.PoolAddress))
|
|
|
|
// Log the liquidity event to database
|
|
eventType := "add"
|
|
if !isAdd {
|
|
eventType = "remove"
|
|
}
|
|
s.logLiquidityEvent(event, eventType)
|
|
|
|
// Update cached pool data
|
|
s.updatePoolData(event)
|
|
|
|
s.logger.Info(fmt.Sprintf("Liquidity %s event processed for pool %s", action, event.PoolAddress))
|
|
}
|
|
|
|
// analyzeNewPoolEvent analyzes new pool creation events
|
|
func (s *MarketScanner) analyzeNewPoolEvent(event events.Event) {
|
|
s.logger.Info(fmt.Sprintf("New pool created: %s (protocol: %s)", event.PoolAddress, event.Protocol))
|
|
|
|
// Add to known pools by fetching and caching the pool data
|
|
s.logger.Debug(fmt.Sprintf("Adding new pool %s to monitoring", event.PoolAddress))
|
|
|
|
// Fetch pool data to validate it's a real pool
|
|
poolData, err := s.getPoolData(event.PoolAddress.Hex())
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("Failed to fetch data for new pool %s: %v", event.PoolAddress, err))
|
|
return
|
|
}
|
|
|
|
// Validate that this is a real pool contract
|
|
if poolData.Address == (common.Address{}) {
|
|
s.logger.Warn(fmt.Sprintf("Invalid pool contract at address %s", event.PoolAddress.Hex()))
|
|
return
|
|
}
|
|
|
|
// Log pool data to database
|
|
s.logPoolData(poolData)
|
|
|
|
s.logger.Info(fmt.Sprintf("Successfully added new pool %s to monitoring (tokens: %s-%s, fee: %d)",
|
|
event.PoolAddress.Hex(), poolData.Token0.Hex(), poolData.Token1.Hex(), poolData.Fee))
|
|
}
|
|
|
|
// isSignificantMovement determines if a price movement is significant enough to exploit
|
|
func (s *MarketScanner) isSignificantMovement(movement *PriceMovement, threshold float64) bool {
|
|
// Check if the price impact is above our 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)
|
|
|
|
// Use dynamic pool discovery by checking known DEX factories
|
|
poolAddresses := s.discoverPoolsForPair(token0, token1)
|
|
|
|
s.logger.Debug(fmt.Sprintf("Found %d potential pools for pair %s-%s", len(poolAddresses), token0.Hex(), token1.Hex()))
|
|
|
|
for _, poolAddr := range poolAddresses {
|
|
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) {
|
|
relatedPools = append(relatedPools, poolData)
|
|
}
|
|
}
|
|
|
|
s.logger.Debug(fmt.Sprintf("Found %d related pools", len(relatedPools)))
|
|
return relatedPools
|
|
}
|
|
|
|
// discoverPoolsForPair discovers pools for a specific token pair using real factory contracts
|
|
func (s *MarketScanner) discoverPoolsForPair(token0, token1 common.Address) []string {
|
|
poolAddresses := make([]string, 0)
|
|
|
|
// Use the CREATE2 calculator to find all possible pools
|
|
pools, err := s.create2Calculator.FindPoolsForTokenPair(token0, token1)
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("Failed to discover pools for pair %s/%s: %v", token0.Hex(), token1.Hex(), err))
|
|
return poolAddresses
|
|
}
|
|
|
|
// Convert to string addresses
|
|
for _, pool := range pools {
|
|
poolAddresses = append(poolAddresses, pool.PoolAddr.Hex())
|
|
}
|
|
|
|
s.logger.Debug(fmt.Sprintf("Discovered %d potential pools for pair %s/%s", len(poolAddresses), token0.Hex(), token1.Hex()))
|
|
return poolAddresses
|
|
}
|
|
|
|
// estimateProfit estimates the potential profit from an arbitrage opportunity using real slippage protection
|
|
func (s *MarketScanner) estimateProfit(event events.Event, pool *CachedData, priceDiff float64) *big.Int {
|
|
// Use comprehensive slippage analysis instead of simplified calculation
|
|
if s.slippageProtector != nil {
|
|
return s.calculateProfitWithSlippageProtection(event, pool, priceDiff)
|
|
}
|
|
|
|
// Fallback to simplified calculation if slippage protection not available
|
|
return s.calculateSimplifiedProfit(event, pool, priceDiff)
|
|
}
|
|
|
|
// calculateProfitWithSlippageProtection uses slippage protection for accurate profit estimation
|
|
func (s *MarketScanner) calculateProfitWithSlippageProtection(event events.Event, pool *CachedData, priceDiff float64) *big.Int {
|
|
// Create trade parameters from event data
|
|
tradeParams := &slippage.TradeParams{
|
|
TokenIn: event.Token0,
|
|
TokenOut: event.Token1,
|
|
AmountIn: event.Amount0,
|
|
PoolAddress: event.PoolAddress,
|
|
Fee: big.NewInt(3000), // Assume 0.3% fee for now
|
|
Deadline: uint64(time.Now().Add(5 * time.Minute).Unix()),
|
|
}
|
|
|
|
// Analyze slippage with timeout
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
slippageResult, err := s.slippageProtector.AnalyzeSlippage(ctx, tradeParams)
|
|
if err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Slippage analysis failed: %v", err))
|
|
return s.calculateSimplifiedProfit(event, pool, priceDiff)
|
|
}
|
|
|
|
// Don't proceed if trade is not safe
|
|
if !slippageResult.SafeToExecute || slippageResult.EmergencyStop {
|
|
s.logger.Debug("Trade rejected by slippage protection")
|
|
return big.NewInt(0)
|
|
}
|
|
|
|
// Calculate profit considering slippage
|
|
expectedAmountOut := slippageResult.MaxAllowedAmountOut
|
|
minAmountOut := slippageResult.MinRequiredAmountOut
|
|
|
|
// Profit = (expected_out - amount_in) - gas_costs - slippage_buffer
|
|
profit := new(big.Int).Sub(expectedAmountOut, event.Amount0)
|
|
|
|
// REAL gas cost calculation for competitive MEV on Arbitrum
|
|
// Base gas: 800k units, Price: 1.5 gwei, MEV premium: 15x = 0.018 ETH total
|
|
baseGas := big.NewInt(800000) // 800k gas units for flash swap arbitrage
|
|
gasPrice := big.NewInt(1500000000) // 1.5 gwei base price on Arbitrum
|
|
mevPremium := big.NewInt(15) // 15x premium for MEV competition
|
|
|
|
gasCostWei := new(big.Int).Mul(baseGas, gasPrice)
|
|
totalGasCost := new(big.Int).Mul(gasCostWei, mevPremium)
|
|
|
|
profit.Sub(profit, totalGasCost)
|
|
|
|
// Apply safety margin for slippage
|
|
slippageMargin := new(big.Int).Sub(expectedAmountOut, minAmountOut)
|
|
profit.Sub(profit, slippageMargin)
|
|
|
|
// Ensure profit is not negative
|
|
if profit.Sign() < 0 {
|
|
return big.NewInt(0)
|
|
}
|
|
|
|
return profit
|
|
}
|
|
|
|
// calculateSimplifiedProfit provides fallback profit calculation
|
|
func (s *MarketScanner) calculateSimplifiedProfit(event events.Event, pool *CachedData, priceDiff float64) *big.Int {
|
|
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))
|
|
|
|
// REAL gas costs for multi-hop arbitrage
|
|
baseGas := big.NewInt(1200000) // 1.2M gas for complex multi-hop
|
|
gasPrice := big.NewInt(1500000000) // 1.5 gwei
|
|
mevPremium := big.NewInt(20) // 20x premium for multi-hop MEV
|
|
|
|
totalGasCost := new(big.Int).Mul(new(big.Int).Mul(baseGas, gasPrice), mevPremium)
|
|
profit = profit.Sub(profit, totalGasCost)
|
|
|
|
// 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) []types.ArbitrageOpportunity {
|
|
s.logger.Debug(fmt.Sprintf("Searching for triangular arbitrage opportunities involving pool %s", event.PoolAddress))
|
|
|
|
opportunities := make([]types.ArbitrageOpportunity, 0)
|
|
|
|
// Define common triangular paths on Arbitrum
|
|
// Get triangular arbitrage paths from token configuration
|
|
triangularPaths := tokens.GetTriangularPaths()
|
|
|
|
// Check if the event involves any tokens from our triangular paths
|
|
eventInvolvesPaths := make([]int, 0)
|
|
for i, path := range triangularPaths {
|
|
for _, token := range path.Tokens {
|
|
if token == event.Token0 || token == event.Token1 {
|
|
eventInvolvesPaths = append(eventInvolvesPaths, i)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
// For each relevant triangular path, calculate potential profit
|
|
for _, pathIdx := range eventInvolvesPaths {
|
|
path := triangularPaths[pathIdx]
|
|
|
|
// Define test amounts for arbitrage calculation
|
|
testAmounts := []*big.Int{
|
|
big.NewInt(1000000), // 1 USDC (6 decimals)
|
|
big.NewInt(100000000), // 0.1 WETH (18 decimals)
|
|
big.NewInt(10000000), // 0.01 WETH (18 decimals)
|
|
}
|
|
|
|
for _, testAmount := range testAmounts {
|
|
profit, gasEstimate, err := s.calculateTriangularProfit(path.Tokens, testAmount)
|
|
if err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Error calculating triangular profit for %s: %v", path.Name, err))
|
|
continue
|
|
}
|
|
|
|
// Check if profitable after gas costs
|
|
netProfit := new(big.Int).Sub(profit, gasEstimate)
|
|
if netProfit.Sign() > 0 {
|
|
// Calculate ROI
|
|
roi := 0.0
|
|
if testAmount.Sign() > 0 {
|
|
roiFloat := new(big.Float).Quo(new(big.Float).SetInt(netProfit), new(big.Float).SetInt(testAmount))
|
|
roi, _ = roiFloat.Float64()
|
|
roi *= 100 // Convert to percentage
|
|
}
|
|
|
|
// Create arbitrage opportunity
|
|
tokenPaths := make([]string, len(path.Tokens))
|
|
for i, token := range path.Tokens {
|
|
tokenPaths[i] = token.Hex()
|
|
}
|
|
// Close the loop by adding the first token at the end
|
|
tokenPaths = append(tokenPaths, path.Tokens[0].Hex())
|
|
|
|
opportunity := types.ArbitrageOpportunity{
|
|
Path: tokenPaths,
|
|
Pools: []string{}, // Pool addresses will be discovered dynamically
|
|
Profit: netProfit,
|
|
GasEstimate: gasEstimate,
|
|
ROI: roi,
|
|
Protocol: fmt.Sprintf("Triangular_%s", path.Name),
|
|
}
|
|
|
|
opportunities = append(opportunities, opportunity)
|
|
s.logger.Info(fmt.Sprintf("Found triangular arbitrage opportunity: %s, Profit: %s, ROI: %.2f%%",
|
|
path.Name, netProfit.String(), roi))
|
|
}
|
|
}
|
|
}
|
|
|
|
return opportunities
|
|
}
|
|
|
|
// calculateTriangularProfit calculates the profit from a triangular arbitrage path
|
|
func (s *MarketScanner) calculateTriangularProfit(tokens []common.Address, initialAmount *big.Int) (*big.Int, *big.Int, error) {
|
|
if len(tokens) < 3 {
|
|
return nil, nil, fmt.Errorf("triangular arbitrage requires at least 3 tokens")
|
|
}
|
|
|
|
currentAmount := new(big.Int).Set(initialAmount)
|
|
totalGasCost := big.NewInt(0)
|
|
|
|
// Simulate trading through the triangular path
|
|
for i := 0; i < len(tokens); i++ {
|
|
nextIndex := (i + 1) % len(tokens)
|
|
tokenIn := tokens[i]
|
|
tokenOut := tokens[nextIndex]
|
|
|
|
// Get pools that trade this token pair
|
|
relatedPools := s.findRelatedPools(tokenIn, tokenOut)
|
|
if len(relatedPools) == 0 {
|
|
// No pools found for this pair, use estimation
|
|
// Apply a 0.3% fee reduction as approximation
|
|
currentAmount = new(big.Int).Mul(currentAmount, big.NewInt(997))
|
|
currentAmount = new(big.Int).Div(currentAmount, big.NewInt(1000))
|
|
} else {
|
|
// Use the best pool for this trade
|
|
bestPool := relatedPools[0]
|
|
|
|
// Calculate swap output using current amount
|
|
outputAmount, err := s.calculateSwapOutput(currentAmount, bestPool, tokenIn, tokenOut)
|
|
if err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Error calculating swap output: %v", err))
|
|
// Fallback to simple fee calculation
|
|
currentAmount = new(big.Int).Mul(currentAmount, big.NewInt(997))
|
|
currentAmount = new(big.Int).Div(currentAmount, big.NewInt(1000))
|
|
} else {
|
|
currentAmount = outputAmount
|
|
}
|
|
}
|
|
|
|
// Add gas cost for this hop (estimated)
|
|
hopGas := big.NewInt(150000) // ~150k gas per swap
|
|
totalGasCost.Add(totalGasCost, hopGas)
|
|
}
|
|
|
|
// Calculate profit (final amount - initial amount)
|
|
profit := new(big.Int).Sub(currentAmount, initialAmount)
|
|
|
|
return profit, totalGasCost, nil
|
|
}
|
|
|
|
// calculateSwapOutput calculates the output amount for a token swap
|
|
func (s *MarketScanner) calculateSwapOutput(amountIn *big.Int, pool *CachedData, tokenIn, tokenOut common.Address) (*big.Int, error) {
|
|
if pool.SqrtPriceX96 == nil || pool.Liquidity == nil {
|
|
return nil, fmt.Errorf("missing pool price or liquidity data")
|
|
}
|
|
|
|
// Convert sqrtPriceX96 to price for calculation
|
|
price := uniswap.SqrtPriceX96ToPrice(pool.SqrtPriceX96.ToBig())
|
|
|
|
// Simple approximation: apply price and fee
|
|
amountInFloat := new(big.Float).SetInt(amountIn)
|
|
var amountOut *big.Float
|
|
|
|
if tokenIn == pool.Token0 {
|
|
// Token0 -> Token1: multiply by price
|
|
amountOut = new(big.Float).Mul(amountInFloat, price)
|
|
} else {
|
|
// Token1 -> Token0: divide by price
|
|
amountOut = new(big.Float).Quo(amountInFloat, price)
|
|
}
|
|
|
|
// Apply fee (assume 0.3% for simplicity)
|
|
feeRate := big.NewFloat(0.997) // 1 - 0.003
|
|
amountOut.Mul(amountOut, feeRate)
|
|
|
|
// Convert back to big.Int
|
|
result := new(big.Int)
|
|
amountOut.Int(result)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// findArbitrageOpportunities looks for arbitrage opportunities based on price movements
|
|
func (s *MarketScanner) findArbitrageOpportunities(event events.Event, movement *PriceMovement) []types.ArbitrageOpportunity {
|
|
s.logger.Debug(fmt.Sprintf("Searching for arbitrage opportunities for pool %s", event.PoolAddress))
|
|
|
|
opportunities := make([]types.ArbitrageOpportunity, 0)
|
|
|
|
// 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 := types.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
|
|
ROI: priceDiffFloat * 100, // Convert to percentage
|
|
Protocol: fmt.Sprintf("%s->%s", event.Protocol, pool.Protocol),
|
|
}
|
|
opportunities = append(opportunities, opp)
|
|
s.logger.Info(fmt.Sprintf("Found arbitrage opportunity: %+v", opp))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Also look for triangular arbitrage opportunities
|
|
triangularOpps := s.findTriangularArbitrageOpportunities(event)
|
|
opportunities = append(opportunities, triangularOpps...)
|
|
|
|
return opportunities
|
|
}
|
|
|
|
// executeArbitrageOpportunity executes an arbitrage opportunity using the smart contract
|
|
func (s *MarketScanner) executeArbitrageOpportunity(opportunity types.ArbitrageOpportunity) {
|
|
// Check if contract executor is available
|
|
if s.contractExecutor == nil {
|
|
s.logger.Warn("Contract executor not available, skipping arbitrage execution")
|
|
return
|
|
}
|
|
|
|
// Only execute opportunities with sufficient profit
|
|
minProfitThreshold := big.NewInt(10000000000000000) // 0.01 ETH minimum profit
|
|
if opportunity.Profit.Cmp(minProfitThreshold) < 0 {
|
|
s.logger.Debug(fmt.Sprintf("Arbitrage opportunity profit too low: %s < %s",
|
|
opportunity.Profit.String(), minProfitThreshold.String()))
|
|
return
|
|
}
|
|
|
|
s.logger.Info(fmt.Sprintf("Executing arbitrage opportunity with profit: %s", opportunity.Profit.String()))
|
|
|
|
// Execute the arbitrage opportunity
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
var tx *types.Transaction
|
|
var err error
|
|
|
|
// Determine if this is a triangular arbitrage or standard arbitrage
|
|
if len(opportunity.Path) == 3 && len(opportunity.Pools) == 3 {
|
|
// Triangular arbitrage
|
|
tx, err = s.contractExecutor.ExecuteTriangularArbitrage(ctx, opportunity)
|
|
} else {
|
|
// Standard arbitrage
|
|
tx, err = s.contractExecutor.ExecuteArbitrage(ctx, opportunity)
|
|
}
|
|
|
|
if err != nil {
|
|
s.logger.Error(fmt.Sprintf("Failed to execute arbitrage opportunity: %v", err))
|
|
return
|
|
}
|
|
|
|
s.logger.Info(fmt.Sprintf("Arbitrage transaction submitted: %s", tx.Hash().Hex()))
|
|
}
|
|
|
|
// logSwapEvent logs a swap event to the database
|
|
func (s *MarketScanner) logSwapEvent(event events.Event) {
|
|
if s.database == nil {
|
|
return // Database not available
|
|
}
|
|
|
|
// Convert event to database record
|
|
swapEvent := &database.SwapEvent{
|
|
Timestamp: time.Now(),
|
|
BlockNumber: event.BlockNumber,
|
|
TxHash: event.TxHash,
|
|
PoolAddress: event.PoolAddress,
|
|
Token0: event.Token0,
|
|
Token1: event.Token1,
|
|
Amount0In: event.Amount0,
|
|
Amount1In: event.Amount1,
|
|
Amount0Out: big.NewInt(0), // Would need to calculate from event data
|
|
Amount1Out: big.NewInt(0), // Would need to calculate from event data
|
|
Sender: common.Address{}, // Would need to extract from transaction
|
|
Recipient: common.Address{}, // Would need to extract from transaction
|
|
Protocol: event.Protocol,
|
|
}
|
|
|
|
// Log the swap event asynchronously to avoid blocking
|
|
go func() {
|
|
if err := s.database.InsertSwapEvent(swapEvent); err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Failed to log swap event: %v", err))
|
|
}
|
|
}()
|
|
}
|
|
|
|
// logLiquidityEvent logs a liquidity event to the database
|
|
func (s *MarketScanner) logLiquidityEvent(event events.Event, eventType string) {
|
|
if s.database == nil {
|
|
return // Database not available
|
|
}
|
|
|
|
// Convert event to database record
|
|
liquidityEvent := &database.LiquidityEvent{
|
|
Timestamp: time.Now(),
|
|
BlockNumber: event.BlockNumber,
|
|
TxHash: event.TxHash,
|
|
PoolAddress: event.PoolAddress,
|
|
Token0: event.Token0,
|
|
Token1: event.Token1,
|
|
Liquidity: event.Liquidity,
|
|
Amount0: event.Amount0,
|
|
Amount1: event.Amount1,
|
|
Sender: common.Address{}, // Would need to extract from transaction
|
|
Recipient: common.Address{}, // Would need to extract from transaction
|
|
EventType: eventType,
|
|
Protocol: event.Protocol,
|
|
}
|
|
|
|
// Log the liquidity event asynchronously to avoid blocking
|
|
go func() {
|
|
if err := s.database.InsertLiquidityEvent(liquidityEvent); err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Failed to log liquidity event: %v", err))
|
|
}
|
|
}()
|
|
}
|
|
|
|
// logPoolData logs pool data to the database
|
|
func (s *MarketScanner) logPoolData(poolData *CachedData) {
|
|
if s.database == nil {
|
|
return // Database not available
|
|
}
|
|
|
|
// Convert cached data to database record
|
|
dbPoolData := &database.PoolData{
|
|
Address: poolData.Address,
|
|
Token0: poolData.Token0,
|
|
Token1: poolData.Token1,
|
|
Fee: poolData.Fee,
|
|
Liquidity: poolData.Liquidity.ToBig(),
|
|
SqrtPriceX96: poolData.SqrtPriceX96.ToBig(),
|
|
Tick: int64(poolData.Tick),
|
|
LastUpdated: time.Now(),
|
|
Protocol: poolData.Protocol,
|
|
}
|
|
|
|
// Log the pool data asynchronously to avoid blocking
|
|
go func() {
|
|
if err := s.database.InsertPoolData(dbPoolData); err != nil {
|
|
s.logger.Debug(fmt.Sprintf("Failed to log pool data: %v", err))
|
|
}
|
|
}()
|
|
}
|
|
|
|
// PriceMovement represents a potential price movement
|
|
type PriceMovement struct {
|
|
Token0 string // Token address
|
|
Token1 string // Token address
|
|
Pool string // Pool address
|
|
Protocol string // DEX protocol
|
|
AmountIn *big.Int // Amount of token being swapped in
|
|
AmountOut *big.Int // Amount of token being swapped out
|
|
PriceBefore *big.Float // Price before the swap
|
|
PriceAfter *big.Float // Price after the swap (to be calculated)
|
|
PriceImpact float64 // Calculated price impact
|
|
TickBefore int // Tick before the swap
|
|
TickAfter int // Tick after the swap (to be calculated)
|
|
Timestamp time.Time // Event timestamp
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// getPoolData retrieves pool data with caching
|
|
func (s *MarketScanner) getPoolData(poolAddress string) (*CachedData, error) {
|
|
// Check cache first
|
|
cacheKey := fmt.Sprintf("pool_%s", poolAddress)
|
|
|
|
s.cacheMutex.RLock()
|
|
if data, exists := s.cache[cacheKey]; exists && time.Since(data.LastUpdated) < s.cacheTTL {
|
|
s.cacheMutex.RUnlock()
|
|
s.logger.Debug(fmt.Sprintf("Cache hit for pool %s", poolAddress))
|
|
return data, nil
|
|
}
|
|
s.cacheMutex.RUnlock()
|
|
|
|
// Use singleflight to prevent duplicate requests
|
|
result, err, _ := s.cacheGroup.Do(cacheKey, func() (interface{}, error) {
|
|
return s.fetchPoolData(poolAddress)
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
poolData := result.(*CachedData)
|
|
|
|
// Update cache
|
|
s.cacheMutex.Lock()
|
|
s.cache[cacheKey] = poolData
|
|
s.cacheMutex.Unlock()
|
|
|
|
s.logger.Debug(fmt.Sprintf("Fetched and cached pool data for %s", poolAddress))
|
|
return poolData, nil
|
|
}
|
|
|
|
// fetchPoolData fetches pool data from the blockchain
|
|
func (s *MarketScanner) fetchPoolData(poolAddress string) (*CachedData, error) {
|
|
s.logger.Debug(fmt.Sprintf("Fetching pool data for %s", poolAddress))
|
|
|
|
address := common.HexToAddress(poolAddress)
|
|
|
|
// In test environment, return mock data to avoid network calls
|
|
if s.isTestEnvironment() {
|
|
return s.getMockPoolData(poolAddress), nil
|
|
}
|
|
|
|
// Create RPC client connection
|
|
// Get RPC endpoint from config or environment
|
|
rpcEndpoint := os.Getenv("ARBITRUM_RPC_ENDPOINT")
|
|
if rpcEndpoint == "" {
|
|
rpcEndpoint = "wss://arbitrum-mainnet.core.chainstack.com/f69d14406bc00700da9b936504e1a870" // fallback
|
|
}
|
|
client, err := ethclient.Dial(rpcEndpoint)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to Ethereum node: %w", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
// Create Uniswap V3 pool interface
|
|
pool := uniswap.NewUniswapV3Pool(address, client)
|
|
|
|
// Validate that this is a real pool contract
|
|
if !uniswap.IsValidPool(context.Background(), client, address) {
|
|
return nil, fmt.Errorf("invalid pool contract at address %s", address.Hex())
|
|
}
|
|
|
|
// Fetch real pool state from the blockchain
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
poolState, err := pool.GetPoolState(ctx)
|
|
if err != nil {
|
|
s.logger.Warn(fmt.Sprintf("Failed to fetch real pool state for %s: %v", address.Hex(), err))
|
|
return nil, fmt.Errorf("failed to fetch pool state: %w", err)
|
|
}
|
|
|
|
// Determine tick spacing based on fee tier
|
|
tickSpacing := 60 // Default for 0.3% fee
|
|
switch poolState.Fee {
|
|
case 100: // 0.01%
|
|
tickSpacing = 1
|
|
case 500: // 0.05%
|
|
tickSpacing = 10
|
|
case 3000: // 0.3%
|
|
tickSpacing = 60
|
|
case 10000: // 1%
|
|
tickSpacing = 200
|
|
}
|
|
|
|
// Determine protocol (assume UniswapV3 for now, could be enhanced to detect protocol)
|
|
protocol := "UniswapV3"
|
|
|
|
// Create pool data from real blockchain state
|
|
poolData := &CachedData{
|
|
Address: address,
|
|
Token0: poolState.Token0,
|
|
Token1: poolState.Token1,
|
|
Fee: poolState.Fee,
|
|
Liquidity: poolState.Liquidity,
|
|
SqrtPriceX96: poolState.SqrtPriceX96,
|
|
Tick: poolState.Tick,
|
|
TickSpacing: tickSpacing,
|
|
Protocol: protocol,
|
|
LastUpdated: time.Now(),
|
|
}
|
|
|
|
s.logger.Info(fmt.Sprintf("Fetched real pool data for %s: Token0=%s, Token1=%s, Fee=%d, Liquidity=%s",
|
|
address.Hex(), poolState.Token0.Hex(), poolState.Token1.Hex(), poolState.Fee, poolState.Liquidity.String()))
|
|
|
|
return poolData, nil
|
|
}
|
|
|
|
// updatePoolData updates cached pool data from an event
|
|
func (s *MarketScanner) updatePoolData(event events.Event) {
|
|
poolKey := event.PoolAddress.Hex()
|
|
|
|
s.cacheMutex.Lock()
|
|
defer s.cacheMutex.Unlock()
|
|
|
|
// Update existing cache entry or create new one
|
|
if pool, exists := s.cache[poolKey]; exists {
|
|
// Update liquidity if provided
|
|
if event.Liquidity != nil {
|
|
pool.Liquidity = event.Liquidity
|
|
}
|
|
|
|
// Update sqrtPriceX96 if provided
|
|
if event.SqrtPriceX96 != nil {
|
|
pool.SqrtPriceX96 = event.SqrtPriceX96
|
|
}
|
|
|
|
// Update tick if provided
|
|
if event.Tick != 0 {
|
|
pool.Tick = event.Tick
|
|
}
|
|
|
|
// Update last updated time
|
|
pool.LastUpdated = time.Now()
|
|
|
|
// Log updated pool data to database
|
|
s.logPoolData(pool)
|
|
} else {
|
|
// Create new pool entry
|
|
pool := &CachedData{
|
|
Address: event.PoolAddress,
|
|
Token0: event.Token0,
|
|
Token1: event.Token1,
|
|
Fee: event.Fee,
|
|
Liquidity: event.Liquidity,
|
|
SqrtPriceX96: event.SqrtPriceX96,
|
|
Tick: event.Tick,
|
|
TickSpacing: getTickSpacing(event.Fee),
|
|
Protocol: event.Protocol,
|
|
LastUpdated: time.Now(),
|
|
}
|
|
|
|
s.cache[poolKey] = pool
|
|
|
|
// Log new pool data to database
|
|
s.logPoolData(pool)
|
|
}
|
|
|
|
s.logger.Debug(fmt.Sprintf("Updated cache for pool %s", event.PoolAddress.Hex()))
|
|
}
|
|
|
|
// cleanupCache removes expired cache entries
|
|
func (s *MarketScanner) cleanupCache() {
|
|
ticker := time.NewTicker(10 * time.Minute)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
s.cacheMutex.Lock()
|
|
for key, data := range s.cache {
|
|
if time.Since(data.LastUpdated) > s.cacheTTL {
|
|
delete(s.cache, key)
|
|
s.logger.Debug(fmt.Sprintf("Removed expired cache entry: %s", key))
|
|
}
|
|
}
|
|
s.cacheMutex.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
// isTestEnvironment checks if we're running in a test environment
|
|
func (s *MarketScanner) isTestEnvironment() bool {
|
|
// Check for explicit test environment variable
|
|
if os.Getenv("GO_TEST") == "true" {
|
|
return true
|
|
}
|
|
|
|
// Check for testing framework flags
|
|
for _, arg := range os.Args {
|
|
if strings.HasPrefix(arg, "-test.") || arg == "test" {
|
|
return true
|
|
}
|
|
}
|
|
|
|
// Check if the program name is from 'go test'
|
|
progName := os.Args[0]
|
|
if strings.Contains(progName, ".test") || strings.HasSuffix(progName, ".test") {
|
|
return true
|
|
}
|
|
|
|
// Check if running under go test command
|
|
if strings.Contains(progName, "go_build_") && strings.Contains(progName, "_test") {
|
|
return true
|
|
}
|
|
|
|
// Default to production mode - NEVER return true by default
|
|
return false
|
|
}
|
|
|
|
// getMockPoolData returns mock pool data for testing
|
|
func (s *MarketScanner) getMockPoolData(poolAddress string) *CachedData {
|
|
// Create deterministic mock data based on pool address
|
|
mockTokens := tokens.GetArbitrumTokens()
|
|
|
|
// Use different token pairs based on pool address
|
|
var token0, token1 common.Address
|
|
switch poolAddress {
|
|
case "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640":
|
|
token0 = mockTokens.USDC
|
|
token1 = mockTokens.WETH
|
|
case "0xB4e16d0168e52d35CaCD2c6185b44281Ec28C9Dc":
|
|
token0 = mockTokens.USDC
|
|
token1 = mockTokens.WETH
|
|
default:
|
|
token0 = mockTokens.USDC
|
|
token1 = mockTokens.WETH
|
|
}
|
|
|
|
// Convert big.Int to uint256.Int for compatibility
|
|
liquidity := uint256.NewInt(1000000000000000000) // 1 ETH equivalent
|
|
|
|
// Create a reasonable sqrtPriceX96 value for ~2000 USDC per ETH
|
|
sqrtPrice, _ := uint256.FromHex("0x668F0BD9C5DB9D2F2DF6A0E4C") // Reasonable value
|
|
|
|
return &CachedData{
|
|
Address: common.HexToAddress(poolAddress),
|
|
Token0: token0,
|
|
Token1: token1,
|
|
Fee: 3000, // 0.3%
|
|
TickSpacing: 60,
|
|
Liquidity: liquidity,
|
|
SqrtPriceX96: sqrtPrice,
|
|
Tick: -74959, // Corresponds to the sqrt price above
|
|
Protocol: "UniswapV3",
|
|
LastUpdated: time.Now(),
|
|
}
|
|
}
|