package risk import ( "fmt" "math/big" "github.com/fraktal/mev-beta/internal/logger" ) // ProfitTier represents a profit threshold tier with specific requirements type ProfitTier struct { Name string MinProfitMarginBps int64 // Minimum profit margin in basis points MaxProfitMarginBps int64 // Maximum profit margin in basis points (exclusive) MinExecutionSizeETH float64 // Minimum execution size in ETH MaxGasCostRatio float64 // Maximum gas cost as ratio of profit MaxSlippageBps int64 // Maximum slippage in basis points RequireHighLiquidity bool // Require high liquidity pools Description string } // ProfitTierSystem manages profit validation across different tiers type ProfitTierSystem struct { logger *logger.Logger tiers []ProfitTier } // NewProfitTierSystem creates a new profit tier system func NewProfitTierSystem(logger *logger.Logger) *ProfitTierSystem { return &ProfitTierSystem{ logger: logger, tiers: []ProfitTier{ { Name: "Ultra High Margin", MinProfitMarginBps: 1000, // 10%+ MaxProfitMarginBps: 100000, MinExecutionSizeETH: 0.05, MaxGasCostRatio: 0.3, MaxSlippageBps: 200, // 2% RequireHighLiquidity: false, Description: "Rare high-margin opportunities (10%+) - Minimum 0.05 ETH execution", }, { Name: "High Margin", MinProfitMarginBps: 500, // 5-10% MaxProfitMarginBps: 1000, MinExecutionSizeETH: 0.1, MaxGasCostRatio: 0.4, MaxSlippageBps: 150, // 1.5% RequireHighLiquidity: false, Description: "High-margin opportunities (5-10%) - Minimum 0.1 ETH execution", }, { Name: "Medium Margin", MinProfitMarginBps: 200, // 2-5% MaxProfitMarginBps: 500, MinExecutionSizeETH: 0.5, MaxGasCostRatio: 0.35, MaxSlippageBps: 100, // 1% RequireHighLiquidity: true, Description: "Medium-margin opportunities (2-5%) - Minimum 0.5 ETH execution, high liquidity required", }, { Name: "Standard Margin", MinProfitMarginBps: 100, // 1-2% MaxProfitMarginBps: 200, MinExecutionSizeETH: 1.0, MaxGasCostRatio: 0.25, MaxSlippageBps: 75, // 0.75% RequireHighLiquidity: true, Description: "Standard-margin opportunities (1-2%) - Minimum 1 ETH execution, high liquidity required", }, { Name: "Low Margin", MinProfitMarginBps: 50, // 0.5-1% MaxProfitMarginBps: 100, MinExecutionSizeETH: 2.0, MaxGasCostRatio: 0.15, MaxSlippageBps: 50, // 0.5% RequireHighLiquidity: true, Description: "Low-margin opportunities (0.5-1%) - Minimum 2 ETH execution, high liquidity required, strict gas limits", }, }, } } // ValidateOpportunity validates an arbitrage opportunity against tier requirements func (pts *ProfitTierSystem) ValidateOpportunity( profitMarginBps int64, executionSizeETH float64, gasCostRatio float64, slippageBps int64, hasHighLiquidity bool, ) (*ValidationResult, error) { // Find applicable tier tier := pts.findTier(profitMarginBps) if tier == nil { return &ValidationResult{ IsValid: false, Tier: nil, FailureReason: fmt.Sprintf("Profit margin %d bps is below minimum threshold (50 bps / 0.5%%)", profitMarginBps), }, nil } // Validate execution size if executionSizeETH < tier.MinExecutionSizeETH { return &ValidationResult{ IsValid: false, Tier: tier, FailureReason: fmt.Sprintf("Execution size %.4f ETH is below tier minimum %.2f ETH for %s tier", executionSizeETH, tier.MinExecutionSizeETH, tier.Name), }, nil } // Validate gas cost ratio if gasCostRatio > tier.MaxGasCostRatio { return &ValidationResult{ IsValid: false, Tier: tier, FailureReason: fmt.Sprintf("Gas cost ratio %.2f%% exceeds tier maximum %.2f%% for %s tier", gasCostRatio*100, tier.MaxGasCostRatio*100, tier.Name), }, nil } // Validate slippage if slippageBps > tier.MaxSlippageBps { return &ValidationResult{ IsValid: false, Tier: tier, FailureReason: fmt.Sprintf("Slippage %d bps exceeds tier maximum %d bps for %s tier", slippageBps, tier.MaxSlippageBps, tier.Name), }, nil } // Validate liquidity requirement if tier.RequireHighLiquidity && !hasHighLiquidity { return &ValidationResult{ IsValid: false, Tier: tier, FailureReason: fmt.Sprintf("High liquidity required for %s tier but not available", tier.Name), }, nil } // All checks passed return &ValidationResult{ IsValid: true, Tier: tier, FailureReason: "", }, nil } // findTier finds the appropriate tier for a given profit margin func (pts *ProfitTierSystem) findTier(profitMarginBps int64) *ProfitTier { for i := range pts.tiers { tier := &pts.tiers[i] if profitMarginBps >= tier.MinProfitMarginBps && profitMarginBps < tier.MaxProfitMarginBps { return tier } } return nil } // GetTierForMargin returns the tier for a specific profit margin func (pts *ProfitTierSystem) GetTierForMargin(profitMarginBps int64) *ProfitTier { return pts.findTier(profitMarginBps) } // GetAllTiers returns all defined tiers func (pts *ProfitTierSystem) GetAllTiers() []ProfitTier { return pts.tiers } // CalculateProfitMarginBps calculates profit margin in basis points func CalculateProfitMarginBps(profit, revenue *big.Float) int64 { if revenue.Cmp(big.NewFloat(0)) == 0 { return 0 } // margin = (profit / revenue) * 10000 margin := new(big.Float).Quo(profit, revenue) margin.Mul(margin, big.NewFloat(10000)) marginInt, _ := margin.Int64() return marginInt } // CalculateGasCostRatio calculates gas cost as a ratio of profit func CalculateGasCostRatio(gasCost, profit *big.Float) float64 { if profit.Cmp(big.NewFloat(0)) == 0 { return 1.0 // 100% if no profit } ratio := new(big.Float).Quo(gasCost, profit) ratioFloat, _ := ratio.Float64() return ratioFloat } // ValidationResult contains the result of tier validation type ValidationResult struct { IsValid bool Tier *ProfitTier FailureReason string } // EstimateMinExecutionSize estimates minimum execution size for a profit margin func (pts *ProfitTierSystem) EstimateMinExecutionSize(profitMarginBps int64) float64 { tier := pts.findTier(profitMarginBps) if tier == nil { // Default to highest requirement if below minimum return 2.0 } return tier.MinExecutionSizeETH } // GetTierSummary returns a summary of all tiers for logging func (pts *ProfitTierSystem) GetTierSummary() string { summary := "Profit Tier System Configuration:\n" for i, tier := range pts.tiers { summary += fmt.Sprintf(" Tier %d: %s\n", i+1, tier.Name) summary += fmt.Sprintf(" Margin: %.2f%% - %.2f%%\n", float64(tier.MinProfitMarginBps)/100, float64(tier.MaxProfitMarginBps)/100) summary += fmt.Sprintf(" Min Size: %.2f ETH\n", tier.MinExecutionSizeETH) summary += fmt.Sprintf(" Max Gas Ratio: %.1f%%\n", tier.MaxGasCostRatio*100) summary += fmt.Sprintf(" Max Slippage: %.2f%%\n", float64(tier.MaxSlippageBps)/100) summary += fmt.Sprintf(" High Liquidity Required: %v\n", tier.RequireHighLiquidity) } return summary } // IsHighLiquidity determines if a pool has high liquidity func IsHighLiquidity(liquidityETH float64) bool { // Threshold: 50 ETH+ liquidity is considered high return liquidityETH >= 50.0 }