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>
84 lines
3.5 KiB
Go
84 lines
3.5 KiB
Go
package risk
|
|
|
|
import (
|
|
"math/big"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/fraktal/mev-beta/internal/logger"
|
|
"github.com/fraktal/mev-beta/pkg/math"
|
|
)
|
|
|
|
func TestAssessOpportunityProvidesDecimalSnapshots(t *testing.T) {
|
|
log := logger.New("debug", "text", "")
|
|
rm := NewRiskManager(log)
|
|
|
|
expectedProfit := big.NewInt(20000000000000000) // 0.02 ETH
|
|
gasCost := big.NewInt(500000000000000) // 0.0005 ETH
|
|
gasPrice := big.NewInt(3000000000) // 3 gwei
|
|
|
|
assessment := rm.AssessOpportunity("op-1", expectedProfit, gasCost, 0.005, gasPrice)
|
|
if assessment.MaxPositionSizeDecimal == nil {
|
|
t.Fatalf("expected max position size decimal snapshot to be populated")
|
|
}
|
|
if assessment.RecommendedGasDecimal == nil {
|
|
t.Fatalf("expected gas decimal snapshot to be populated")
|
|
}
|
|
|
|
expectedMax := rm.fromWei(assessment.MaxPositionSize, "ETH")
|
|
if cmp, err := rm.decimalConverter.Compare(assessment.MaxPositionSizeDecimal, expectedMax); err != nil || cmp != 0 {
|
|
t.Fatalf("expected position decimal %s to match %s", rm.decimalConverter.ToHumanReadable(assessment.MaxPositionSizeDecimal), rm.decimalConverter.ToHumanReadable(expectedMax))
|
|
}
|
|
|
|
expectedGas := rm.fromWei(assessment.RecommendedGas, "GWEI")
|
|
if cmp, err := rm.decimalConverter.Compare(assessment.RecommendedGasDecimal, expectedGas); err != nil || cmp != 0 {
|
|
t.Fatalf("expected gas decimal %s to match %s", rm.decimalConverter.ToHumanReadable(assessment.RecommendedGasDecimal), rm.decimalConverter.ToHumanReadable(expectedGas))
|
|
}
|
|
}
|
|
|
|
func TestAssessOpportunityRejectsBelowMinimumProfit(t *testing.T) {
|
|
log := logger.New("debug", "text", "")
|
|
rm := NewRiskManager(log)
|
|
|
|
belowThreshold := big.NewInt(5000000000000000) // 0.005 ETH < 0.01 ETH
|
|
gasCost := big.NewInt(100000000000000) // 0.0001 ETH
|
|
gasPrice := big.NewInt(1500000000) // 1.5 gwei
|
|
|
|
assessment := rm.AssessOpportunity("op-2", belowThreshold, gasCost, 0.001, gasPrice)
|
|
if assessment.Acceptable {
|
|
t.Fatalf("expected opportunity below profit threshold to be rejected")
|
|
}
|
|
if !strings.Contains(assessment.Reason, "Profit below minimum threshold") {
|
|
t.Fatalf("unexpected rejection reason: %s", assessment.Reason)
|
|
}
|
|
|
|
thresholdDecimal := rm.minProfitThresholdDecimal
|
|
if cmp, err := rm.decimalConverter.Compare(assessment.MaxPositionSizeDecimal, rm.maxPositionSizeDecimal); err != nil || cmp != 0 {
|
|
t.Fatalf("expected max position decimal to remain default when rejected")
|
|
}
|
|
if cmp, err := rm.decimalConverter.Compare(rm.minProfitThresholdDecimal, thresholdDecimal); err != nil || cmp != 0 {
|
|
t.Fatalf("expected threshold decimal to remain unchanged")
|
|
}
|
|
}
|
|
|
|
func TestRecordTradeTracksDecimalTotals(t *testing.T) {
|
|
log := logger.New("debug", "text", "")
|
|
rm := NewRiskManager(log)
|
|
|
|
profit := big.NewInt(20000000000000000) // 0.02 ETH
|
|
gasCost := big.NewInt(5000000000000000) // 0.005 ETH
|
|
|
|
rm.RecordTrade(true, profit, gasCost)
|
|
|
|
expectedProfit, _ := math.NewUniversalDecimal(profit, 18, "ETH")
|
|
if cmp, err := rm.decimalConverter.Compare(rm.totalProfitDecimal, expectedProfit); err != nil || cmp != 0 {
|
|
t.Fatalf("expected total profit decimal %s to equal %s", rm.decimalConverter.ToHumanReadable(rm.totalProfitDecimal), rm.decimalConverter.ToHumanReadable(expectedProfit))
|
|
}
|
|
|
|
rm.RecordTrade(false, nil, gasCost)
|
|
expectedLoss, _ := math.NewUniversalDecimal(gasCost, 18, "ETH")
|
|
if cmp, err := rm.decimalConverter.Compare(rm.totalLossDecimal, expectedLoss); err != nil || cmp != 0 {
|
|
t.Fatalf("expected total loss decimal %s to equal %s", rm.decimalConverter.ToHumanReadable(rm.totalLossDecimal), rm.decimalConverter.ToHumanReadable(expectedLoss))
|
|
}
|
|
}
|