feat(transport): implement comprehensive universal message bus

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Krypto Kajun
2025-09-19 16:39:14 -05:00
parent fac8a64092
commit c0ec08468c
13 changed files with 5515 additions and 63 deletions

View File

@@ -12,8 +12,8 @@ import (
"github.com/fraktal/mev-beta/internal/logger"
)
// SimpleProfitCalculator provides basic arbitrage profit estimation for integration with scanner
type SimpleProfitCalculator struct {
// ProfitCalculator provides sophisticated arbitrage profit estimation with slippage protection and multi-DEX price feeds
type ProfitCalculator struct {
logger *logger.Logger
minProfitThreshold *big.Int // Minimum profit in wei to consider viable
maxSlippage float64 // Maximum slippage tolerance (e.g., 0.03 for 3%)
@@ -51,9 +51,9 @@ type SimpleOpportunity struct {
MinAmountOut *big.Float // Minimum amount out with slippage protection
}
// NewSimpleProfitCalculator creates a new simplified profit calculator
func NewSimpleProfitCalculator(logger *logger.Logger) *SimpleProfitCalculator {
return &SimpleProfitCalculator{
// NewProfitCalculator creates a new simplified profit calculator
func NewProfitCalculator(logger *logger.Logger) *ProfitCalculator {
return &ProfitCalculator{
logger: logger,
minProfitThreshold: big.NewInt(10000000000000000), // 0.01 ETH minimum (more realistic)
maxSlippage: 0.03, // 3% max slippage
@@ -64,9 +64,9 @@ func NewSimpleProfitCalculator(logger *logger.Logger) *SimpleProfitCalculator {
}
}
// NewSimpleProfitCalculatorWithClient creates a profit calculator with Ethereum client for gas price updates
func NewSimpleProfitCalculatorWithClient(logger *logger.Logger, client *ethclient.Client) *SimpleProfitCalculator {
calc := NewSimpleProfitCalculator(logger)
// NewProfitCalculatorWithClient creates a profit calculator with Ethereum client for gas price updates
func NewProfitCalculatorWithClient(logger *logger.Logger, client *ethclient.Client) *ProfitCalculator {
calc := NewProfitCalculator(logger)
calc.client = client
// Initialize price feed if client is provided
@@ -80,7 +80,7 @@ func NewSimpleProfitCalculatorWithClient(logger *logger.Logger, client *ethclien
}
// AnalyzeSwapOpportunity analyzes a swap event for potential arbitrage profit
func (spc *SimpleProfitCalculator) AnalyzeSwapOpportunity(
func (spc *ProfitCalculator) AnalyzeSwapOpportunity(
ctx context.Context,
tokenA, tokenB common.Address,
amountIn, amountOut *big.Float,
@@ -238,7 +238,7 @@ func (spc *SimpleProfitCalculator) AnalyzeSwapOpportunity(
}
// calculateGasCost estimates the gas cost for an arbitrage transaction
func (spc *SimpleProfitCalculator) calculateGasCost() *big.Float {
func (spc *ProfitCalculator) calculateGasCost() *big.Float {
// Gas cost = Gas price * Gas limit
gasLimit := big.NewInt(int64(spc.gasLimit))
currentGasPrice := spc.GetCurrentGasPrice()
@@ -256,7 +256,7 @@ func (spc *SimpleProfitCalculator) calculateGasCost() *big.Float {
}
// calculateConfidence calculates a confidence score for the opportunity
func (spc *SimpleProfitCalculator) calculateConfidence(opp *SimpleOpportunity) float64 {
func (spc *ProfitCalculator) calculateConfidence(opp *SimpleOpportunity) float64 {
confidence := 0.0
// Base confidence for positive profit
@@ -292,7 +292,7 @@ func (spc *SimpleProfitCalculator) calculateConfidence(opp *SimpleOpportunity) f
}
// FormatEther formats a big.Float ether amount to string (public method)
func (spc *SimpleProfitCalculator) FormatEther(ether *big.Float) string {
func (spc *ProfitCalculator) FormatEther(ether *big.Float) string {
if ether == nil {
return "0.000000"
}
@@ -300,7 +300,7 @@ func (spc *SimpleProfitCalculator) FormatEther(ether *big.Float) string {
}
// UpdateGasPrice updates the current gas price for calculations
func (spc *SimpleProfitCalculator) UpdateGasPrice(gasPrice *big.Int) {
func (spc *ProfitCalculator) UpdateGasPrice(gasPrice *big.Int) {
spc.gasPriceMutex.Lock()
defer spc.gasPriceMutex.Unlock()
@@ -311,14 +311,14 @@ func (spc *SimpleProfitCalculator) UpdateGasPrice(gasPrice *big.Int) {
}
// GetCurrentGasPrice gets the current gas price (thread-safe)
func (spc *SimpleProfitCalculator) GetCurrentGasPrice() *big.Int {
func (spc *ProfitCalculator) GetCurrentGasPrice() *big.Int {
spc.gasPriceMutex.RLock()
defer spc.gasPriceMutex.RUnlock()
return new(big.Int).Set(spc.gasPrice)
}
// startGasPriceUpdater starts a background goroutine to update gas prices
func (spc *SimpleProfitCalculator) startGasPriceUpdater() {
func (spc *ProfitCalculator) startGasPriceUpdater() {
ticker := time.NewTicker(spc.gasPriceUpdateInterval)
defer ticker.Stop()
@@ -333,7 +333,7 @@ func (spc *SimpleProfitCalculator) startGasPriceUpdater() {
}
// updateGasPriceFromNetwork fetches current gas price from the network
func (spc *SimpleProfitCalculator) updateGasPriceFromNetwork() {
func (spc *ProfitCalculator) updateGasPriceFromNetwork() {
if spc.client == nil {
return
}
@@ -355,14 +355,14 @@ func (spc *SimpleProfitCalculator) updateGasPriceFromNetwork() {
}
// SetMinProfitThreshold sets the minimum profit threshold
func (spc *SimpleProfitCalculator) SetMinProfitThreshold(threshold *big.Int) {
func (spc *ProfitCalculator) SetMinProfitThreshold(threshold *big.Int) {
spc.minProfitThreshold = threshold
spc.logger.Info(fmt.Sprintf("Updated minimum profit threshold to %s ETH",
new(big.Float).Quo(new(big.Float).SetInt(threshold), big.NewFloat(1e18))))
}
// GetPriceFeedStats returns statistics about the price feed
func (spc *SimpleProfitCalculator) GetPriceFeedStats() map[string]interface{} {
func (spc *ProfitCalculator) GetPriceFeedStats() map[string]interface{} {
if spc.priceFeed != nil {
return spc.priceFeed.GetPriceStats()
}
@@ -372,12 +372,12 @@ func (spc *SimpleProfitCalculator) GetPriceFeedStats() map[string]interface{} {
}
// HasPriceFeed returns true if the calculator has an active price feed
func (spc *SimpleProfitCalculator) HasPriceFeed() bool {
func (spc *ProfitCalculator) HasPriceFeed() bool {
return spc.priceFeed != nil
}
// Stop gracefully shuts down the profit calculator
func (spc *SimpleProfitCalculator) Stop() {
func (spc *ProfitCalculator) Stop() {
if spc.priceFeed != nil {
spc.priceFeed.Stop()
spc.logger.Info("Price feed stopped")