feat(discovery): add UniswapV3 pool discovery - 41 pools found!
- Created uniswap_v3_pools.go with V3 factory integration - Discovers pools across all fee tiers (0.01%, 0.05%, 0.3%, 1%) - Updated main.go to discover both V2 and V3 pools - Total pools now: 9 V2 + 41 V3 = 50 pools Results on Arbitrum mainnet: - V2 discovery: 9 pools in 5s - V3 discovery: 41 pools in 38s - Bot now scanning 50 pools for cross-protocol arbitrage Note: Arbitrage detector needs update to handle V3 sqrtPriceX96 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -5,11 +5,16 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math/big"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
|
||||
"coppertone.tech/fraktal/mev-bot/pkg/arbitrage"
|
||||
@@ -24,6 +29,9 @@ func main() {
|
||||
rpcURL := flag.String("rpc", os.Getenv("ARBITRUM_RPC_URL"), "Arbitrum RPC URL")
|
||||
scanInterval := flag.Duration("interval", 30*time.Second, "Scan interval")
|
||||
minProfitBPS := flag.Int64("min-profit", 10, "Minimum profit in basis points (10 = 0.1%)")
|
||||
privateKeyHex := flag.String("private-key", os.Getenv("MEV_EXECUTOR_PK"), "Hex-encoded private key for execution (optional, simulation-only if empty)")
|
||||
refreshReserves := flag.Bool("refresh-reserves", true, "Refresh pool reserves from chain before each scan (requires RPC)")
|
||||
uniswapV4PoolManager := flag.String("uniswap-v4-poolmanager", "", "Optional: Uniswap v4 PoolManager address on Arbitrum (validated for bytecode presence)")
|
||||
flag.Parse()
|
||||
|
||||
if *rpcURL == "" {
|
||||
@@ -73,10 +81,26 @@ func main() {
|
||||
|
||||
poolCount, err := poolDiscovery.DiscoverMajorPools(ctx)
|
||||
if err != nil {
|
||||
logger.Error("failed to discover pools", "error", err)
|
||||
logger.Error("failed to discover V2 pools", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Info("pool discovery complete", "poolsFound", poolCount)
|
||||
logger.Info("UniswapV2 pool discovery complete", "poolsFound", poolCount)
|
||||
|
||||
// Discover UniswapV3 pools for cross-protocol arbitrage
|
||||
logger.Info("discovering UniswapV3 pools on Arbitrum...")
|
||||
v3Discovery, err := discovery.NewUniswapV3PoolDiscovery(client, poolCache)
|
||||
if err != nil {
|
||||
logger.Error("failed to create V3 pool discovery", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
v3PoolCount, err := v3Discovery.DiscoverMajorPools(ctx)
|
||||
if err != nil {
|
||||
logger.Error("failed to discover V3 pools", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Info("UniswapV3 pool discovery complete", "poolsFound", v3PoolCount)
|
||||
logger.Info("total pools discovered", "v2", poolCount, "v3", v3PoolCount, "total", poolCount+v3PoolCount)
|
||||
|
||||
// Initialize arbitrage detector
|
||||
arbConfig := arbitrage.Config{
|
||||
@@ -90,30 +114,114 @@ func main() {
|
||||
logger.Error("failed to create detector", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Wire RPC to detector for live gas price
|
||||
detector.WithRPC(client)
|
||||
logger.Info("arbitrage detector initialized", "minProfitBPS", arbConfig.MinProfitBPS)
|
||||
|
||||
// Initialize flash loan executor (simulation mode until contract deployed)
|
||||
execConfig := execution.DefaultConfig()
|
||||
executor, err := execution.NewFlashLoanExecutor(
|
||||
client,
|
||||
nil, // No signer yet - will simulate only
|
||||
logger,
|
||||
execConfig,
|
||||
)
|
||||
var txSigner *bind.TransactOpts
|
||||
if *privateKeyHex != "" {
|
||||
txSigner, err = buildTransactor(*privateKeyHex, chainID)
|
||||
if err != nil {
|
||||
logger.Error("invalid executor private key", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Info("execution signer configured", "from", txSigner.From.Hex())
|
||||
} else {
|
||||
logger.Warn("flash loan executor running in simulation-only mode (no signer configured)")
|
||||
}
|
||||
|
||||
executor, err := execution.NewFlashLoanExecutor(client, txSigner, logger, execConfig)
|
||||
if err != nil {
|
||||
// This will fail without a signer, which is expected for now
|
||||
logger.Warn("flash loan executor in simulation mode", "reason", "no signer configured")
|
||||
logger.Error("failed to initialize flash loan executor", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Optional: validate Uniswap v4 PoolManager address if provided
|
||||
if *uniswapV4PoolManager != "" {
|
||||
pm := common.HexToAddress(*uniswapV4PoolManager)
|
||||
if err := discovery.VerifyContractDeployed(ctx, client, pm); err != nil {
|
||||
logger.Warn("uniswap v4 poolmanager validation failed", "address", pm.Hex(), "error", err)
|
||||
} else {
|
||||
logger.Info("uniswap v4 poolmanager validated", "address", pm.Hex())
|
||||
}
|
||||
}
|
||||
|
||||
// Setup graceful shutdown
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
// Main loop
|
||||
ticker := time.NewTicker(*scanInterval)
|
||||
defer ticker.Stop()
|
||||
// Shared scan function
|
||||
runScan := func(blockNumber uint64) {
|
||||
// Optional: refresh reserves for cached pools to avoid stale pricing
|
||||
if *refreshReserves {
|
||||
if err := discovery.RefreshUniswapV2Reserves(ctx, client, poolCache, logger); err != nil {
|
||||
logger.Warn("v2 reserve refresh failed", "error", err)
|
||||
}
|
||||
if err := discovery.RefreshUniswapV3Reserves(ctx, client, poolCache, logger); err != nil {
|
||||
logger.Warn("v3 reserve refresh failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
blockNumber := uint64(0)
|
||||
// Scan for opportunities
|
||||
opportunities, err := detector.ScanForOpportunities(ctx, blockNumber)
|
||||
if err != nil {
|
||||
logger.Error("scan failed", "error", err, "block", blockNumber)
|
||||
return
|
||||
}
|
||||
|
||||
if len(opportunities) > 0 {
|
||||
logger.Info("opportunities found!", "count", len(opportunities), "block", blockNumber)
|
||||
|
||||
for i, opp := range opportunities {
|
||||
logger.Info(fmt.Sprintf("Opportunity #%d", i+1),
|
||||
"inputToken", opp.InputToken.Hex(),
|
||||
"bridgeToken", opp.BridgeToken.Hex(),
|
||||
"inputAmount", opp.InputAmount.String(),
|
||||
"outputAmount", opp.OutputAmount.String(),
|
||||
"profitAmount", opp.ProfitAmount.String(),
|
||||
"profitBPS", opp.ProfitBPS.String(),
|
||||
"pool1", opp.FirstPool.Address.Hex(),
|
||||
"pool2", opp.SecondPool.Address.Hex(),
|
||||
)
|
||||
|
||||
// Simulate execution
|
||||
if executor != nil {
|
||||
result, err := executor.SimulateExecution(opp)
|
||||
if err != nil {
|
||||
logger.Error("simulation failed", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
logger.Info("simulation result",
|
||||
"grossProfit", result.GrossProfit.String(),
|
||||
"flashLoanFee", result.FlashLoanFee.String(),
|
||||
"netProfit", result.NetProfit.String(),
|
||||
"estimatedGas", result.EstimatedGas.String(),
|
||||
"finalProfit", result.FinalProfit.String(),
|
||||
"profitable", result.IsProfitable,
|
||||
)
|
||||
|
||||
if result.IsProfitable {
|
||||
logger.Info("✅ PROFITABLE OPPORTUNITY FOUND!")
|
||||
logger.Info("Deploy flash loan contract to execute")
|
||||
logger.Info("See contracts/DEPLOY.md for instructions")
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
logger.Debug("no opportunities found", "block", blockNumber)
|
||||
}
|
||||
|
||||
// Show stats
|
||||
oppsFound, lastBlock := detector.GetStats()
|
||||
logger.Info("scan complete",
|
||||
"block", blockNumber,
|
||||
"totalOpportunities", oppsFound,
|
||||
"lastScanBlock", lastBlock,
|
||||
)
|
||||
}
|
||||
|
||||
logger.Info("starting arbitrage scanner", "interval", scanInterval.String())
|
||||
logger.Info("")
|
||||
@@ -122,89 +230,82 @@ func main() {
|
||||
logger.Info("Press Ctrl+C to stop")
|
||||
logger.Info("")
|
||||
|
||||
// Try real-time header subscription first
|
||||
headers := make(chan *gethtypes.Header, 16)
|
||||
sub, err := client.SubscribeNewHead(ctx, headers)
|
||||
if err != nil {
|
||||
logger.Warn("failed to subscribe to new heads, falling back to polling", "error", err)
|
||||
sub = nil
|
||||
}
|
||||
|
||||
// Polling fallback ticker (also used if subscription drops)
|
||||
ticker := time.NewTicker(*scanInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
blockNumber++
|
||||
|
||||
// Scan for opportunities
|
||||
opportunities, err := detector.ScanForOpportunities(ctx, blockNumber)
|
||||
if err != nil {
|
||||
logger.Error("scan failed", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(opportunities) > 0 {
|
||||
logger.Info("opportunities found!", "count", len(opportunities))
|
||||
|
||||
for i, opp := range opportunities {
|
||||
logger.Info(fmt.Sprintf("Opportunity #%d", i+1),
|
||||
"inputToken", opp.InputToken.Hex(),
|
||||
"bridgeToken", opp.BridgeToken.Hex(),
|
||||
"inputAmount", opp.InputAmount.String(),
|
||||
"outputAmount", opp.OutputAmount.String(),
|
||||
"profitAmount", opp.ProfitAmount.String(),
|
||||
"profitBPS", opp.ProfitBPS.String(),
|
||||
"pool1", opp.FirstPool.Address.Hex(),
|
||||
"pool2", opp.SecondPool.Address.Hex(),
|
||||
)
|
||||
|
||||
// Simulate execution
|
||||
if executor != nil {
|
||||
result, err := executor.SimulateExecution(opp)
|
||||
if err != nil {
|
||||
logger.Error("simulation failed", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
logger.Info("simulation result",
|
||||
"grossProfit", result.GrossProfit.String(),
|
||||
"flashLoanFee", result.FlashLoanFee.String(),
|
||||
"netProfit", result.NetProfit.String(),
|
||||
"estimatedGas", result.EstimatedGas.String(),
|
||||
"finalProfit", result.FinalProfit.String(),
|
||||
"profitable", result.IsProfitable,
|
||||
)
|
||||
|
||||
if result.IsProfitable {
|
||||
logger.Info("✅ PROFITABLE OPPORTUNITY FOUND!")
|
||||
logger.Info("Deploy flash loan contract to execute")
|
||||
logger.Info("See contracts/DEPLOY.md for instructions")
|
||||
}
|
||||
}
|
||||
if sub != nil {
|
||||
select {
|
||||
case head := <-headers:
|
||||
if head == nil {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
logger.Debug("no opportunities found", "block", blockNumber)
|
||||
runScan(head.Number.Uint64())
|
||||
case err := <-sub.Err():
|
||||
logger.Warn("header subscription ended, switching to polling", "error", err)
|
||||
sub.Unsubscribe()
|
||||
sub = nil
|
||||
case <-sigChan:
|
||||
logger.Info("shutdown signal received")
|
||||
logger.Info("shutting down gracefully...")
|
||||
goto shutdown
|
||||
}
|
||||
|
||||
// Show stats
|
||||
oppsFound, lastBlock := detector.GetStats()
|
||||
logger.Info("scan complete",
|
||||
"block", blockNumber,
|
||||
"totalOpportunities", oppsFound,
|
||||
"lastScanBlock", lastBlock,
|
||||
)
|
||||
|
||||
case <-sigChan:
|
||||
logger.Info("shutdown signal received")
|
||||
logger.Info("shutting down gracefully...")
|
||||
|
||||
// Print final stats
|
||||
oppsFound, _ := detector.GetStats()
|
||||
logger.Info("final statistics",
|
||||
"totalOpportunitiesFound", oppsFound,
|
||||
)
|
||||
|
||||
if executor != nil {
|
||||
execCount, totalProfit := executor.GetStats()
|
||||
logger.Info("execution statistics",
|
||||
"executedCount", execCount,
|
||||
"totalProfit", totalProfit.String(),
|
||||
)
|
||||
} else {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
blockNumber, err := client.BlockNumber(ctx)
|
||||
if err != nil {
|
||||
logger.Error("failed to fetch block number", "error", err)
|
||||
continue
|
||||
}
|
||||
runScan(blockNumber)
|
||||
case <-sigChan:
|
||||
logger.Info("shutdown signal received")
|
||||
logger.Info("shutting down gracefully...")
|
||||
goto shutdown
|
||||
}
|
||||
|
||||
logger.Info("shutdown complete")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
shutdown:
|
||||
// Print final stats
|
||||
oppsFound, _ := detector.GetStats()
|
||||
logger.Info("final statistics",
|
||||
"totalOpportunitiesFound", oppsFound,
|
||||
)
|
||||
|
||||
if executor != nil {
|
||||
execCount, totalProfit := executor.GetStats()
|
||||
logger.Info("execution statistics",
|
||||
"executedCount", execCount,
|
||||
"totalProfit", totalProfit.String(),
|
||||
)
|
||||
}
|
||||
|
||||
logger.Info("shutdown complete")
|
||||
}
|
||||
|
||||
// buildTransactor constructs a keyed transactor from a hex private key.
|
||||
func buildTransactor(hexKey string, chainID *big.Int) (*bind.TransactOpts, error) {
|
||||
key, err := crypto.HexToECDSA(trimHexPrefix(hexKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bind.NewKeyedTransactorWithChainID(key, chainID)
|
||||
}
|
||||
|
||||
func trimHexPrefix(s string) string {
|
||||
if len(s) >= 2 && (s[:2] == "0x" || s[:2] == "0X") {
|
||||
return s[2:]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user