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:
Krypto Kajun
2025-09-14 10:16:29 -05:00
parent 5db7587923
commit c16182d80c
1364 changed files with 473970 additions and 1202 deletions

View File

@@ -24,36 +24,36 @@ type AdaptiveRateLimiter struct {
// AdaptiveEndpoint represents an endpoint with adaptive rate limiting
type AdaptiveEndpoint struct {
URL string
limiter *rate.Limiter
config config.RateLimitConfig
circuitBreaker *CircuitBreaker
metrics *EndpointMetrics
healthChecker *HealthChecker
lastAdjustment time.Time
consecutiveErrors int64
URL string
limiter *rate.Limiter
config config.RateLimitConfig
circuitBreaker *CircuitBreaker
metrics *EndpointMetrics
healthChecker *HealthChecker
lastAdjustment time.Time
consecutiveErrors int64
consecutiveSuccess int64
}
// EndpointMetrics tracks performance metrics for an endpoint
type EndpointMetrics struct {
TotalRequests int64
TotalRequests int64
SuccessfulRequests int64
FailedRequests int64
TotalLatency int64 // nanoseconds
LastRequestTime int64 // unix timestamp
SuccessRate float64
AverageLatency float64 // milliseconds
FailedRequests int64
TotalLatency int64 // nanoseconds
LastRequestTime int64 // unix timestamp
SuccessRate float64
AverageLatency float64 // milliseconds
}
// CircuitBreaker implements circuit breaker pattern for failed endpoints
type CircuitBreaker struct {
state int32 // 0: Closed, 1: Open, 2: HalfOpen
failureCount int64
lastFailTime int64
threshold int64
timeout time.Duration // How long to wait before trying again
testRequests int64 // Number of test requests in half-open state
state int32 // 0: Closed, 1: Open, 2: HalfOpen
failureCount int64
lastFailTime int64
threshold int64
timeout time.Duration // How long to wait before trying again
testRequests int64 // Number of test requests in half-open state
}
// Circuit breaker states
@@ -65,12 +65,12 @@ const (
// HealthChecker monitors endpoint health
type HealthChecker struct {
endpoint string
interval time.Duration
timeout time.Duration
isHealthy int64 // atomic bool
lastCheck int64 // unix timestamp
stopChan chan struct{}
endpoint string
interval time.Duration
timeout time.Duration
isHealthy int64 // atomic bool
lastCheck int64 // unix timestamp
stopChan chan struct{}
}
// NewAdaptiveRateLimiter creates a new adaptive rate limiter
@@ -239,7 +239,7 @@ func (arl *AdaptiveRateLimiter) calculateEndpointScore(endpoint *AdaptiveEndpoin
loadWeight := 0.1
successScore := endpoint.metrics.SuccessRate
// Invert latency score (lower latency = higher score)
latencyScore := 1.0
if endpoint.metrics.AverageLatency > 0 {
@@ -317,7 +317,7 @@ func (arl *AdaptiveRateLimiter) adjustEndpointRateLimit(url string, endpoint *Ad
if abs(newLimit-currentLimit)/currentLimit > 0.05 { // 5% change threshold
endpoint.limiter.SetLimit(rate.Limit(newLimit))
endpoint.lastAdjustment = time.Now()
arl.logger.Info(fmt.Sprintf("Adjusted rate limit for %s: %.2f -> %.2f (success: %.2f%%, latency: %.2fms)",
url, currentLimit, newLimit, successRate*100, avgLatency))
}
@@ -376,7 +376,7 @@ func (cb *CircuitBreaker) recordSuccess() {
func (cb *CircuitBreaker) recordFailure() {
failures := atomic.AddInt64(&cb.failureCount, 1)
atomic.StoreInt64(&cb.lastFailTime, time.Now().Unix())
if failures >= cb.threshold {
atomic.StoreInt32(&cb.state, CircuitOpen)
}
@@ -405,13 +405,13 @@ func (hc *HealthChecker) checkHealth() {
// Simple health check - try to connect
// In production, this might make a simple RPC call
healthy := hc.performHealthCheck(ctx)
if healthy {
atomic.StoreInt64(&hc.isHealthy, 1)
} else {
atomic.StoreInt64(&hc.isHealthy, 0)
}
atomic.StoreInt64(&hc.lastCheck, time.Now().Unix())
}
@@ -425,7 +425,7 @@ func (hc *HealthChecker) performHealthCheck(ctx context.Context) bool {
// Stop stops the adaptive rate limiter
func (arl *AdaptiveRateLimiter) Stop() {
close(arl.stopChan)
// Stop all health checkers
arl.mu.RLock()
for _, endpoint := range arl.endpoints {
@@ -445,6 +445,6 @@ func (arl *AdaptiveRateLimiter) GetMetrics() map[string]*EndpointMetrics {
arl.updateCalculatedMetrics(endpoint)
metrics[url] = endpoint.metrics
}
return metrics
}
}

View File

@@ -125,4 +125,4 @@ func (lm *LimiterManager) GetEndpoints() []string {
}
return endpoints
}
}

View File

@@ -230,4 +230,4 @@ func TestRateLimiting(t *testing.T) {
assert.NoError(t, err)
duration = time.Since(start)
assert.True(t, duration >= time.Second, "Second request should be delayed by rate limiter")
}
}