Restructured project for V2 refactor: **Structure Changes:** - Moved all V1 code to orig/ folder (preserved with git mv) - Created docs/planning/ directory - Added orig/README_V1.md explaining V1 preservation **Planning Documents:** - 00_V2_MASTER_PLAN.md: Complete architecture overview - Executive summary of critical V1 issues - High-level component architecture diagrams - 5-phase implementation roadmap - Success metrics and risk mitigation - 07_TASK_BREAKDOWN.md: Atomic task breakdown - 99+ hours of detailed tasks - Every task < 2 hours (atomic) - Clear dependencies and success criteria - Organized by implementation phase **V2 Key Improvements:** - Per-exchange parsers (factory pattern) - Multi-layer strict validation - Multi-index pool cache - Background validation pipeline - Comprehensive observability **Critical Issues Addressed:** - Zero address tokens (strict validation + cache enrichment) - Parsing accuracy (protocol-specific parsers) - No audit trail (background validation channel) - Inefficient lookups (multi-index cache) - Stats disconnection (event-driven metrics) Next Steps: 1. Review planning documents 2. Begin Phase 1: Foundation (P1-001 through P1-010) 3. Implement parsers in Phase 2 4. Build cache system in Phase 3 5. Add validation pipeline in Phase 4 6. Migrate and test in Phase 5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package parser
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
logpkg "github.com/fraktal/mev-beta/internal/logger"
|
|
pkgtypes "github.com/fraktal/mev-beta/pkg/types"
|
|
)
|
|
|
|
// OpportunityDispatcher represents the arbitrage service entry point that can
|
|
// accept opportunities discovered by the transaction analyzer.
|
|
type OpportunityDispatcher interface {
|
|
SubmitBridgeOpportunity(ctx context.Context, bridgeOpportunity interface{}) error
|
|
}
|
|
|
|
// Executor routes arbitrage opportunities discovered in the Arbitrum parser to
|
|
// the core arbitrage service.
|
|
type Executor struct {
|
|
logger *logpkg.Logger
|
|
dispatcher OpportunityDispatcher
|
|
metrics *ExecutorMetrics
|
|
serviceName string
|
|
}
|
|
|
|
// ExecutorMetrics captures lightweight counters about dispatched opportunities.
|
|
type ExecutorMetrics struct {
|
|
OpportunitiesForwarded int64
|
|
OpportunitiesRejected int64
|
|
LastDispatchTime time.Time
|
|
}
|
|
|
|
// NewExecutor creates a new parser executor that forwards opportunities to the
|
|
// provided dispatcher (typically the arbitrage service).
|
|
func NewExecutor(dispatcher OpportunityDispatcher, log *logpkg.Logger) *Executor {
|
|
if log == nil {
|
|
log = logpkg.New("info", "text", "")
|
|
}
|
|
|
|
return &Executor{
|
|
logger: log,
|
|
dispatcher: dispatcher,
|
|
metrics: &ExecutorMetrics{
|
|
OpportunitiesForwarded: 0,
|
|
OpportunitiesRejected: 0,
|
|
},
|
|
serviceName: "arbitrum-parser",
|
|
}
|
|
}
|
|
|
|
// ExecuteArbitrage forwards the opportunity to the arbitrage service.
|
|
func (e *Executor) ExecuteArbitrage(ctx context.Context, arbOp *pkgtypes.ArbitrageOpportunity) error {
|
|
if arbOp == nil {
|
|
e.metrics.OpportunitiesRejected++
|
|
return fmt.Errorf("arbitrage opportunity cannot be nil")
|
|
}
|
|
|
|
if e.dispatcher == nil {
|
|
e.metrics.OpportunitiesRejected++
|
|
return fmt.Errorf("no dispatcher configured for executor")
|
|
}
|
|
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
|
|
e.logger.Info("Forwarding arbitrage opportunity detected by parser",
|
|
"id", arbOp.ID,
|
|
"path_length", len(arbOp.Path),
|
|
"pools", len(arbOp.Pools),
|
|
"profit", arbOp.NetProfit,
|
|
)
|
|
|
|
if err := e.dispatcher.SubmitBridgeOpportunity(ctx, arbOp); err != nil {
|
|
e.metrics.OpportunitiesRejected++
|
|
e.logger.Error("Failed to forward arbitrage opportunity",
|
|
"id", arbOp.ID,
|
|
"error", err,
|
|
)
|
|
return err
|
|
}
|
|
|
|
e.metrics.OpportunitiesForwarded++
|
|
e.metrics.LastDispatchTime = time.Now()
|
|
return nil
|
|
}
|
|
|
|
// Metrics returns a snapshot of executor metrics.
|
|
func (e *Executor) Metrics() ExecutorMetrics {
|
|
return *e.metrics
|
|
}
|