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)) } }