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>
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package arbitrage
|
|
|
|
import (
|
|
"math/big"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/fraktal/mev-beta/internal/logger"
|
|
"github.com/fraktal/mev-beta/pkg/math"
|
|
pkgtypes "github.com/fraktal/mev-beta/pkg/types"
|
|
)
|
|
|
|
func TestPerformRiskChecksRespectsDailyLossLimitDecimals(t *testing.T) {
|
|
log := logger.New("debug", "text", "")
|
|
dc := math.NewDecimalConverter()
|
|
|
|
maxPos, _ := dc.FromString("10", 18, "ETH")
|
|
dailyLimit, _ := dc.FromString("0.5", 18, "ETH")
|
|
profitTarget, _ := dc.FromString("1", 18, "ETH")
|
|
zero, _ := dc.FromString("0", 18, "ETH")
|
|
|
|
framework := &LiveExecutionFramework{
|
|
logger: log,
|
|
decimalConverter: dc,
|
|
config: FrameworkConfig{
|
|
MaxPositionSize: maxPos,
|
|
DailyLossLimit: dailyLimit,
|
|
DailyProfitTarget: profitTarget,
|
|
},
|
|
stats: &FrameworkStats{
|
|
DailyStats: make(map[string]*DailyStats),
|
|
},
|
|
}
|
|
|
|
today := time.Now().Format("2006-01-02")
|
|
framework.stats.DailyStats[today] = &DailyStats{
|
|
Date: today,
|
|
ProfitRealized: zero.Copy(),
|
|
GasCostPaid: zero.Copy(),
|
|
}
|
|
|
|
loss, _ := math.NewUniversalDecimal(big.NewInt(-600000000000000000), 18, "ETH")
|
|
framework.stats.DailyStats[today].NetProfit = loss
|
|
|
|
opportunity := &pkgtypes.ArbitrageOpportunity{
|
|
AmountIn: big.NewInt(1000000000000000000),
|
|
Confidence: 0.95,
|
|
}
|
|
|
|
if framework.performRiskChecks(opportunity) {
|
|
t.Fatalf("expected opportunity to be rejected when loss exceeds limit")
|
|
}
|
|
|
|
acceptableLoss, _ := math.NewUniversalDecimal(big.NewInt(-200000000000000000), 18, "ETH")
|
|
framework.stats.DailyStats[today].NetProfit = acceptableLoss
|
|
|
|
if !framework.performRiskChecks(opportunity) {
|
|
t.Fatalf("expected opportunity to pass when loss within limit")
|
|
}
|
|
}
|
|
|
|
func TestOpportunityHelpersHandleMissingQuantities(t *testing.T) {
|
|
dc := math.NewDecimalConverter()
|
|
profit := big.NewInt(2100000000000000)
|
|
opportunity := &pkgtypes.ArbitrageOpportunity{
|
|
NetProfit: profit,
|
|
}
|
|
|
|
expectedDecimal := universalFromWei(dc, profit, "ETH")
|
|
|
|
resultDecimal := opportunityNetProfitDecimal(dc, opportunity)
|
|
if cmp, err := dc.Compare(resultDecimal, expectedDecimal); err != nil || cmp != 0 {
|
|
t.Fatalf("expected fallback decimal to match wei amount, got %s", dc.ToHumanReadable(resultDecimal))
|
|
}
|
|
|
|
expectedString := ethAmountString(dc, expectedDecimal, nil)
|
|
resultString := opportunityAmountString(dc, opportunity)
|
|
if resultString != expectedString {
|
|
t.Fatalf("expected fallback string %s, got %s", expectedString, resultString)
|
|
}
|
|
}
|
|
|
|
func TestOpportunityHelpersPreferDecimalSnapshots(t *testing.T) {
|
|
dc := math.NewDecimalConverter()
|
|
declared, _ := math.NewUniversalDecimal(big.NewInt(1500000000000000000), 18, "ETH")
|
|
|
|
opportunity := &pkgtypes.ArbitrageOpportunity{
|
|
NetProfit: new(big.Int).Set(declared.Value),
|
|
Quantities: &pkgtypes.OpportunityQuantities{
|
|
NetProfit: pkgtypes.DecimalAmount{
|
|
Value: declared.Value.String(),
|
|
Decimals: declared.Decimals,
|
|
Symbol: declared.Symbol,
|
|
},
|
|
},
|
|
}
|
|
|
|
resultDecimal := opportunityNetProfitDecimal(dc, opportunity)
|
|
if cmp, err := dc.Compare(resultDecimal, declared); err != nil || cmp != 0 {
|
|
t.Fatalf("expected decimal snapshot to be used, got %s", dc.ToHumanReadable(resultDecimal))
|
|
}
|
|
|
|
expectedString := ethAmountString(dc, declared, nil)
|
|
resultString := opportunityAmountString(dc, opportunity)
|
|
if resultString != expectedString {
|
|
t.Fatalf("expected human-readable snapshot %s, got %s", expectedString, resultString)
|
|
}
|
|
}
|