saving in place
This commit is contained in:
23
pkg/arbitrum/market/config.go
Normal file
23
pkg/arbitrum/market/config.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// LoadMarketConfig loads market configuration from YAML file
|
||||
func LoadMarketConfig(configPath string) (*MarketConfig, error) {
|
||||
data, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read config file: %w", err)
|
||||
}
|
||||
|
||||
var config MarketConfig
|
||||
if err := yaml.Unmarshal(data, &config); err != nil {
|
||||
return nil, fmt.Errorf("failed to parse config: %w", err)
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
74
pkg/arbitrum/market/logging.go
Normal file
74
pkg/arbitrum/market/logging.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// initializeLogging sets up JSONL logging files
|
||||
func (md *MarketDiscovery) initializeLogging() error {
|
||||
// Create logs directory if it doesn't exist
|
||||
if err := os.MkdirAll("logs", 0755); err != nil {
|
||||
return fmt.Errorf("failed to create logs directory: %w", err)
|
||||
}
|
||||
|
||||
// Open market scan log file
|
||||
marketScanFile, err := os.OpenFile(md.config.Logging.Files["market_scans"], os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open market scan log file: %w", err)
|
||||
}
|
||||
md.marketScanLogger = marketScanFile
|
||||
|
||||
// Open arbitrage log file
|
||||
arbFile, err := os.OpenFile(md.config.Logging.Files["arbitrage"], os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open arbitrage log file: %w", err)
|
||||
}
|
||||
md.arbLogger = arbFile
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Logging methods
|
||||
func (md *MarketDiscovery) logMarketScan(result *MarketScanResult) error {
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = md.marketScanLogger.Write(append(data, '\n'))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return md.marketScanLogger.Sync()
|
||||
}
|
||||
|
||||
func (md *MarketDiscovery) logArbitrageOpportunity(opp *ArbitrageOpportunityDetailed) error {
|
||||
data, err := json.Marshal(opp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = md.arbLogger.Write(append(data, '\n'))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return md.arbLogger.Sync()
|
||||
}
|
||||
|
||||
func (md *MarketDiscovery) logPoolDiscovery(result *PoolDiscoveryResult) error {
|
||||
data, err := json.Marshal(result)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = md.marketScanLogger.Write(append(data, '\n'))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return md.marketScanLogger.Sync()
|
||||
}
|
||||
180
pkg/arbitrum/market/market_discovery.go
Normal file
180
pkg/arbitrum/market/market_discovery.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
)
|
||||
|
||||
// NewMarketDiscovery creates a new market discovery instance
|
||||
func NewMarketDiscovery(client interface{}, loggerInstance *logger.Logger, configPath string) (*MarketDiscovery, error) {
|
||||
// Load configuration
|
||||
config, err := LoadMarketConfig(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
|
||||
// Initialize math calculator
|
||||
// mathCalc := exchangeMath.NewMathCalculator()
|
||||
|
||||
md := &MarketDiscovery{
|
||||
client: client,
|
||||
logger: loggerInstance,
|
||||
config: config,
|
||||
// mathCalc: mathCalc,
|
||||
pools: make(map[common.Address]*PoolInfoDetailed),
|
||||
tokens: make(map[common.Address]*TokenInfo),
|
||||
factories: make(map[common.Address]*FactoryInfo),
|
||||
routers: make(map[common.Address]*RouterInfo),
|
||||
}
|
||||
|
||||
// Initialize logging
|
||||
if err := md.initializeLogging(); err != nil {
|
||||
return nil, fmt.Errorf("failed to initialize logging: %w", err)
|
||||
}
|
||||
|
||||
// Load initial configuration
|
||||
if err := md.loadInitialMarkets(); err != nil {
|
||||
return nil, fmt.Errorf("failed to load initial markets: %w", err)
|
||||
}
|
||||
|
||||
loggerInstance.Info("Market discovery initialized with comprehensive pool detection")
|
||||
return md, nil
|
||||
}
|
||||
|
||||
// loadInitialMarkets loads initial tokens, factories, and priority pools
|
||||
func (md *MarketDiscovery) loadInitialMarkets() error {
|
||||
md.mu.Lock()
|
||||
defer md.mu.Unlock()
|
||||
|
||||
// Load tokens
|
||||
for _, token := range md.config.Tokens {
|
||||
tokenAddr := common.HexToAddress(token.Address)
|
||||
md.tokens[tokenAddr] = &TokenInfo{
|
||||
Address: tokenAddr,
|
||||
Symbol: token.Symbol,
|
||||
Decimals: uint8(token.Decimals),
|
||||
Priority: token.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load factories
|
||||
for _, factory := range md.config.Factories {
|
||||
factoryAddr := common.HexToAddress(factory.Address)
|
||||
md.factories[factoryAddr] = &FactoryInfo{
|
||||
Address: factoryAddr,
|
||||
Type: factory.Type,
|
||||
InitCodeHash: common.HexToHash(factory.InitCodeHash),
|
||||
FeeTiers: factory.FeeTiers,
|
||||
Priority: factory.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load routers
|
||||
for _, router := range md.config.Routers {
|
||||
routerAddr := common.HexToAddress(router.Address)
|
||||
factoryAddr := common.Address{}
|
||||
if router.Factory != "" {
|
||||
for _, f := range md.config.Factories {
|
||||
if f.Type == router.Factory {
|
||||
factoryAddr = common.HexToAddress(f.Address)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
md.routers[routerAddr] = &RouterInfo{
|
||||
Address: routerAddr,
|
||||
Factory: factoryAddr,
|
||||
Type: router.Type,
|
||||
Priority: router.Priority,
|
||||
}
|
||||
}
|
||||
|
||||
// Load priority pools
|
||||
for _, poolConfig := range md.config.PriorityPools {
|
||||
poolAddr := common.HexToAddress(poolConfig.Pool)
|
||||
token0 := common.HexToAddress(poolConfig.Token0)
|
||||
token1 := common.HexToAddress(poolConfig.Token1)
|
||||
|
||||
// Find factory
|
||||
var factoryAddr common.Address
|
||||
var factoryType string
|
||||
for _, f := range md.config.Factories {
|
||||
if f.Type == poolConfig.Factory {
|
||||
factoryAddr = common.HexToAddress(f.Address)
|
||||
factoryType = f.Type
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
pool := &PoolInfoDetailed{
|
||||
Address: poolAddr,
|
||||
Factory: factoryAddr,
|
||||
FactoryType: factoryType,
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Fee: poolConfig.Fee,
|
||||
Priority: poolConfig.Priority,
|
||||
Active: true,
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
md.pools[poolAddr] = pool
|
||||
}
|
||||
|
||||
loggerMsg := fmt.Sprintf("Loaded initial markets: %d tokens, %d factories, %d routers, %d priority pools",
|
||||
len(md.tokens), len(md.factories), len(md.routers), len(md.pools))
|
||||
md.logger.Info(loggerMsg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Helper methods
|
||||
func abs(x float64) float64 {
|
||||
if x < 0 {
|
||||
return -x
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// GetStatistics returns market discovery statistics
|
||||
func (md *MarketDiscovery) GetStatistics() map[string]interface{} {
|
||||
md.mu.RLock()
|
||||
defer md.mu.RUnlock()
|
||||
|
||||
return map[string]interface{}{
|
||||
"pools_tracked": len(md.pools),
|
||||
"tokens_tracked": len(md.tokens),
|
||||
"factories_tracked": len(md.factories),
|
||||
"pools_discovered": md.poolsDiscovered,
|
||||
"arbitrage_opportunities": md.arbitrageOpps,
|
||||
"last_scan_time": md.lastScanTime,
|
||||
"total_scan_time": md.totalScanTime.String(),
|
||||
}
|
||||
}
|
||||
|
||||
// Close closes all log files and resources
|
||||
func (md *MarketDiscovery) Close() error {
|
||||
var errors []error
|
||||
|
||||
// if md.marketScanLogger != nil {
|
||||
// if err := md.marketScanLogger.(*os.File).Close(); err != nil {
|
||||
// errors = append(errors, err)
|
||||
// }
|
||||
// }
|
||||
|
||||
// if md.arbLogger != nil {
|
||||
// if err := md.arbLogger.(*os.File).Close(); err != nil {
|
||||
// errors = append(errors, err)
|
||||
// }
|
||||
// }
|
||||
|
||||
if len(errors) > 0 {
|
||||
return fmt.Errorf("errors closing resources: %v", errors)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
220
pkg/arbitrum/market/types.go
Normal file
220
pkg/arbitrum/market/types.go
Normal file
@@ -0,0 +1,220 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
)
|
||||
|
||||
// MarketDiscovery manages pool discovery and market building
|
||||
type MarketDiscovery struct {
|
||||
client interface{} // ethclient.Client
|
||||
logger *logger.Logger
|
||||
config *MarketConfig
|
||||
mathCalc interface{} // exchangeMath.MathCalculator
|
||||
|
||||
// Market state
|
||||
pools map[common.Address]*PoolInfoDetailed
|
||||
tokens map[common.Address]*TokenInfo
|
||||
factories map[common.Address]*FactoryInfo
|
||||
routers map[common.Address]*RouterInfo
|
||||
mu sync.RWMutex
|
||||
|
||||
// Logging
|
||||
marketScanLogger *os.File
|
||||
arbLogger *os.File
|
||||
|
||||
// Performance tracking
|
||||
poolsDiscovered uint64
|
||||
arbitrageOpps uint64
|
||||
lastScanTime time.Time
|
||||
totalScanTime time.Duration
|
||||
}
|
||||
|
||||
// MarketConfig represents the configuration for market discovery
|
||||
type MarketConfig struct {
|
||||
Version string `yaml:"version"`
|
||||
Network string `yaml:"network"`
|
||||
ChainID int64 `yaml:"chain_id"`
|
||||
Tokens map[string]*TokenConfigInfo `yaml:"tokens"`
|
||||
Factories map[string]*FactoryConfig `yaml:"factories"`
|
||||
Routers map[string]*RouterConfig `yaml:"routers"`
|
||||
PriorityPools []PriorityPoolConfig `yaml:"priority_pools"`
|
||||
MarketScan MarketScanConfig `yaml:"market_scan"`
|
||||
Arbitrage ArbitrageConfig `yaml:"arbitrage"`
|
||||
Logging LoggingConfig `yaml:"logging"`
|
||||
Risk RiskConfig `yaml:"risk"`
|
||||
Monitoring MonitoringConfig `yaml:"monitoring"`
|
||||
}
|
||||
|
||||
type TokenConfigInfo struct {
|
||||
Address string `yaml:"address"`
|
||||
Symbol string `yaml:"symbol"`
|
||||
Decimals int `yaml:"decimals"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type FactoryConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Type string `yaml:"type"`
|
||||
InitCodeHash string `yaml:"init_code_hash"`
|
||||
FeeTiers []uint32 `yaml:"fee_tiers"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type RouterConfig struct {
|
||||
Address string `yaml:"address"`
|
||||
Factory string `yaml:"factory"`
|
||||
Type string `yaml:"type"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type PriorityPoolConfig struct {
|
||||
Pool string `yaml:"pool"`
|
||||
Factory string `yaml:"factory"`
|
||||
Token0 string `yaml:"token0"`
|
||||
Token1 string `yaml:"token1"`
|
||||
Fee uint32 `yaml:"fee"`
|
||||
Priority int `yaml:"priority"`
|
||||
}
|
||||
|
||||
type MarketScanConfig struct {
|
||||
ScanInterval int `yaml:"scan_interval"`
|
||||
MaxPools int `yaml:"max_pools"`
|
||||
MinLiquidityUSD float64 `yaml:"min_liquidity_usd"`
|
||||
MinVolume24hUSD float64 `yaml:"min_volume_24h_usd"`
|
||||
Discovery PoolDiscoveryConfig `yaml:"discovery"`
|
||||
}
|
||||
|
||||
type PoolDiscoveryConfig struct {
|
||||
MaxBlocksBack uint64 `yaml:"max_blocks_back"`
|
||||
MinPoolAge uint64 `yaml:"min_pool_age"`
|
||||
DiscoveryInterval uint64 `yaml:"discovery_interval"`
|
||||
}
|
||||
|
||||
type ArbitrageConfig struct {
|
||||
MinProfitUSD float64 `yaml:"min_profit_usd"`
|
||||
MaxSlippage float64 `yaml:"max_slippage"`
|
||||
MaxGasPrice float64 `yaml:"max_gas_price"`
|
||||
ProfitMargins map[string]float64 `yaml:"profit_margins"`
|
||||
}
|
||||
|
||||
type LoggingConfig struct {
|
||||
Level string `yaml:"level"`
|
||||
Files map[string]string `yaml:"files"`
|
||||
RealTime map[string]interface{} `yaml:"real_time"`
|
||||
}
|
||||
|
||||
type RiskConfig struct {
|
||||
MaxPositionETH float64 `yaml:"max_position_eth"`
|
||||
MaxDailyLossETH float64 `yaml:"max_daily_loss_eth"`
|
||||
MaxConcurrentTxs int `yaml:"max_concurrent_txs"`
|
||||
CircuitBreaker map[string]interface{} `yaml:"circuit_breaker"`
|
||||
}
|
||||
|
||||
type MonitoringConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
UpdateInterval int `yaml:"update_interval"`
|
||||
Metrics []string `yaml:"metrics"`
|
||||
}
|
||||
|
||||
// PoolInfoDetailed represents detailed pool information for market discovery
|
||||
type PoolInfoDetailed struct {
|
||||
Address common.Address `json:"address"`
|
||||
Factory common.Address `json:"factory"`
|
||||
FactoryType string `json:"factory_type"`
|
||||
Token0 common.Address `json:"token0"`
|
||||
Token1 common.Address `json:"token1"`
|
||||
Fee uint32 `json:"fee"`
|
||||
Reserve0 *big.Int `json:"reserve0"`
|
||||
Reserve1 *big.Int `json:"reserve1"`
|
||||
Liquidity *big.Int `json:"liquidity"`
|
||||
SqrtPriceX96 *big.Int `json:"sqrt_price_x96,omitempty"` // For V3 pools
|
||||
Tick int32 `json:"tick,omitempty"` // For V3 pools
|
||||
LastUpdated time.Time `json:"last_updated"`
|
||||
Volume24h *big.Int `json:"volume_24h"`
|
||||
Priority int `json:"priority"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type TokenInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Symbol string `json:"symbol"`
|
||||
Name string `json:"name"`
|
||||
Decimals uint8 `json:"decimals"`
|
||||
Priority int `json:"priority"`
|
||||
LastPrice *big.Int `json:"last_price"`
|
||||
Volume24h *big.Int `json:"volume_24h"`
|
||||
}
|
||||
|
||||
type FactoryInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Type string `json:"type"`
|
||||
InitCodeHash common.Hash `json:"init_code_hash"`
|
||||
FeeTiers []uint32 `json:"fee_tiers"`
|
||||
PoolCount uint64 `json:"pool_count"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
type RouterInfo struct {
|
||||
Address common.Address `json:"address"`
|
||||
Factory common.Address `json:"factory"`
|
||||
Type string `json:"type"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
|
||||
// MarketScanResult represents the result of a market scan
|
||||
type MarketScanResult struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
BlockNumber uint64 `json:"block_number"`
|
||||
PoolsScanned int `json:"pools_scanned"`
|
||||
NewPoolsFound int `json:"new_pools_found"`
|
||||
ArbitrageOpps []*ArbitrageOpportunityDetailed `json:"arbitrage_opportunities"`
|
||||
TopPools []*PoolInfoDetailed `json:"top_pools"`
|
||||
ScanDuration time.Duration `json:"scan_duration"`
|
||||
GasPrice *big.Int `json:"gas_price"`
|
||||
NetworkConditions map[string]interface{} `json:"network_conditions"`
|
||||
}
|
||||
|
||||
type ArbitrageOpportunityDetailed struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
TokenIn common.Address `json:"token_in"`
|
||||
TokenOut common.Address `json:"token_out"`
|
||||
AmountIn *big.Int `json:"amount_in"`
|
||||
ExpectedAmountOut *big.Int `json:"expected_amount_out"`
|
||||
ActualAmountOut *big.Int `json:"actual_amount_out"`
|
||||
Profit *big.Int `json:"profit"`
|
||||
ProfitUSD float64 `json:"profit_usd"`
|
||||
ProfitMargin float64 `json:"profit_margin"`
|
||||
GasCost *big.Int `json:"gas_cost"`
|
||||
NetProfit *big.Int `json:"net_profit"`
|
||||
ExchangeA string `json:"exchange_a"`
|
||||
ExchangeB string `json:"exchange_b"`
|
||||
PoolA common.Address `json:"pool_a"`
|
||||
PoolB common.Address `json:"pool_b"`
|
||||
PriceA float64 `json:"price_a"`
|
||||
PriceB float64 `json:"price_b"`
|
||||
PriceImpactA float64 `json:"price_impact_a"`
|
||||
PriceImpactB float64 `json:"price_impact_b"`
|
||||
CapitalRequired float64 `json:"capital_required"`
|
||||
GasCostUSD float64 `json:"gas_cost_usd"`
|
||||
Confidence float64 `json:"confidence"`
|
||||
RiskScore float64 `json:"risk_score"`
|
||||
ExecutionTime time.Duration `json:"execution_time"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// PoolDiscoveryResult represents pool discovery results
|
||||
type PoolDiscoveryResult struct {
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
FromBlock uint64 `json:"from_block"`
|
||||
ToBlock uint64 `json:"to_block"`
|
||||
NewPools []*PoolInfoDetailed `json:"new_pools"`
|
||||
PoolsFound int `json:"pools_found"`
|
||||
ScanDuration time.Duration `json:"scan_duration"`
|
||||
}
|
||||
Reference in New Issue
Block a user