- Add complete Market Manager package with in-memory storage and CRUD operations - Implement arbitrage detection with profit calculations and thresholds - Add database adapter with PostgreSQL schema for persistence - Create comprehensive logging system with specialized log files - Add detailed documentation and implementation plans - Include example application and comprehensive test suite - Update Makefile with market manager build targets - Add check-implementations command for verification
135 lines
4.0 KiB
Go
135 lines
4.0 KiB
Go
package test
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
"github.com/fraktal/mev-beta/internal/logger"
|
|
"github.com/fraktal/mev-beta/pkg/profitcalc"
|
|
)
|
|
|
|
func TestSimpleProfitCalculator(t *testing.T) {
|
|
// Create a test logger
|
|
log := logger.New("debug", "text", "")
|
|
|
|
// Create profit calculator
|
|
calc := profitcalc.NewSimpleProfitCalculator(log)
|
|
|
|
// Test tokens (WETH and USDC on Arbitrum)
|
|
wethAddr := common.HexToAddress("0x82af49447d8a07e3bd95bd0d56f35241523fbab1")
|
|
usdcAddr := common.HexToAddress("0xaf88d065e77c8cc2239327c5edb3a432268e5831")
|
|
|
|
// Test case: 1 ETH -> 2000 USDC swap
|
|
amountIn := big.NewFloat(1.0) // 1 ETH
|
|
amountOut := big.NewFloat(2000.0) // 2000 USDC
|
|
|
|
// Analyze the opportunity
|
|
opportunity := calc.AnalyzeSwapOpportunity(
|
|
context.Background(),
|
|
wethAddr,
|
|
usdcAddr,
|
|
amountIn,
|
|
amountOut,
|
|
"UniswapV3",
|
|
)
|
|
|
|
// Verify opportunity was created
|
|
if opportunity == nil {
|
|
t.Fatal("Expected opportunity to be created, got nil")
|
|
}
|
|
|
|
// Verify basic fields
|
|
if opportunity.TokenA != wethAddr {
|
|
t.Errorf("Expected TokenA to be WETH, got %s", opportunity.TokenA.Hex())
|
|
}
|
|
|
|
if opportunity.TokenB != usdcAddr {
|
|
t.Errorf("Expected TokenB to be USDC, got %s", opportunity.TokenB.Hex())
|
|
}
|
|
|
|
// Verify amounts
|
|
if opportunity.AmountIn.Cmp(amountIn) != 0 {
|
|
t.Errorf("Expected AmountIn to be %s, got %s", amountIn.String(), opportunity.AmountIn.String())
|
|
}
|
|
|
|
if opportunity.AmountOut.Cmp(amountOut) != 0 {
|
|
t.Errorf("Expected AmountOut to be %s, got %s", amountOut.String(), opportunity.AmountOut.String())
|
|
}
|
|
|
|
// Verify profit calculations exist
|
|
if opportunity.EstimatedProfit == nil {
|
|
t.Error("Expected EstimatedProfit to be calculated")
|
|
}
|
|
|
|
if opportunity.GasCost == nil {
|
|
t.Error("Expected GasCost to be calculated")
|
|
}
|
|
|
|
if opportunity.NetProfit == nil {
|
|
t.Error("Expected NetProfit to be calculated")
|
|
}
|
|
|
|
// Verify profit margin is calculated
|
|
if opportunity.ProfitMargin == 0 {
|
|
t.Error("Expected ProfitMargin to be calculated")
|
|
}
|
|
|
|
// Verify confidence score
|
|
if opportunity.Confidence < 0 || opportunity.Confidence > 1 {
|
|
t.Errorf("Expected Confidence to be between 0 and 1, got %f", opportunity.Confidence)
|
|
}
|
|
|
|
// Log results for manual verification
|
|
t.Logf("Opportunity Analysis:")
|
|
t.Logf(" ID: %s", opportunity.ID)
|
|
t.Logf(" AmountIn: %s ETH", opportunity.AmountIn.String())
|
|
t.Logf(" AmountOut: %s tokens", opportunity.AmountOut.String())
|
|
t.Logf(" EstimatedProfit: %s ETH", calc.FormatEther(opportunity.EstimatedProfit))
|
|
t.Logf(" GasCost: %s ETH", calc.FormatEther(opportunity.GasCost))
|
|
t.Logf(" NetProfit: %s ETH", calc.FormatEther(opportunity.NetProfit))
|
|
t.Logf(" ProfitMargin: %.4f%%", opportunity.ProfitMargin*100)
|
|
t.Logf(" IsExecutable: %t", opportunity.IsExecutable)
|
|
t.Logf(" RejectReason: %s", opportunity.RejectReason)
|
|
t.Logf(" Confidence: %.2f", opportunity.Confidence)
|
|
}
|
|
|
|
func TestSimpleProfitCalculatorSmallTrade(t *testing.T) {
|
|
// Create a test logger
|
|
log := logger.New("debug", "text", "")
|
|
|
|
// Create profit calculator
|
|
calc := profitcalc.NewSimpleProfitCalculator(log)
|
|
|
|
// Test tokens
|
|
tokenA := common.HexToAddress("0x1111111111111111111111111111111111111111")
|
|
tokenB := common.HexToAddress("0x2222222222222222222222222222222222222222")
|
|
|
|
// Test case: Small trade that should be unprofitable after gas
|
|
amountIn := big.NewFloat(0.01) // 0.01 ETH
|
|
amountOut := big.NewFloat(20.0) // 20 tokens
|
|
|
|
// Analyze the opportunity
|
|
opportunity := calc.AnalyzeSwapOpportunity(
|
|
context.Background(),
|
|
tokenA,
|
|
tokenB,
|
|
amountIn,
|
|
amountOut,
|
|
"UniswapV2",
|
|
)
|
|
|
|
// Verify opportunity was created
|
|
if opportunity == nil {
|
|
t.Fatal("Expected opportunity to be created, got nil")
|
|
}
|
|
|
|
// Small trades should likely be unprofitable due to gas costs
|
|
t.Logf("Small Trade Analysis:")
|
|
t.Logf(" NetProfit: %s ETH", calc.FormatEther(opportunity.NetProfit))
|
|
t.Logf(" IsExecutable: %t", opportunity.IsExecutable)
|
|
t.Logf(" RejectReason: %s", opportunity.RejectReason)
|
|
t.Logf(" Confidence: %.2f", opportunity.Confidence)
|
|
}
|