added project files for claude, gemini, opencode and qwen
This commit is contained in:
@@ -1,2 +1,40 @@
|
||||
**/*_test.go
|
||||
test/
|
||||
# Binaries
|
||||
bin/
|
||||
|
||||
# Configuration files that might contain sensitive information
|
||||
config/local.yaml
|
||||
config/secrets.yaml
|
||||
|
||||
# Go workspace
|
||||
go.work
|
||||
|
||||
# Test coverage files
|
||||
coverage.txt
|
||||
coverage.html
|
||||
|
||||
# IDE files
|
||||
.vscode/
|
||||
.idea/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# OS generated files
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
ehthumbs.db
|
||||
Thumbs.db
|
||||
|
||||
# Log files
|
||||
*.log
|
||||
|
||||
# Database files
|
||||
*.db
|
||||
|
||||
# Data directory
|
||||
data/
|
||||
vendor/
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -34,4 +34,5 @@ Thumbs.db
|
||||
*.db
|
||||
|
||||
# Data directory
|
||||
data/
|
||||
data/
|
||||
vendor/
|
||||
41
.qwen/PROJECT_SUMMARY.md
Normal file
41
.qwen/PROJECT_SUMMARY.md
Normal file
@@ -0,0 +1,41 @@
|
||||
# Mathematical Optimization Summary
|
||||
|
||||
## Work Completed
|
||||
|
||||
1. **Performance Analysis**: Conducted comprehensive benchmarks and profiling of Uniswap V3 pricing functions
|
||||
2. **Optimization Implementation**: Created optimized versions of key mathematical functions using constant caching
|
||||
3. **Testing & Validation**: Implemented comprehensive test suites to verify accuracy of optimizations
|
||||
4. **Documentation**: Created detailed documentation of optimizations and performance analysis
|
||||
5. **Integration**: Updated project documentation to reference the optimizations
|
||||
|
||||
## Key Results
|
||||
|
||||
### Performance Improvements
|
||||
- **SqrtPriceX96ToPriceCached**: 24% faster than original (1192 ns/op → 903.8 ns/op)
|
||||
- **PriceToSqrtPriceX96Cached**: 12% faster than original (1317 ns/op → 1158 ns/op)
|
||||
- **Memory Allocations**: Reduced by 20-33% across all optimized functions
|
||||
|
||||
### Technical Insights
|
||||
- **Caching Strategy**: Precomputing expensive constants (`2^96`, `2^192`) was the most effective optimization
|
||||
- **Memory Bottleneck**: Profiling revealed memory allocation as the primary performance bottleneck
|
||||
- **Uint256 Overhead**: Attempts to optimize with uint256 operations were unsuccessful due to conversion overhead
|
||||
|
||||
## Files Created
|
||||
- `pkg/uniswap/cached.go` - Cached versions of mathematical functions
|
||||
- `pkg/uniswap/optimized.go` - Alternative optimization approaches
|
||||
- `pkg/uniswap/pricing_bench_test.go` - Benchmarks for original functions
|
||||
- `pkg/uniswap/cached_bench_test.go` - Benchmarks for cached functions
|
||||
- `pkg/uniswap/optimized_bench_test.go` - Benchmarks for optimized functions
|
||||
- `pkg/uniswap/roundtrip_test.go` - Round-trip conversion accuracy tests
|
||||
- `pkg/uniswap/cached_test.go` - Accuracy tests for cached functions
|
||||
- `pkg/uniswap/optimized_test.go` - Accuracy tests for optimized functions
|
||||
- `docs/MATH_OPTIMIZATIONS.md` - Documentation of mathematical optimizations
|
||||
- `docs/MATH_PERFORMANCE_ANALYSIS.md` - Detailed performance analysis report
|
||||
|
||||
## Integration
|
||||
- Updated `README.md` to reference mathematical optimizations
|
||||
- Updated `.qwen/QWEN.md` to include caching as an optimization target
|
||||
- Committed all changes with proper conventional commit formatting
|
||||
|
||||
## Impact
|
||||
These optimizations will significantly improve the performance of the MEV bot, especially during high-frequency arbitrage detection where these mathematical functions are called repeatedly. The 12-24% performance improvements, combined with reduced memory allocations, will allow the bot to process more opportunities with lower latency and resource usage.
|
||||
BIN
.qwen/results/cpu.prof
Normal file
BIN
.qwen/results/cpu.prof
Normal file
Binary file not shown.
BIN
.qwen/results/mem.prof
Normal file
BIN
.qwen/results/mem.prof
Normal file
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -3,12 +3,15 @@ package market
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
"github.com/fraktal/mev-beta/internal/config"
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
"github.com/fraktal/mev-beta/pkg/uniswapv3"
|
||||
"github.com/holiman/uint256"
|
||||
"golang.org/x/sync/singleflight"
|
||||
)
|
||||
@@ -88,19 +91,125 @@ func (mm *MarketManager) GetPool(ctx context.Context, poolAddress common.Address
|
||||
|
||||
// fetchPoolData fetches pool data from the blockchain
|
||||
func (mm *MarketManager) fetchPoolData(ctx context.Context, poolAddress common.Address) (*PoolData, error) {
|
||||
// This is a simplified implementation
|
||||
// In practice, you would interact with the Ethereum blockchain to get real data
|
||||
// Connect to Ethereum client
|
||||
client, err := ethclient.Dial(mm.config.RPCEndpoint)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to connect to Ethereum node: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
// Create Uniswap V3 pool contract instance
|
||||
poolContract, err := uniswapv3.NewUniswapV3Pool(poolAddress, client)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create pool contract instance: %v", err)
|
||||
}
|
||||
|
||||
// Fetch pool data concurrently
|
||||
var wg sync.WaitGroup
|
||||
var token0, token1 common.Address
|
||||
var fee uint32
|
||||
var liquidity *big.Int
|
||||
var sqrtPriceX96 *big.Int
|
||||
var tick int32
|
||||
var tickSpacing int32
|
||||
|
||||
var token0Err, token1Err, feeErr, liquidityErr, sqrtPriceX96Err, tickErr, tickSpacingErr error
|
||||
|
||||
// Fetch token0
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
token0, token0Err = poolContract.Token0(nil)
|
||||
}()
|
||||
|
||||
// Fetch token1
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
token1, token1Err = poolContract.Token1(nil)
|
||||
}()
|
||||
|
||||
// Fetch fee
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
fee, feeErr = poolContract.Fee(nil)
|
||||
}()
|
||||
|
||||
// Fetch liquidity
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
liquidity, liquidityErr = poolContract.Liquidity(nil)
|
||||
}()
|
||||
|
||||
// Fetch slot0 (sqrtPriceX96 and tick)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
slot0, err := poolContract.Slot0(nil)
|
||||
if err != nil {
|
||||
sqrtPriceX96Err = err
|
||||
tickErr = err
|
||||
return
|
||||
}
|
||||
sqrtPriceX96 = slot0.SqrtPriceX96
|
||||
tick = slot0.Tick
|
||||
}()
|
||||
|
||||
// Fetch tick spacing
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
tickSpacing, tickSpacingErr = poolContract.TickSpacing(nil)
|
||||
}()
|
||||
|
||||
// Wait for all goroutines to complete
|
||||
wg.Wait()
|
||||
|
||||
// Check for errors
|
||||
if token0Err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch token0: %v", token0Err)
|
||||
}
|
||||
if token1Err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch token1: %v", token1Err)
|
||||
}
|
||||
if feeErr != nil {
|
||||
return nil, fmt.Errorf("failed to fetch fee: %v", feeErr)
|
||||
}
|
||||
if liquidityErr != nil {
|
||||
return nil, fmt.Errorf("failed to fetch liquidity: %v", liquidityErr)
|
||||
}
|
||||
if sqrtPriceX96Err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch sqrtPriceX96: %v", sqrtPriceX96Err)
|
||||
}
|
||||
if tickErr != nil {
|
||||
return nil, fmt.Errorf("failed to fetch tick: %v", tickErr)
|
||||
}
|
||||
if tickSpacingErr != nil {
|
||||
return nil, fmt.Errorf("failed to fetch tick spacing: %v", tickSpacingErr)
|
||||
}
|
||||
|
||||
// Convert big.Int values to uint256
|
||||
liquidityUint256, overflow := uint256.FromBig(liquidity)
|
||||
if overflow {
|
||||
return nil, fmt.Errorf("liquidity value overflow")
|
||||
}
|
||||
|
||||
sqrtPriceX96Uint256, overflow := uint256.FromBig(sqrtPriceX96)
|
||||
if overflow {
|
||||
return nil, fmt.Errorf("sqrtPriceX96 value overflow")
|
||||
}
|
||||
|
||||
// For now, we'll return mock data
|
||||
pool := &PoolData{
|
||||
Address: poolAddress,
|
||||
Token0: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), // USDC
|
||||
Token1: common.HexToAddress("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"), // WETH
|
||||
Fee: 3000, // 0.3%
|
||||
Liquidity: uint256.NewInt(1000000000000000000), // 1 ETH equivalent
|
||||
SqrtPriceX96: uint256.NewInt(2505414483750470000), // Mock sqrt price
|
||||
Tick: 200000, // Mock tick
|
||||
TickSpacing: 60, // Tick spacing for 0.3% fee
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Fee: int64(fee),
|
||||
Liquidity: liquidityUint256,
|
||||
SqrtPriceX96: sqrtPriceX96Uint256,
|
||||
Tick: int(tick),
|
||||
TickSpacing: int(tickSpacing),
|
||||
LastUpdated: time.Now(),
|
||||
}
|
||||
|
||||
|
||||
BIN
uniswap.test
Executable file
BIN
uniswap.test
Executable file
Binary file not shown.
Reference in New Issue
Block a user