refactor: move all remaining files to orig/ directory

Completed clean root directory structure:
- Root now contains only: .git, .env, docs/, orig/
- Moved all remaining files and directories to orig/:
  - Config files (.claude, .dockerignore, .drone.yml, etc.)
  - All .env variants (except active .env)
  - Git config (.gitconfig, .github, .gitignore, etc.)
  - Tool configs (.golangci.yml, .revive.toml, etc.)
  - Documentation (*.md files, @prompts)
  - Build files (Dockerfiles, Makefile, go.mod, go.sum)
  - Docker compose files
  - All source directories (scripts, tests, tools, etc.)
  - Runtime directories (logs, monitoring, reports)
  - Dependency files (node_modules, lib, cache)
  - Special files (--delete)

- Removed empty runtime directories (bin/, data/)

V2 structure is now clean:
- docs/planning/ - V2 planning documents
- orig/ - Complete V1 codebase preserved
- .env - Active environment config (not in git)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Administrator
2025-11-10 10:53:05 +01:00
parent 803de231ba
commit c54c569f30
718 changed files with 8304 additions and 8281 deletions

View File

@@ -0,0 +1,398 @@
//go:build integration && legacy && forked
// +build integration,legacy,forked
package production_test
import (
"context"
"log"
"math/big"
"os"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/fraktal/mev-beta/bindings/arbitrage"
"github.com/fraktal/mev-beta/internal/config"
arbService "github.com/fraktal/mev-beta/pkg/arbitrage"
"github.com/fraktal/mev-beta/pkg/arbitrum"
"github.com/fraktal/mev-beta/pkg/mev"
"github.com/fraktal/mev-beta/pkg/monitor"
"github.com/fraktal/mev-beta/pkg/uniswap"
)
// ProductionLogger provides structured logging for production validation
type ProductionLogger struct {
*log.Logger
}
func NewProductionLogger() *ProductionLogger {
return &ProductionLogger{
Logger: log.New(os.Stdout, "[PRODUCTION-TEST] ", log.LstdFlags|log.Lmicroseconds),
}
}
func (pl *ProductionLogger) LogArbitrageOpportunity(opportunity *mev.MEVOpportunity, profit *big.Int) {
profitETH := new(big.Float).Quo(new(big.Float).SetInt(profit), new(big.Float).SetInt(big.NewInt(1e18)))
pl.Printf("🎯 ARBITRAGE OPPORTUNITY DETECTED: Pool=%s, Type=%s, EstimatedProfit=%.6f ETH",
opportunity.PoolAddress.Hex(), opportunity.Type, profitETH)
}
func (pl *ProductionLogger) LogTradeExecution(txHash common.Hash, gasUsed uint64, actualProfit *big.Int) {
profitETH := new(big.Float).Quo(new(big.Float).SetInt(actualProfit), new(big.Float).SetInt(big.NewInt(1e18)))
pl.Printf("⚡ ARBITRAGE EXECUTED: TxHash=%s, GasUsed=%d, ActualProfit=%.6f ETH",
txHash.Hex(), gasUsed, profitETH)
}
func (pl *ProductionLogger) LogMarketConditions(pool1Price, pool2Price *big.Int, spread *big.Float) {
price1ETH := new(big.Float).Quo(new(big.Float).SetInt(pool1Price), new(big.Float).SetInt(big.NewInt(1e6)))
price2ETH := new(big.Float).Quo(new(big.Float).SetInt(pool2Price), new(big.Float).SetInt(big.NewInt(1e6)))
pl.Printf("📊 MARKET CONDITIONS: Pool1Price=%.2f USDC, Pool2Price=%.2f USDC, Spread=%.4f%%",
price1ETH, price2ETH, spread)
}
// TestProductionArbitrageValidation proves the bot can detect and execute real arbitrages
func TestProductionArbitrageValidation(t *testing.T) {
logger := NewProductionLogger()
logger.Printf("🚀 STARTING PRODUCTION ARBITRAGE VALIDATION TEST")
// Setup forked Arbitrum environment
client, cleanup := setupForkedArbitrum(t)
defer cleanup()
logger.Printf("✅ Connected to forked Arbitrum mainnet")
// Validate we can connect to real Arbitrum contracts
ctx := context.Background()
// Real Arbitrum pool addresses with different fee tiers
wethUsdcPool05 := common.HexToAddress("0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443") // 0.05% fee
wethUsdcPool30 := common.HexToAddress("0x17c14D2c404D167802b16C450d3c99F88F2c4F4d") // 0.3% fee
logger.Printf("📍 Target Pools: WETH/USDC 0.05%% (%s), WETH/USDC 0.30%% (%s)",
wethUsdcPool05.Hex(), wethUsdcPool30.Hex())
t.Run("Real World Market Analysis", func(t *testing.T) {
logger.Printf("🔍 ANALYZING REAL MARKET CONDITIONS...")
// Get current prices from both pools
price1, err := uniswap.GetPoolPrice(client, wethUsdcPool05)
require.NoError(t, err, "Failed to get price from 0.05% pool")
price2, err := uniswap.GetPoolPrice(client, wethUsdcPool30)
require.NoError(t, err, "Failed to get price from 0.30% pool")
// Calculate price spread
priceDiff := new(big.Int).Sub(price1, price2)
if priceDiff.Sign() < 0 {
priceDiff.Neg(priceDiff)
}
spreadBasisPoints := new(big.Int).Div(
new(big.Int).Mul(priceDiff, big.NewInt(10000)),
price1,
)
spreadPercent := new(big.Float).Quo(
new(big.Float).SetInt(spreadBasisPoints),
new(big.Float).SetInt(big.NewInt(100)),
)
logger.LogMarketConditions(price1, price2, spreadPercent)
// Validate prices are reasonable (WETH/USDC should be between $1000-$10000)
minPrice := big.NewInt(1000 * 1e6) // $1000 USDC
maxPrice := big.NewInt(10000 * 1e6) // $10000 USDC
assert.True(t, price1.Cmp(minPrice) >= 0 && price1.Cmp(maxPrice) <= 0,
"Pool 1 price should be reasonable: got %s USDC",
new(big.Float).Quo(new(big.Float).SetInt(price1), new(big.Float).SetInt(big.NewInt(1e6))))
assert.True(t, price2.Cmp(minPrice) >= 0 && price2.Cmp(maxPrice) <= 0,
"Pool 2 price should be reasonable: got %s USDC",
new(big.Float).Quo(new(big.Float).SetInt(price2), new(big.Float).SetInt(big.NewInt(1e6))))
logger.Printf("✅ Market conditions validated - prices are within expected ranges")
})
t.Run("Live Arbitrage Opportunity Detection", func(t *testing.T) {
logger.Printf("🎯 TESTING LIVE ARBITRAGE OPPORTUNITY DETECTION...")
// Deploy our arbitrage contract to forked environment
privateKey, err := crypto.GenerateKey()
require.NoError(t, err)
auth, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(42161))
require.NoError(t, err)
// Set reasonable gas price for Arbitrum
gasPrice, err := client.SuggestGasPrice(ctx)
require.NoError(t, err)
auth.GasPrice = gasPrice
auth.GasLimit = uint64(5000000)
logger.Printf("⚙️ Deploying arbitrage contract with gas price: %s gwei",
new(big.Float).Quo(new(big.Float).SetInt(gasPrice), new(big.Float).SetInt(big.NewInt(1e9))))
// Deploy ArbitrageExecutor
contractAddr, tx, contract, err := arbitrage.DeployArbitrageExecutor(
auth,
client,
common.HexToAddress("0x1F98431c8aD98523631AE4a59f267346ea31F984"), // Uniswap V3 Factory
common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), // WETH
)
require.NoError(t, err, "Failed to deploy arbitrage contract")
logger.Printf("📝 Contract deployment tx: %s", tx.Hash().Hex())
// Wait for deployment
receipt, err := bind.WaitMined(ctx, client, tx)
require.NoError(t, err)
require.Equal(t, types.ReceiptStatusSuccessful, receipt.Status)
logger.Printf("✅ ArbitrageExecutor deployed at: %s (Gas used: %d)",
contractAddr.Hex(), receipt.GasUsed)
// Test arbitrage opportunity detection
swapAmount := big.NewInt(1000000000000000000) // 1 ETH
opportunity, err := contract.DetectArbitrageOpportunity(nil, wethUsdcPool05, wethUsdcPool30, swapAmount)
require.NoError(t, err, "Failed to detect arbitrage opportunity")
logger.LogArbitrageOpportunity(&mev.MEVOpportunity{
Type: mev.TypeArbitrage,
EstimatedProfit: opportunity.EstimatedProfit,
PoolAddress: wethUsdcPool05,
}, opportunity.EstimatedProfit)
if opportunity.Profitable {
logger.Printf("🎉 PROFITABLE ARBITRAGE DETECTED!")
// Calculate net profit after gas costs
gasEstimate := big.NewInt(300000) // Estimated gas for arbitrage
gasCost := new(big.Int).Mul(gasPrice, gasEstimate)
netProfit := new(big.Int).Sub(opportunity.EstimatedProfit, gasCost)
netProfitETH := new(big.Float).Quo(new(big.Float).SetInt(netProfit), new(big.Float).SetInt(big.NewInt(1e18)))
logger.Printf("💰 Net profit after gas: %.6f ETH", netProfitETH)
assert.True(t, netProfit.Sign() > 0, "Net profit should be positive after gas costs")
} else {
logger.Printf(" No profitable arbitrage found in current market conditions")
// This is acceptable - real markets may not always have arbitrage opportunities
}
})
t.Run("MEV Competition Analysis", func(t *testing.T) {
logger.Printf("🏁 TESTING MEV COMPETITION ANALYSIS...")
analyzer := mev.NewCompetitionAnalyzer(client)
opportunity := &mev.MEVOpportunity{
Type: mev.TypeArbitrage,
EstimatedProfit: big.NewInt(50000000000000000), // 0.05 ETH
RequiredGasLimit: big.NewInt(300000),
PoolAddress: wethUsdcPool05,
Timestamp: time.Now(),
}
competition, err := analyzer.AnalyzeCompetition(ctx, opportunity)
require.NoError(t, err, "Failed to analyze MEV competition")
logger.Printf("🏆 Competition Analysis: Competitors=%d, AvgPriorityFee=%s gwei, SuccessRate=%.2f%%",
competition.CompetitorCount,
new(big.Float).Quo(new(big.Float).SetInt(competition.AveragePriorityFee), new(big.Float).SetInt(big.NewInt(1e9))),
competition.SuccessRate*100)
strategy, err := analyzer.CalculateOptimalBid(ctx, opportunity, competition)
require.NoError(t, err, "Failed to calculate optimal bidding strategy")
logger.Printf("💡 Optimal Strategy: PriorityFee=%s gwei, MaxFee=%s gwei, ExpectedProfit=%.6f ETH",
new(big.Float).Quo(new(big.Float).SetInt(strategy.PriorityFeePerGas), new(big.Float).SetInt(big.NewInt(1e9))),
new(big.Float).Quo(new(big.Float).SetInt(strategy.MaxFeePerGas), new(big.Float).SetInt(big.NewInt(1e9))),
new(big.Float).Quo(new(big.Float).SetInt(strategy.ExpectedProfit), new(big.Float).SetInt(big.NewInt(1e18))))
assert.Greater(t, strategy.ExpectedProfit.Sign(), 0, "Strategy should maintain profitability")
})
t.Run("Real-Time Market Monitoring", func(t *testing.T) {
logger.Printf("📡 TESTING REAL-TIME MARKET MONITORING...")
// Setup connection manager with fallback
cfg := &config.ArbitrumConfig{
RPCEndpoint: os.Getenv("ARBITRUM_RPC_ENDPOINT"),
}
connManager := arbitrum.NewConnectionManager(cfg)
defer connManager.Close()
// Test connection with automatic fallback
healthyClient, err := connManager.GetClientWithRetry(ctx, 3)
require.NoError(t, err, "Failed to get healthy client connection")
defer healthyClient.Close()
logger.Printf("✅ Established healthy connection with fallback support")
// Test real-time block monitoring
monitor := monitor.NewConcurrentMonitor(healthyClient)
// Monitor for 30 seconds to catch real blocks
monitorCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
blockChan := make(chan uint64, 10)
eventChan := make(chan *arbService.SimpleSwapEvent, 100)
// Start monitoring in background
go func() {
err := monitor.StartMonitoring(monitorCtx, blockChan)
if err != nil {
logger.Printf("❌ Monitoring error: %v", err)
}
}()
// Process blocks and detect swap events
go func() {
for {
select {
case blockNum := <-blockChan:
logger.Printf("📦 Processing block: %d", blockNum)
// Get block and analyze transactions
block, err := healthyClient.BlockByNumber(monitorCtx, big.NewInt(int64(blockNum)))
if err != nil {
continue
}
// Look for large swaps that could create arbitrage opportunities
for _, tx := range block.Transactions() {
if tx.To() != nil &&
(tx.To().Hex() == wethUsdcPool05.Hex() || tx.To().Hex() == wethUsdcPool30.Hex()) {
logger.Printf("🔄 Large swap detected in target pool: TxHash=%s, Pool=%s",
tx.Hash().Hex(), tx.To().Hex())
// Create mock swap event for testing
mockEvent := &arbService.SimpleSwapEvent{
TxHash: tx.Hash(),
Pool: *tx.To(),
Token0: common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), // WETH
Token1: common.HexToAddress("0xaf88d065e77c8cC2239327C5EDb3A432268e5831"), // USDC
Amount0: big.NewInt(1000000000000000000), // 1 ETH
Amount1: big.NewInt(-2000000000), // -2000 USDC
SqrtPriceX96: func() *big.Int { x, _ := new(big.Int).SetString("79228162514264337593543950336", 10); return x }(),
}
eventChan <- mockEvent
}
}
case <-monitorCtx.Done():
return
}
}
}()
// Collect and analyze events
eventCount := 0
arbitrageCount := 0
for {
select {
case event := <-eventChan:
eventCount++
logger.Printf("⚡ Swap event #%d: Pool=%s, Amount0=%s ETH",
eventCount, event.Pool.Hex(),
new(big.Float).Quo(new(big.Float).SetInt(event.Amount0), new(big.Float).SetInt(big.NewInt(1e18))))
// Check if this creates arbitrage opportunity
if event.Amount0.Cmp(big.NewInt(500000000000000000)) >= 0 { // >= 0.5 ETH
arbitrageCount++
logger.Printf("🎯 Large swap detected - potential arbitrage opportunity #%d", arbitrageCount)
}
case <-monitorCtx.Done():
logger.Printf("📊 MONITORING SUMMARY: ProcessedEvents=%d, PotentialArbitrages=%d",
eventCount, arbitrageCount)
return
}
}
})
t.Run("Production Configuration Validation", func(t *testing.T) {
logger.Printf("⚙️ VALIDATING PRODUCTION CONFIGURATION...")
// Test configuration loading
cfg, err := config.Load("../../config/arbitrum_production.yaml")
require.NoError(t, err, "Failed to load production config")
// Validate all critical addresses are configured
assert.NotEmpty(t, cfg.Arbitrum.RPCEndpoint, "RPC endpoint must be configured")
assert.NotEmpty(t, cfg.Arbitrum.FallbackEndpoints, "Fallback endpoints must be configured")
assert.Greater(t, len(cfg.Arbitrum.FallbackEndpoints), 2, "Should have multiple fallback endpoints")
logger.Printf("✅ Configuration validation passed:")
logger.Printf(" - Primary RPC: %s", cfg.Arbitrum.RPCEndpoint)
logger.Printf(" - Fallback endpoints: %d configured", len(cfg.Arbitrum.FallbackEndpoints))
logger.Printf(" - Rate limit: %d RPS", cfg.Arbitrum.RateLimit.RequestsPerSecond)
// Test environment variable override
originalEndpoint := os.Getenv("ARBITRUM_RPC_ENDPOINT")
testEndpoint := "wss://test-override.com"
os.Setenv("ARBITRUM_RPC_ENDPOINT", testEndpoint)
defer func() {
if originalEndpoint != "" {
os.Setenv("ARBITRUM_RPC_ENDPOINT", originalEndpoint)
} else {
os.Unsetenv("ARBITRUM_RPC_ENDPOINT")
}
}()
cfg.OverrideWithEnv()
assert.Equal(t, testEndpoint, cfg.Arbitrum.RPCEndpoint, "Environment variable should override config")
logger.Printf("✅ Environment variable override working correctly")
})
logger.Printf("🎉 PRODUCTION VALIDATION COMPLETED SUCCESSFULLY!")
logger.Printf("📋 VALIDATION SUMMARY:")
logger.Printf(" ✅ Real market data access verified")
logger.Printf(" ✅ Smart contract deployment successful")
logger.Printf(" ✅ Arbitrage detection functional")
logger.Printf(" ✅ MEV competition analysis working")
logger.Printf(" ✅ Real-time monitoring operational")
logger.Printf(" ✅ Configuration system validated")
logger.Printf(" ✅ Fallback connectivity confirmed")
logger.Printf("")
logger.Printf("🚀 THE MEV BOT IS PRODUCTION READY!")
}
// setupForkedArbitrum sets up a forked Arbitrum environment for testing
func setupForkedArbitrum(t *testing.T) (*ethclient.Client, func()) {
// Use environment variable or default to a working endpoint
rpcEndpoint := os.Getenv("ARBITRUM_RPC_ENDPOINT")
if rpcEndpoint == "" {
rpcEndpoint = "https://arb1.arbitrum.io/rpc" // Public endpoint for testing
}
client, err := ethclient.Dial(rpcEndpoint)
require.NoError(t, err, "Failed to connect to Arbitrum")
// Verify we're connected to Arbitrum mainnet
chainID, err := client.ChainID(context.Background())
require.NoError(t, err, "Failed to get chain ID")
require.Equal(t, int64(42161), chainID.Int64(), "Must be connected to Arbitrum mainnet")
cleanup := func() {
client.Close()
}
return client, cleanup
}