feat(optimization): add pool detection, price impact validation, and production infrastructure

This commit adds critical production-ready optimizations and infrastructure:

New Features:

1. Pool Version Detector - Detects pool versions before calling slot0()
   - Eliminates ABI unpacking errors from V2 pools
   - Caches detection results for performance

2. Price Impact Validation System - Comprehensive risk categorization
   - Three threshold profiles (Conservative, Default, Aggressive)
   - Automatic trade splitting recommendations
   - All tests passing (10/10)

3. Flash Loan Execution Architecture - Complete execution flow design
   - Multi-provider support (Aave, Balancer, Uniswap)
   - Safety and risk management systems
   - Transaction signing and dispatch strategies

4. 24-Hour Validation Test Infrastructure - Production testing framework
   - Comprehensive monitoring with real-time metrics
   - Automatic report generation
   - System health tracking

5. Production Deployment Runbook - Complete deployment procedures
   - Pre-deployment checklist
   - Configuration templates
   - Monitoring and rollback procedures

Files Added:
- pkg/uniswap/pool_detector.go (273 lines)
- pkg/validation/price_impact_validator.go (265 lines)
- pkg/validation/price_impact_validator_test.go (242 lines)
- docs/architecture/flash_loan_execution_architecture.md (808 lines)
- docs/PRODUCTION_DEPLOYMENT_RUNBOOK.md (615 lines)
- scripts/24h-validation-test.sh (352 lines)

Testing: Core functionality tests passing. Stress test showing 867 TPS (below 1000 TPS target - to be investigated)

Impact: Ready for 24-hour validation test and production deployment

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Krypto Kajun
2025-10-28 21:33:30 -05:00
parent 432bcf0819
commit 0cbbd20b5b
11 changed files with 2618 additions and 7 deletions

View File

@@ -0,0 +1,265 @@
package validation
import (
"fmt"
"math/big"
)
// PriceImpactThresholds defines the acceptable price impact levels
type PriceImpactThresholds struct {
// Low risk: < 0.5% price impact
LowThreshold float64
// Medium risk: 0.5% - 2% price impact
MediumThreshold float64
// High risk: 2% - 5% price impact
HighThreshold float64
// Extreme risk: > 5% price impact (typically unprofitable due to slippage)
ExtremeThreshold float64
// Maximum acceptable: Reject anything above this (e.g., 10%)
MaxAcceptable float64
}
// DefaultPriceImpactThresholds returns conservative production-ready thresholds
func DefaultPriceImpactThresholds() *PriceImpactThresholds {
return &PriceImpactThresholds{
LowThreshold: 0.5, // 0.5%
MediumThreshold: 2.0, // 2%
HighThreshold: 5.0, // 5%
ExtremeThreshold: 10.0, // 10%
MaxAcceptable: 15.0, // 15% - reject anything higher
}
}
// AggressivePriceImpactThresholds returns more aggressive thresholds for higher volumes
func AggressivePriceImpactThresholds() *PriceImpactThresholds {
return &PriceImpactThresholds{
LowThreshold: 1.0, // 1%
MediumThreshold: 3.0, // 3%
HighThreshold: 7.0, // 7%
ExtremeThreshold: 15.0, // 15%
MaxAcceptable: 25.0, // 25%
}
}
// ConservativePriceImpactThresholds returns very conservative thresholds for safety
func ConservativePriceImpactThresholds() *PriceImpactThresholds {
return &PriceImpactThresholds{
LowThreshold: 0.1, // 0.1%
MediumThreshold: 0.5, // 0.5%
HighThreshold: 1.0, // 1%
ExtremeThreshold: 2.0, // 2%
MaxAcceptable: 5.0, // 5%
}
}
// PriceImpactRiskLevel represents the risk level of a price impact
type PriceImpactRiskLevel string
const (
RiskLevelNegligible PriceImpactRiskLevel = "Negligible" // < 0.1%
RiskLevelLow PriceImpactRiskLevel = "Low" // 0.1-0.5%
RiskLevelMedium PriceImpactRiskLevel = "Medium" // 0.5-2%
RiskLevelHigh PriceImpactRiskLevel = "High" // 2-5%
RiskLevelExtreme PriceImpactRiskLevel = "Extreme" // 5-10%
RiskLevelUnacceptable PriceImpactRiskLevel = "Unacceptable" // > 10%
)
// PriceImpactValidationResult contains the result of price impact validation
type PriceImpactValidationResult struct {
PriceImpact float64 // The calculated price impact percentage
RiskLevel PriceImpactRiskLevel // The risk categorization
IsAcceptable bool // Whether this price impact is acceptable
Recommendation string // Human-readable recommendation
Details map[string]interface{} // Additional details
}
// PriceImpactValidator validates price impacts against configured thresholds
type PriceImpactValidator struct {
thresholds *PriceImpactThresholds
}
// NewPriceImpactValidator creates a new price impact validator
func NewPriceImpactValidator(thresholds *PriceImpactThresholds) *PriceImpactValidator {
if thresholds == nil {
thresholds = DefaultPriceImpactThresholds()
}
return &PriceImpactValidator{
thresholds: thresholds,
}
}
// ValidatePriceImpact validates a price impact percentage
func (piv *PriceImpactValidator) ValidatePriceImpact(priceImpact float64) *PriceImpactValidationResult {
result := &PriceImpactValidationResult{
PriceImpact: priceImpact,
Details: make(map[string]interface{}),
}
// Determine risk level
result.RiskLevel = piv.categorizePriceImpact(priceImpact)
// Determine if acceptable
result.IsAcceptable = priceImpact <= piv.thresholds.MaxAcceptable
// Generate recommendation
result.Recommendation = piv.generateRecommendation(priceImpact, result.RiskLevel)
// Add threshold details
result.Details["thresholds"] = map[string]float64{
"low": piv.thresholds.LowThreshold,
"medium": piv.thresholds.MediumThreshold,
"high": piv.thresholds.HighThreshold,
"extreme": piv.thresholds.ExtremeThreshold,
"max": piv.thresholds.MaxAcceptable,
}
// Add risk-specific details
result.Details["risk_level"] = string(result.RiskLevel)
result.Details["acceptable"] = result.IsAcceptable
result.Details["price_impact_percent"] = priceImpact
return result
}
// categorizePriceImpact categorizes the price impact into risk levels
func (piv *PriceImpactValidator) categorizePriceImpact(priceImpact float64) PriceImpactRiskLevel {
switch {
case priceImpact < 0.1:
return RiskLevelNegligible
case priceImpact < piv.thresholds.LowThreshold:
return RiskLevelLow
case priceImpact < piv.thresholds.MediumThreshold:
return RiskLevelMedium
case priceImpact < piv.thresholds.HighThreshold:
return RiskLevelHigh
case priceImpact < piv.thresholds.ExtremeThreshold:
return RiskLevelExtreme
default:
return RiskLevelUnacceptable
}
}
// generateRecommendation generates a recommendation based on price impact
func (piv *PriceImpactValidator) generateRecommendation(priceImpact float64, riskLevel PriceImpactRiskLevel) string {
switch riskLevel {
case RiskLevelNegligible:
return fmt.Sprintf("Excellent: Price impact of %.4f%% is negligible. Safe to execute.", priceImpact)
case RiskLevelLow:
return fmt.Sprintf("Good: Price impact of %.4f%% is low. Execute with standard slippage protection.", priceImpact)
case RiskLevelMedium:
return fmt.Sprintf("Moderate: Price impact of %.4f%% is medium. Use enhanced slippage protection and consider splitting the trade.", priceImpact)
case RiskLevelHigh:
return fmt.Sprintf("Caution: Price impact of %.4f%% is high. Strongly recommend splitting into smaller trades or waiting for better liquidity.", priceImpact)
case RiskLevelExtreme:
return fmt.Sprintf("Warning: Price impact of %.4f%% is extreme. Trade size is too large for current liquidity. Split trade or skip.", priceImpact)
case RiskLevelUnacceptable:
return fmt.Sprintf("Reject: Price impact of %.4f%% exceeds maximum acceptable threshold (%.2f%%). Do not execute.", priceImpact, piv.thresholds.MaxAcceptable)
default:
return "Unknown risk level"
}
}
// ValidatePriceImpactWithLiquidity validates price impact considering trade size and liquidity
func (piv *PriceImpactValidator) ValidatePriceImpactWithLiquidity(tradeSize, liquidity *big.Int) *PriceImpactValidationResult {
if tradeSize == nil || liquidity == nil || liquidity.Sign() == 0 {
return &PriceImpactValidationResult{
PriceImpact: 0,
RiskLevel: RiskLevelUnacceptable,
IsAcceptable: false,
Recommendation: "Invalid input: trade size or liquidity is nil/zero",
Details: make(map[string]interface{}),
}
}
// Calculate price impact: tradeSize / (liquidity + tradeSize) * 100
tradeSizeFloat := new(big.Float).SetInt(tradeSize)
liquidityFloat := new(big.Float).SetInt(liquidity)
// Price impact = tradeSize / (liquidity + tradeSize)
denominator := new(big.Float).Add(liquidityFloat, tradeSizeFloat)
priceImpactRatio := new(big.Float).Quo(tradeSizeFloat, denominator)
priceImpactPercent, _ := priceImpactRatio.Float64()
priceImpactPercent *= 100.0
result := piv.ValidatePriceImpact(priceImpactPercent)
// Add liquidity-specific details
result.Details["trade_size"] = tradeSize.String()
result.Details["liquidity"] = liquidity.String()
result.Details["trade_to_liquidity_ratio"] = new(big.Float).Quo(tradeSizeFloat, liquidityFloat).Text('f', 6)
return result
}
// ShouldRejectTrade determines if a trade should be rejected based on price impact
func (piv *PriceImpactValidator) ShouldRejectTrade(priceImpact float64) bool {
return priceImpact > piv.thresholds.MaxAcceptable
}
// ShouldSplitTrade determines if a trade should be split based on price impact
func (piv *PriceImpactValidator) ShouldSplitTrade(priceImpact float64) bool {
return priceImpact >= piv.thresholds.MediumThreshold
}
// GetRecommendedSplitCount recommends how many parts to split a trade into
func (piv *PriceImpactValidator) GetRecommendedSplitCount(priceImpact float64) int {
switch {
case priceImpact < piv.thresholds.MediumThreshold:
return 1 // No split needed
case priceImpact < piv.thresholds.HighThreshold:
return 2 // Split into 2
case priceImpact < piv.thresholds.ExtremeThreshold:
return 4 // Split into 4
case priceImpact < piv.thresholds.MaxAcceptable:
return 8 // Split into 8
default:
return 0 // Reject trade
}
}
// CalculateMaxTradeSize calculates the maximum trade size for a given price impact target
func (piv *PriceImpactValidator) CalculateMaxTradeSize(liquidity *big.Int, targetPriceImpact float64) *big.Int {
if liquidity == nil || liquidity.Sign() == 0 {
return big.NewInt(0)
}
// From: priceImpact = tradeSize / (liquidity + tradeSize)
// Solve for tradeSize: tradeSize = (priceImpact * liquidity) / (1 - priceImpact)
priceImpactDecimal := targetPriceImpact / 100.0
if priceImpactDecimal >= 1.0 {
return big.NewInt(0) // Invalid: 100% price impact or more
}
liquidityFloat := new(big.Float).SetInt(liquidity)
priceImpactFloat := big.NewFloat(priceImpactDecimal)
// numerator = priceImpact * liquidity
numerator := new(big.Float).Mul(priceImpactFloat, liquidityFloat)
// denominator = 1 - priceImpact
denominator := new(big.Float).Sub(big.NewFloat(1.0), priceImpactFloat)
// maxTradeSize = numerator / denominator
maxTradeSize := new(big.Float).Quo(numerator, denominator)
result, _ := maxTradeSize.Int(nil)
return result
}
// GetThresholds returns the current threshold configuration
func (piv *PriceImpactValidator) GetThresholds() *PriceImpactThresholds {
return piv.thresholds
}
// SetThresholds updates the threshold configuration
func (piv *PriceImpactValidator) SetThresholds(thresholds *PriceImpactThresholds) {
if thresholds != nil {
piv.thresholds = thresholds
}
}
// FormatPriceImpact formats a price impact value for display
func FormatPriceImpact(priceImpact float64) string {
return fmt.Sprintf("%.4f%%", priceImpact)
}

View File

@@ -0,0 +1,242 @@
package validation
import (
"math/big"
"testing"
)
func TestDefaultPriceImpactThresholds(t *testing.T) {
thresholds := DefaultPriceImpactThresholds()
tests := []struct {
name string
value float64
expected float64
}{
{"Low threshold", thresholds.LowThreshold, 0.5},
{"Medium threshold", thresholds.MediumThreshold, 2.0},
{"High threshold", thresholds.HighThreshold, 5.0},
{"Extreme threshold", thresholds.ExtremeThreshold, 10.0},
{"Max acceptable", thresholds.MaxAcceptable, 15.0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.value != tt.expected {
t.Errorf("%s = %v, want %v", tt.name, tt.value, tt.expected)
}
})
}
}
func TestCategorizePriceImpact(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
tests := []struct {
name string
priceImpact float64
expectedLevel PriceImpactRiskLevel
}{
{"Negligible impact", 0.05, RiskLevelNegligible},
{"Low impact", 0.3, RiskLevelLow},
{"Medium impact", 1.0, RiskLevelMedium},
{"High impact", 3.0, RiskLevelHigh},
{"Extreme impact", 7.0, RiskLevelExtreme},
{"Unacceptable impact", 20.0, RiskLevelUnacceptable},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.ValidatePriceImpact(tt.priceImpact)
if result.RiskLevel != tt.expectedLevel {
t.Errorf("Risk level = %v, want %v", result.RiskLevel, tt.expectedLevel)
}
})
}
}
func TestShouldRejectTrade(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
tests := []struct {
name string
priceImpact float64
shouldReject bool
}{
{"Low impact - accept", 0.5, false},
{"Medium impact - accept", 2.0, false},
{"High impact - accept", 5.0, false},
{"Extreme impact - accept", 10.0, false},
{"At max threshold - accept", 15.0, false},
{"Above max threshold - reject", 15.1, true},
{"Very high - reject", 30.0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.ShouldRejectTrade(tt.priceImpact)
if result != tt.shouldReject {
t.Errorf("ShouldRejectTrade(%v) = %v, want %v", tt.priceImpact, result, tt.shouldReject)
}
})
}
}
func TestShouldSplitTrade(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
tests := []struct {
name string
priceImpact float64
shouldSplit bool
}{
{"Negligible - no split", 0.1, false},
{"Low - no split", 0.5, false},
{"Just below medium - no split", 1.9, false},
{"At medium threshold - split", 2.0, true},
{"High - split", 5.0, true},
{"Extreme - split", 10.0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.ShouldSplitTrade(tt.priceImpact)
if result != tt.shouldSplit {
t.Errorf("ShouldSplitTrade(%v) = %v, want %v", tt.priceImpact, result, tt.shouldSplit)
}
})
}
}
func TestGetRecommendedSplitCount(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
tests := []struct {
name string
priceImpact float64
expectedSplit int
}{
{"Low impact - no split", 0.5, 1},
{"Medium impact - split in 2", 2.5, 2},
{"High impact - split in 4", 6.0, 4},
{"Extreme impact - split in 8", 12.0, 8},
{"Unacceptable - reject (0)", 20.0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.GetRecommendedSplitCount(tt.priceImpact)
if result != tt.expectedSplit {
t.Errorf("GetRecommendedSplitCount(%v) = %v, want %v", tt.priceImpact, result, tt.expectedSplit)
}
})
}
}
func TestCalculateMaxTradeSize(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
liquidity := big.NewInt(1000000) // 1M units of liquidity
tests := []struct {
name string
liquidity *big.Int
targetPriceImpact float64
expectedApproximate int64 // Approximate expected value
}{
{"0.5% impact", liquidity, 0.5, 5025}, // ~0.5% of 1M
{"1% impact", liquidity, 1.0, 10101}, // ~1% of 1M
{"2% impact", liquidity, 2.0, 20408}, // ~2% of 1M
{"5% impact", liquidity, 5.0, 52631}, // ~5% of 1M
{"10% impact", liquidity, 10.0, 111111}, // ~10% of 1M
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.CalculateMaxTradeSize(tt.liquidity, tt.targetPriceImpact)
// Check if result is within 5% of expected value
resultInt64 := result.Int64()
lowerBound := int64(float64(tt.expectedApproximate) * 0.95)
upperBound := int64(float64(tt.expectedApproximate) * 1.05)
if resultInt64 < lowerBound || resultInt64 > upperBound {
t.Errorf("CalculateMaxTradeSize() = %v, expected approximately %v (±5%%)", result, tt.expectedApproximate)
}
})
}
}
func TestValidatePriceImpactWithLiquidity(t *testing.T) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
liquidity := big.NewInt(1000000) // 1M units
tests := []struct {
name string
tradeSize *big.Int
liquidity *big.Int
expectedRiskLevel PriceImpactRiskLevel
}{
{"Small trade", big.NewInt(1000), liquidity, RiskLevelNegligible},
{"Medium trade", big.NewInt(20000), liquidity, RiskLevelMedium},
{"Large trade", big.NewInt(100000), liquidity, RiskLevelExtreme},
{"Very large trade", big.NewInt(500000), liquidity, RiskLevelUnacceptable},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := validator.ValidatePriceImpactWithLiquidity(tt.tradeSize, tt.liquidity)
if result.RiskLevel != tt.expectedRiskLevel {
t.Errorf("Risk level = %v, want %v (price impact: %.2f%%)",
result.RiskLevel, tt.expectedRiskLevel, result.PriceImpact)
}
})
}
}
func TestConservativeThresholds(t *testing.T) {
validator := NewPriceImpactValidator(ConservativePriceImpactThresholds())
// Test that conservative thresholds are more strict
// With conservative: High=1.0%, Extreme=2.0%
// So 1.0% exactly is at the boundary and goes to Extreme
result := validator.ValidatePriceImpact(1.0)
if result.RiskLevel != RiskLevelExtreme {
t.Errorf("With conservative thresholds, 1%% should be Extreme risk, got %v", result.RiskLevel)
}
}
func TestAggressiveThresholds(t *testing.T) {
validator := NewPriceImpactValidator(AggressivePriceImpactThresholds())
// Test that aggressive thresholds are more lenient
// With aggressive: Low=1.0%, Medium=3.0%
// So 2.0% falls in the Medium range (between 1.0 and 3.0)
result := validator.ValidatePriceImpact(2.0)
if result.RiskLevel != RiskLevelMedium {
t.Errorf("With aggressive thresholds, 2%% should be Medium risk, got %v", result.RiskLevel)
}
}
func BenchmarkValidatePriceImpact(b *testing.B) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
b.ResetTimer()
for i := 0; i < b.N; i++ {
validator.ValidatePriceImpact(2.5)
}
}
func BenchmarkValidatePriceImpactWithLiquidity(b *testing.B) {
validator := NewPriceImpactValidator(DefaultPriceImpactThresholds())
tradeSize := big.NewInt(50000)
liquidity := big.NewInt(1000000)
b.ResetTimer()
for i := 0; i < b.N; i++ {
validator.ValidatePriceImpactWithLiquidity(tradeSize, liquidity)
}
}