Files
mev-beta/pkg/arbitrage/live_execution_framework_test.go
Krypto Kajun 850223a953 fix(multicall): resolve critical multicall parsing corruption issues
- Added comprehensive bounds checking to prevent buffer overruns in multicall parsing
- Implemented graduated validation system (Strict/Moderate/Permissive) to reduce false positives
- Added LRU caching system for address validation with 10-minute TTL
- Enhanced ABI decoder with missing Universal Router and Arbitrum-specific DEX signatures
- Fixed duplicate function declarations and import conflicts across multiple files
- Added error recovery mechanisms with multiple fallback strategies
- Updated tests to handle new validation behavior for suspicious addresses
- Fixed parser test expectations for improved validation system
- Applied gofmt formatting fixes to ensure code style compliance
- Fixed mutex copying issues in monitoring package by introducing MetricsSnapshot
- Resolved critical security vulnerabilities in heuristic address extraction
- Progress: Updated TODO audit from 10% to 35% complete

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-17 00:12:55 -05:00

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