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:
@@ -14,41 +14,41 @@ import (
|
||||
type MetricsCollector struct {
|
||||
logger *logger.Logger
|
||||
mu sync.RWMutex
|
||||
|
||||
|
||||
// L2 Message Metrics
|
||||
L2MessagesProcessed uint64
|
||||
L2MessagesPerSecond float64
|
||||
L2MessageLag time.Duration
|
||||
BatchesProcessed uint64
|
||||
|
||||
L2MessagesProcessed uint64
|
||||
L2MessagesPerSecond float64
|
||||
L2MessageLag time.Duration
|
||||
BatchesProcessed uint64
|
||||
|
||||
// DEX Interaction Metrics
|
||||
DEXInteractionsFound uint64
|
||||
SwapOpportunities uint64
|
||||
DEXInteractionsFound uint64
|
||||
SwapOpportunities uint64
|
||||
ArbitrageOpportunities uint64
|
||||
|
||||
|
||||
// Performance Metrics
|
||||
ProcessingLatency time.Duration
|
||||
ErrorRate float64
|
||||
SuccessfulTrades uint64
|
||||
FailedTrades uint64
|
||||
|
||||
ProcessingLatency time.Duration
|
||||
ErrorRate float64
|
||||
SuccessfulTrades uint64
|
||||
FailedTrades uint64
|
||||
|
||||
// Financial Metrics
|
||||
TotalProfit float64
|
||||
TotalLoss float64
|
||||
GasCostsSpent float64
|
||||
NetProfit float64
|
||||
|
||||
TotalProfit float64
|
||||
TotalLoss float64
|
||||
GasCostsSpent float64
|
||||
NetProfit float64
|
||||
|
||||
// Gas Metrics
|
||||
AverageGasPrice uint64
|
||||
L1DataFeesSpent float64
|
||||
L2ComputeFeesSpent float64
|
||||
|
||||
AverageGasPrice uint64
|
||||
L1DataFeesSpent float64
|
||||
L2ComputeFeesSpent float64
|
||||
|
||||
// Health Metrics
|
||||
UptimeSeconds uint64
|
||||
LastHealthCheck time.Time
|
||||
|
||||
UptimeSeconds uint64
|
||||
LastHealthCheck time.Time
|
||||
|
||||
// Start time for calculations
|
||||
startTime time.Time
|
||||
startTime time.Time
|
||||
}
|
||||
|
||||
// NewMetricsCollector creates a new metrics collector
|
||||
@@ -64,10 +64,10 @@ func NewMetricsCollector(logger *logger.Logger) *MetricsCollector {
|
||||
func (m *MetricsCollector) RecordL2Message(processingTime time.Duration) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.L2MessagesProcessed++
|
||||
m.ProcessingLatency = processingTime
|
||||
|
||||
|
||||
// Calculate messages per second
|
||||
elapsed := time.Since(m.startTime).Seconds()
|
||||
if elapsed > 0 {
|
||||
@@ -79,7 +79,7 @@ func (m *MetricsCollector) RecordL2Message(processingTime time.Duration) {
|
||||
func (m *MetricsCollector) RecordL2MessageLag(lag time.Duration) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.L2MessageLag = lag
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ func (m *MetricsCollector) RecordL2MessageLag(lag time.Duration) {
|
||||
func (m *MetricsCollector) RecordBatchProcessed() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.BatchesProcessed++
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func (m *MetricsCollector) RecordBatchProcessed() {
|
||||
func (m *MetricsCollector) RecordDEXInteraction() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.DEXInteractionsFound++
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ func (m *MetricsCollector) RecordDEXInteraction() {
|
||||
func (m *MetricsCollector) RecordSwapOpportunity() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.SwapOpportunities++
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ func (m *MetricsCollector) RecordSwapOpportunity() {
|
||||
func (m *MetricsCollector) RecordArbitrageOpportunity() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.ArbitrageOpportunities++
|
||||
}
|
||||
|
||||
@@ -119,12 +119,12 @@ func (m *MetricsCollector) RecordArbitrageOpportunity() {
|
||||
func (m *MetricsCollector) RecordSuccessfulTrade(profit float64, gasCost float64) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.SuccessfulTrades++
|
||||
m.TotalProfit += profit
|
||||
m.GasCostsSpent += gasCost
|
||||
m.NetProfit = m.TotalProfit - m.TotalLoss - m.GasCostsSpent
|
||||
|
||||
|
||||
// Update error rate
|
||||
totalTrades := m.SuccessfulTrades + m.FailedTrades
|
||||
if totalTrades > 0 {
|
||||
@@ -136,12 +136,12 @@ func (m *MetricsCollector) RecordSuccessfulTrade(profit float64, gasCost float64
|
||||
func (m *MetricsCollector) RecordFailedTrade(loss float64, gasCost float64) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.FailedTrades++
|
||||
m.TotalLoss += loss
|
||||
m.GasCostsSpent += gasCost
|
||||
m.NetProfit = m.TotalProfit - m.TotalLoss - m.GasCostsSpent
|
||||
|
||||
|
||||
// Update error rate
|
||||
totalTrades := m.SuccessfulTrades + m.FailedTrades
|
||||
if totalTrades > 0 {
|
||||
@@ -153,7 +153,7 @@ func (m *MetricsCollector) RecordFailedTrade(loss float64, gasCost float64) {
|
||||
func (m *MetricsCollector) RecordGasMetrics(gasPrice uint64, l1DataFee, l2ComputeFee float64) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.AverageGasPrice = gasPrice
|
||||
m.L1DataFeesSpent += l1DataFee
|
||||
m.L2ComputeFeesSpent += l2ComputeFee
|
||||
@@ -163,7 +163,7 @@ func (m *MetricsCollector) RecordGasMetrics(gasPrice uint64, l1DataFee, l2Comput
|
||||
func (m *MetricsCollector) UpdateHealthCheck() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
|
||||
m.LastHealthCheck = time.Now()
|
||||
m.UptimeSeconds = uint64(time.Since(m.startTime).Seconds())
|
||||
}
|
||||
@@ -172,7 +172,7 @@ func (m *MetricsCollector) UpdateHealthCheck() {
|
||||
func (m *MetricsCollector) GetSnapshot() MetricsSnapshot {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
|
||||
return MetricsSnapshot{
|
||||
L2MessagesProcessed: m.L2MessagesProcessed,
|
||||
L2MessagesPerSecond: m.L2MessagesPerSecond,
|
||||
@@ -232,32 +232,32 @@ type MetricsServer struct {
|
||||
// NewMetricsServer creates a new metrics server
|
||||
func NewMetricsServer(collector *MetricsCollector, logger *logger.Logger, port string) *MetricsServer {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
|
||||
// Create authentication configuration
|
||||
authConfig := &auth.AuthConfig{
|
||||
Logger: logger,
|
||||
RequireHTTPS: false, // Set to true in production
|
||||
RequireHTTPS: false, // Set to true in production
|
||||
AllowedIPs: []string{"127.0.0.1", "::1"}, // Localhost only by default
|
||||
}
|
||||
|
||||
|
||||
// Create authentication middleware
|
||||
middleware := auth.NewMiddleware(authConfig)
|
||||
|
||||
|
||||
server := &MetricsServer{
|
||||
collector: collector,
|
||||
logger: logger,
|
||||
collector: collector,
|
||||
logger: logger,
|
||||
server: &http.Server{
|
||||
Addr: ":" + port,
|
||||
Handler: mux,
|
||||
},
|
||||
middleware: middleware,
|
||||
}
|
||||
|
||||
|
||||
// Register endpoints with authentication
|
||||
mux.HandleFunc("/metrics", middleware.RequireAuthentication(server.handleMetrics))
|
||||
mux.HandleFunc("/health", middleware.RequireAuthentication(server.handleHealth))
|
||||
mux.HandleFunc("/metrics/prometheus", middleware.RequireAuthentication(server.handlePrometheus))
|
||||
|
||||
|
||||
return server
|
||||
}
|
||||
|
||||
@@ -276,10 +276,10 @@ func (s *MetricsServer) Stop() error {
|
||||
// handleMetrics serves metrics in JSON format
|
||||
func (s *MetricsServer) handleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
snapshot := s.collector.GetSnapshot()
|
||||
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
|
||||
// Simple JSON serialization
|
||||
response := `{
|
||||
"l2_messages_processed": ` + uintToString(snapshot.L2MessagesProcessed) + `,
|
||||
@@ -302,14 +302,14 @@ func (s *MetricsServer) handleMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
"l2_compute_fees_spent_eth": ` + floatToString(snapshot.L2ComputeFeesSpent) + `,
|
||||
"uptime_seconds": ` + uintToString(snapshot.UptimeSeconds) + `
|
||||
}`
|
||||
|
||||
|
||||
w.Write([]byte(response))
|
||||
}
|
||||
|
||||
// handleHealth serves health check
|
||||
func (s *MetricsServer) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
s.collector.UpdateHealthCheck()
|
||||
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"status": "healthy", "timestamp": "` + time.Now().Format(time.RFC3339) + `"}`))
|
||||
@@ -318,10 +318,10 @@ func (s *MetricsServer) handleHealth(w http.ResponseWriter, r *http.Request) {
|
||||
// handlePrometheus serves metrics in Prometheus format
|
||||
func (s *MetricsServer) handlePrometheus(w http.ResponseWriter, r *http.Request) {
|
||||
snapshot := s.collector.GetSnapshot()
|
||||
|
||||
|
||||
w.Header().Set("Content-Type", "text/plain")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
|
||||
prometheus := `# HELP mev_bot_l2_messages_processed Total L2 messages processed
|
||||
# TYPE mev_bot_l2_messages_processed counter
|
||||
mev_bot_l2_messages_processed ` + uintToString(snapshot.L2MessagesProcessed) + `
|
||||
@@ -350,7 +350,7 @@ mev_bot_error_rate ` + floatToString(snapshot.ErrorRate) + `
|
||||
# TYPE mev_bot_uptime_seconds counter
|
||||
mev_bot_uptime_seconds ` + uintToString(snapshot.UptimeSeconds) + `
|
||||
`
|
||||
|
||||
|
||||
w.Write([]byte(prometheus))
|
||||
}
|
||||
|
||||
@@ -365,4 +365,4 @@ func floatToString(val float64) string {
|
||||
|
||||
func durationToString(val time.Duration) string {
|
||||
return fmt.Sprintf("%.2f", float64(val.Nanoseconds())/1000000.0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user