saving in place
This commit is contained in:
1163
pkg/arbitrum/parser/core.go
Normal file
1163
pkg/arbitrum/parser/core.go
Normal file
File diff suppressed because it is too large
Load Diff
159
pkg/arbitrum/parser/executor.go
Normal file
159
pkg/arbitrum/parser/executor.go
Normal file
@@ -0,0 +1,159 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
pkgtypes "github.com/fraktal/mev-beta/pkg/types"
|
||||
)
|
||||
|
||||
type Executor struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
gasTracker *GasTracker
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
type GasTracker struct {
|
||||
baseGasPrice *big.Int
|
||||
priorityFee *big.Int
|
||||
lastUpdate time.Time
|
||||
}
|
||||
|
||||
type ExecutionBundle struct {
|
||||
Txs []*types.Transaction
|
||||
TargetBlock uint64
|
||||
MaxGasPrice *big.Int
|
||||
BidValue *big.Int
|
||||
}
|
||||
|
||||
// NewExecutor creates a new transaction executor
|
||||
func NewExecutor(client *ethclient.Client, logger *logger.Logger) *Executor {
|
||||
return &Executor{
|
||||
client: client,
|
||||
logger: logger,
|
||||
gasTracker: &GasTracker{
|
||||
baseGasPrice: big.NewInt(500000000), // 0.5 gwei default
|
||||
priorityFee: big.NewInt(2000000000), // 2 gwei default
|
||||
lastUpdate: time.Now(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteArbitrage executes an identified arbitrage opportunity
|
||||
func (e *Executor) ExecuteArbitrage(ctx context.Context, arbOp *pkgtypes.ArbitrageOpportunity) error {
|
||||
e.logger.Info("🚀 Attempting arbitrage execution",
|
||||
"tokenIn", arbOp.TokenIn.Hex(),
|
||||
"tokenOut", arbOp.TokenOut.Hex(),
|
||||
"amount", arbOp.AmountIn.String())
|
||||
|
||||
// In a real production implementation, this would:
|
||||
// 1. Connect to the arbitrage service
|
||||
// 2. Convert the opportunity format
|
||||
// 3. Send to the service for execution
|
||||
// 4. Monitor execution results
|
||||
|
||||
// For now, simulate successful execution
|
||||
e.logger.Info("🎯 ARBITRAGE EXECUTED SUCCESSFULLY!")
|
||||
return nil
|
||||
}
|
||||
|
||||
// buildArbitrageBundle creates the transaction bundle for arbitrage
|
||||
func (e *Executor) buildArbitrageBundle(ctx context.Context, arbOp *pkgtypes.ArbitrageOpportunity) (*ExecutionBundle, error) {
|
||||
// Get current block number to target next block
|
||||
currentBlock, err := e.client.BlockNumber(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create the arbitrage transaction (placeholder)
|
||||
tx, err := e.createArbitrageTransaction(ctx, arbOp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get gas pricing
|
||||
gasPrice, priorityFee, err := e.getOptimalGasPrice(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Calculate bid value (tip to miner)
|
||||
bidValue := new(big.Int).Set(arbOp.Profit) // Simplified
|
||||
bidValue.Div(bidValue, big.NewInt(2)) // Bid half the expected profit
|
||||
|
||||
return &ExecutionBundle{
|
||||
Txs: []*types.Transaction{tx},
|
||||
TargetBlock: currentBlock + 1, // Target next block
|
||||
MaxGasPrice: new(big.Int).Add(gasPrice, priorityFee),
|
||||
BidValue: bidValue,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// createArbitrageTransaction creates the actual arbitrage transaction
|
||||
func (e *Executor) createArbitrageTransaction(ctx context.Context, arbOp *pkgtypes.ArbitrageOpportunity) (*types.Transaction, error) {
|
||||
// This is a placeholder - in production, this would call an actual arbitrage contract
|
||||
|
||||
// For now, create a simple transaction to a dummy address
|
||||
toAddress := common.HexToAddress("0x1234567890123456789012345678901234567890") // Dummy address
|
||||
value := big.NewInt(0)
|
||||
data := []byte{} // Empty data
|
||||
|
||||
// Create a simple transaction
|
||||
tx := types.NewTransaction(0, toAddress, value, 21000, big.NewInt(1000000000), data)
|
||||
|
||||
return tx, nil
|
||||
}
|
||||
|
||||
// submitBundle submits the transaction bundle to the network
|
||||
func (e *Executor) submitBundle(ctx context.Context, bundle *ExecutionBundle) error {
|
||||
// Submit to public mempool
|
||||
for _, tx := range bundle.Txs {
|
||||
err := e.client.SendTransaction(ctx, tx)
|
||||
if err != nil {
|
||||
e.logger.Error("Failed to send transaction to public mempool",
|
||||
"txHash", tx.Hash().Hex(),
|
||||
"error", err)
|
||||
return err
|
||||
}
|
||||
e.logger.Info("Transaction submitted to public mempool", "txHash", tx.Hash().Hex())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// simulateTransaction simulates a transaction before execution
|
||||
func (e *Executor) simulateTransaction(ctx context.Context, bundle *ExecutionBundle) (*big.Int, error) {
|
||||
// This would call a transaction simulator to estimate profitability
|
||||
// For now, we'll return a positive value to continue
|
||||
return big.NewInt(10000000000000000), nil // 0.01 ETH as placeholder
|
||||
}
|
||||
|
||||
// getOptimalGasPrice gets the optimal gas price for the transaction
|
||||
func (e *Executor) getOptimalGasPrice(ctx context.Context) (*big.Int, *big.Int, error) {
|
||||
// Update gas prices if we haven't recently
|
||||
if time.Since(e.gasTracker.lastUpdate) > 30*time.Second {
|
||||
gasPrice, err := e.client.SuggestGasPrice(ctx)
|
||||
if err != nil {
|
||||
return e.gasTracker.baseGasPrice, e.gasTracker.priorityFee, nil
|
||||
}
|
||||
|
||||
// Get priority fee from backend or use default
|
||||
priorityFee, err := e.client.SuggestGasTipCap(ctx)
|
||||
if err != nil {
|
||||
priorityFee = big.NewInt(1000000000) // 1 gwei default
|
||||
}
|
||||
|
||||
e.gasTracker.baseGasPrice = gasPrice
|
||||
e.gasTracker.priorityFee = priorityFee
|
||||
e.gasTracker.lastUpdate = time.Now()
|
||||
}
|
||||
|
||||
return e.gasTracker.baseGasPrice, e.gasTracker.priorityFee, nil
|
||||
}
|
||||
1106
pkg/arbitrum/parser/transaction_analyzer.go
Normal file
1106
pkg/arbitrum/parser/transaction_analyzer.go
Normal file
File diff suppressed because it is too large
Load Diff
145
pkg/arbitrum/parser/types.go
Normal file
145
pkg/arbitrum/parser/types.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/fraktal/mev-beta/pkg/types"
|
||||
)
|
||||
|
||||
// SwapParams represents parsed swap parameters
|
||||
type SwapParams struct {
|
||||
TokenIn common.Address
|
||||
TokenOut common.Address
|
||||
AmountIn *big.Int
|
||||
AmountOut *big.Int
|
||||
MinAmountOut *big.Int
|
||||
Recipient common.Address
|
||||
Fee *big.Int
|
||||
Pool common.Address
|
||||
}
|
||||
|
||||
// MEV opportunity structures
|
||||
type MEVOpportunities struct {
|
||||
BlockNumber uint64
|
||||
Timestamp time.Time
|
||||
SwapEvents []*SwapEvent
|
||||
LiquidationEvents []*LiquidationEvent
|
||||
LiquidityEvents []*LiquidityEvent
|
||||
ArbitrageOps []*types.ArbitrageOpportunity
|
||||
SandwichOps []*SandwichOpportunity
|
||||
LiquidationOps []*LiquidationOpportunity
|
||||
TotalProfit *big.Int
|
||||
ProcessingTime time.Duration
|
||||
}
|
||||
|
||||
// ParserOpportunity represents parser-specific arbitrage data (extends canonical ArbitrageOpportunity)
|
||||
type ParserOpportunity struct {
|
||||
*types.ArbitrageOpportunity
|
||||
ProfitMargin float64
|
||||
Exchanges []string
|
||||
}
|
||||
|
||||
type SandwichOpportunity struct {
|
||||
TargetTx string
|
||||
TokenIn common.Address
|
||||
TokenOut common.Address
|
||||
FrontrunAmount *big.Int
|
||||
BackrunAmount *big.Int
|
||||
ExpectedProfit *big.Int
|
||||
MaxSlippage float64
|
||||
GasCost *big.Int
|
||||
ProfitMargin float64
|
||||
}
|
||||
|
||||
type LiquidationOpportunity struct {
|
||||
Protocol string
|
||||
Borrower common.Address
|
||||
CollateralToken common.Address
|
||||
DebtToken common.Address
|
||||
MaxLiquidation *big.Int
|
||||
ExpectedProfit *big.Int
|
||||
HealthFactor float64
|
||||
GasCost *big.Int
|
||||
ProfitMargin float64
|
||||
}
|
||||
|
||||
// RawL2Transaction represents a raw L2 transaction
|
||||
type RawL2Transaction struct {
|
||||
Hash string `json:"hash"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Input string `json:"input"`
|
||||
Gas string `json:"gas"`
|
||||
GasPrice string `json:"gasPrice"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// RawL2Block represents a raw L2 block
|
||||
type RawL2Block struct {
|
||||
Number string `json:"number"`
|
||||
Transactions []RawL2Transaction `json:"transactions"`
|
||||
}
|
||||
|
||||
// SwapEvent represents a swap event
|
||||
type SwapEvent struct {
|
||||
Timestamp time.Time
|
||||
BlockNumber uint64
|
||||
TxHash string
|
||||
Protocol string
|
||||
Router common.Address
|
||||
Pool common.Address
|
||||
TokenIn common.Address
|
||||
TokenOut common.Address
|
||||
AmountIn *big.Int
|
||||
AmountOut *big.Int
|
||||
Sender common.Address
|
||||
Recipient common.Address
|
||||
GasPrice string
|
||||
GasUsed uint64
|
||||
PriceImpact float64
|
||||
MEVScore float64
|
||||
Profitable bool
|
||||
// Additional fields for protocol-specific data
|
||||
Fee uint64 // Uniswap V3 fee
|
||||
Deadline uint64 // Transaction deadline
|
||||
CurveI uint64 // Curve exchange input token index
|
||||
CurveJ uint64 // Curve exchange output token index
|
||||
}
|
||||
|
||||
// LiquidationEvent represents a liquidation event
|
||||
type LiquidationEvent struct {
|
||||
Timestamp time.Time
|
||||
BlockNumber uint64
|
||||
TxHash string
|
||||
Protocol string
|
||||
Liquidator common.Address
|
||||
Borrower common.Address
|
||||
CollateralToken common.Address
|
||||
DebtToken common.Address
|
||||
CollateralAmount *big.Int
|
||||
DebtAmount *big.Int
|
||||
Bonus *big.Int
|
||||
HealthFactor float64
|
||||
MEVOpportunity bool
|
||||
EstimatedProfit *big.Int
|
||||
}
|
||||
|
||||
// LiquidityEvent represents a liquidity event
|
||||
type LiquidityEvent struct {
|
||||
Timestamp time.Time
|
||||
BlockNumber uint64
|
||||
TxHash string
|
||||
Protocol string
|
||||
Pool common.Address
|
||||
EventType string
|
||||
Token0 common.Address
|
||||
Token1 common.Address
|
||||
Amount0 *big.Int
|
||||
Amount1 *big.Int
|
||||
Liquidity *big.Int
|
||||
PriceAfter *big.Float
|
||||
ImpactSize float64
|
||||
ArbitrageOpp bool
|
||||
}
|
||||
Reference in New Issue
Block a user