feat: create v2-prep branch with comprehensive planning
Restructured project for V2 refactor: **Structure Changes:** - Moved all V1 code to orig/ folder (preserved with git mv) - Created docs/planning/ directory - Added orig/README_V1.md explaining V1 preservation **Planning Documents:** - 00_V2_MASTER_PLAN.md: Complete architecture overview - Executive summary of critical V1 issues - High-level component architecture diagrams - 5-phase implementation roadmap - Success metrics and risk mitigation - 07_TASK_BREAKDOWN.md: Atomic task breakdown - 99+ hours of detailed tasks - Every task < 2 hours (atomic) - Clear dependencies and success criteria - Organized by implementation phase **V2 Key Improvements:** - Per-exchange parsers (factory pattern) - Multi-layer strict validation - Multi-index pool cache - Background validation pipeline - Comprehensive observability **Critical Issues Addressed:** - Zero address tokens (strict validation + cache enrichment) - Parsing accuracy (protocol-specific parsers) - No audit trail (background validation channel) - Inefficient lookups (multi-index cache) - Stats disconnection (event-driven metrics) Next Steps: 1. Review planning documents 2. Begin Phase 1: Foundation (P1-001 through P1-010) 3. Implement parsers in Phase 2 4. Build cache system in Phase 3 5. Add validation pipeline in Phase 4 6. Migrate and test in Phase 5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
326
orig/pkg/execution/flashloan_providers.go
Normal file
326
orig/pkg/execution/flashloan_providers.go
Normal file
@@ -0,0 +1,326 @@
|
||||
package execution
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/ethclient"
|
||||
|
||||
"github.com/fraktal/mev-beta/internal/logger"
|
||||
"github.com/fraktal/mev-beta/pkg/types"
|
||||
)
|
||||
|
||||
// AaveFlashLoanProvider implements flash loans using Aave Protocol
|
||||
type AaveFlashLoanProvider struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
|
||||
// Aave V3 Pool contract on Arbitrum
|
||||
poolAddress common.Address
|
||||
fee *big.Int // 0.09% fee = 9 basis points
|
||||
}
|
||||
|
||||
// NewAaveFlashLoanProvider creates a new Aave flash loan provider
|
||||
func NewAaveFlashLoanProvider(client *ethclient.Client, logger *logger.Logger) *AaveFlashLoanProvider {
|
||||
return &AaveFlashLoanProvider{
|
||||
client: client,
|
||||
logger: logger,
|
||||
// Aave V3 Pool on Arbitrum
|
||||
poolAddress: common.HexToAddress("0x794a61358D6845594F94dc1DB02A252b5b4814aD"),
|
||||
fee: big.NewInt(9), // 0.09% = 9 basis points
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteFlashLoan executes arbitrage using Aave flash loan
|
||||
func (a *AaveFlashLoanProvider) ExecuteFlashLoan(
|
||||
ctx context.Context,
|
||||
opportunity *types.ArbitrageOpportunity,
|
||||
config *ExecutionConfig,
|
||||
) (*ExecutionResult, error) {
|
||||
a.logger.Info(fmt.Sprintf("⚡ Executing Aave flash loan for %s ETH", opportunity.AmountIn.String()))
|
||||
|
||||
// TODO: Implement actual Aave flash loan execution
|
||||
// Steps:
|
||||
// 1. Build flashLoan() calldata with:
|
||||
// - Assets to borrow
|
||||
// - Amounts
|
||||
// - Modes (0 for no debt)
|
||||
// - OnBehalfOf address
|
||||
// - Params (encoded arbitrage path)
|
||||
// - ReferralCode
|
||||
// 2. Send transaction to Aave Pool
|
||||
// 3. Wait for receipt
|
||||
// 4. Parse events and calculate actual profit
|
||||
|
||||
return &ExecutionResult{
|
||||
OpportunityID: opportunity.ID,
|
||||
Success: false,
|
||||
Error: fmt.Errorf("Aave flash loan execution not yet implemented"),
|
||||
EstimatedProfit: opportunity.NetProfit,
|
||||
}, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
// GetMaxLoanAmount returns maximum borrowable amount from Aave
|
||||
func (a *AaveFlashLoanProvider) GetMaxLoanAmount(ctx context.Context, token common.Address) (*big.Int, error) {
|
||||
// TODO: Query Aave reserves to get available liquidity
|
||||
// For now, return a large amount
|
||||
return new(big.Int).Mul(big.NewInt(1000), big.NewInt(1e18)), nil // 1000 ETH
|
||||
}
|
||||
|
||||
// GetFee calculates Aave flash loan fee
|
||||
func (a *AaveFlashLoanProvider) GetFee(ctx context.Context, amount *big.Int) (*big.Int, error) {
|
||||
// Aave V3 fee is 0.09% (9 basis points)
|
||||
fee := new(big.Int).Mul(amount, a.fee)
|
||||
fee = fee.Div(fee, big.NewInt(10000))
|
||||
return fee, nil
|
||||
}
|
||||
|
||||
// SupportsToken checks if Aave supports the token
|
||||
func (a *AaveFlashLoanProvider) SupportsToken(token common.Address) bool {
|
||||
// TODO: Query Aave reserves to check token support
|
||||
// For now, support common tokens
|
||||
supportedTokens := map[common.Address]bool{
|
||||
common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"): true, // WETH
|
||||
common.HexToAddress("0xaf88d065e77c8cC2239327C5EDb3A432268e5831"): true, // USDC
|
||||
common.HexToAddress("0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9"): true, // USDT
|
||||
common.HexToAddress("0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f"): true, // WBTC
|
||||
common.HexToAddress("0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1"): true, // DAI
|
||||
}
|
||||
return supportedTokens[token]
|
||||
}
|
||||
|
||||
// UniswapFlashLoanProvider implements flash swaps using Uniswap V2/V3
|
||||
type UniswapFlashLoanProvider struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
}
|
||||
|
||||
// NewUniswapFlashLoanProvider creates a new Uniswap flash swap provider
|
||||
func NewUniswapFlashLoanProvider(client *ethclient.Client, logger *logger.Logger) *UniswapFlashLoanProvider {
|
||||
return &UniswapFlashLoanProvider{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteFlashLoan executes arbitrage using Uniswap flash swap
|
||||
func (u *UniswapFlashLoanProvider) ExecuteFlashLoan(
|
||||
ctx context.Context,
|
||||
opportunity *types.ArbitrageOpportunity,
|
||||
config *ExecutionConfig,
|
||||
) (*ExecutionResult, error) {
|
||||
u.logger.Info(fmt.Sprintf("⚡ Executing Uniswap flash swap for %s ETH", opportunity.AmountIn.String()))
|
||||
|
||||
// TODO: Implement Uniswap V2/V3 flash swap
|
||||
// V2 Flash Swap:
|
||||
// 1. Call swap() on pair with amount0Out/amount1Out
|
||||
// 2. Implement uniswapV2Call callback
|
||||
// 3. Execute arbitrage in callback
|
||||
// 4. Repay loan + fee (0.3%)
|
||||
//
|
||||
// V3 Flash:
|
||||
// 1. Call flash() on pool
|
||||
// 2. Implement uniswapV3FlashCallback
|
||||
// 3. Execute arbitrage
|
||||
// 4. Repay exact amount
|
||||
|
||||
return &ExecutionResult{
|
||||
OpportunityID: opportunity.ID,
|
||||
Success: false,
|
||||
Error: fmt.Errorf("Uniswap flash swap execution not yet implemented"),
|
||||
EstimatedProfit: opportunity.NetProfit,
|
||||
}, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
// GetMaxLoanAmount returns maximum borrowable from Uniswap pools
|
||||
func (u *UniswapFlashLoanProvider) GetMaxLoanAmount(ctx context.Context, token common.Address) (*big.Int, error) {
|
||||
// TODO: Find pool with most liquidity for the token
|
||||
return new(big.Int).Mul(big.NewInt(100), big.NewInt(1e18)), nil // 100 ETH
|
||||
}
|
||||
|
||||
// GetFee calculates Uniswap flash swap fee
|
||||
func (u *UniswapFlashLoanProvider) GetFee(ctx context.Context, amount *big.Int) (*big.Int, error) {
|
||||
// V2 flash swap fee is same as trading fee (0.3%)
|
||||
// V3 fee depends on pool tier (0.05%, 0.3%, 1%)
|
||||
// Use 0.3% as default
|
||||
fee := new(big.Int).Mul(amount, big.NewInt(3))
|
||||
fee = fee.Div(fee, big.NewInt(1000))
|
||||
return fee, nil
|
||||
}
|
||||
|
||||
// SupportsToken checks if Uniswap has pools for the token
|
||||
func (u *UniswapFlashLoanProvider) SupportsToken(token common.Address) bool {
|
||||
// Uniswap supports most tokens via pools
|
||||
return true
|
||||
}
|
||||
|
||||
// BalancerFlashLoanProvider implements flash loans using Balancer Vault
|
||||
type BalancerFlashLoanProvider struct {
|
||||
client *ethclient.Client
|
||||
logger *logger.Logger
|
||||
|
||||
// Balancer Vault on Arbitrum
|
||||
vaultAddress common.Address
|
||||
|
||||
// Flash loan receiver contract address (must be deployed first)
|
||||
receiverAddress common.Address
|
||||
}
|
||||
|
||||
// NewBalancerFlashLoanProvider creates a new Balancer flash loan provider
|
||||
func NewBalancerFlashLoanProvider(client *ethclient.Client, logger *logger.Logger) *BalancerFlashLoanProvider {
|
||||
return &BalancerFlashLoanProvider{
|
||||
client: client,
|
||||
logger: logger,
|
||||
// Balancer Vault on Arbitrum
|
||||
vaultAddress: common.HexToAddress("0xBA12222222228d8Ba445958a75a0704d566BF2C8"),
|
||||
// Flash loan receiver contract (TODO: Set this after deployment)
|
||||
receiverAddress: common.Address{}, // Zero address means not deployed yet
|
||||
}
|
||||
}
|
||||
|
||||
// ExecuteFlashLoan executes arbitrage using Balancer flash loan
|
||||
func (b *BalancerFlashLoanProvider) ExecuteFlashLoan(
|
||||
ctx context.Context,
|
||||
opportunity *types.ArbitrageOpportunity,
|
||||
config *ExecutionConfig,
|
||||
) (*ExecutionResult, error) {
|
||||
startTime := time.Now()
|
||||
b.logger.Info(fmt.Sprintf("⚡ Executing Balancer flash loan for opportunity %s", opportunity.ID))
|
||||
|
||||
// Check if receiver contract is deployed
|
||||
if b.receiverAddress == (common.Address{}) {
|
||||
return &ExecutionResult{
|
||||
OpportunityID: opportunity.ID,
|
||||
Success: false,
|
||||
Error: fmt.Errorf("flash loan receiver contract not deployed"),
|
||||
EstimatedProfit: opportunity.NetProfit,
|
||||
ExecutionTime: time.Since(startTime),
|
||||
Timestamp: time.Now(),
|
||||
}, fmt.Errorf("receiver contract not deployed")
|
||||
}
|
||||
|
||||
// Step 1: Prepare flash loan parameters
|
||||
tokens := []common.Address{opportunity.TokenIn} // Borrow input token
|
||||
amounts := []*big.Int{opportunity.AmountIn}
|
||||
|
||||
// Step 2: Encode arbitrage path as userData
|
||||
userData, err := b.encodeArbitragePath(opportunity, config)
|
||||
if err != nil {
|
||||
b.logger.Error(fmt.Sprintf("Failed to encode arbitrage path: %v", err))
|
||||
return &ExecutionResult{
|
||||
OpportunityID: opportunity.ID,
|
||||
Success: false,
|
||||
Error: fmt.Errorf("failed to encode path: %w", err),
|
||||
EstimatedProfit: opportunity.NetProfit,
|
||||
ExecutionTime: time.Since(startTime),
|
||||
Timestamp: time.Now(),
|
||||
}, err
|
||||
}
|
||||
|
||||
// Step 3: Build flash loan transaction
|
||||
// This would require:
|
||||
// - ABI for FlashLoanReceiver.executeArbitrage()
|
||||
// - Transaction signing
|
||||
// - Gas estimation
|
||||
// - Transaction submission
|
||||
// - Receipt waiting
|
||||
|
||||
b.logger.Info(fmt.Sprintf("Flash loan parameters prepared: tokens=%d, amount=%s", len(tokens), amounts[0].String()))
|
||||
b.logger.Info(fmt.Sprintf("UserData size: %d bytes", len(userData)))
|
||||
|
||||
// For now, return a detailed "not fully implemented" result
|
||||
// In production, this would call the FlashLoanReceiver.executeArbitrage() function
|
||||
return &ExecutionResult{
|
||||
OpportunityID: opportunity.ID,
|
||||
Success: false,
|
||||
Error: fmt.Errorf("transaction signing and submission not yet implemented (calldata encoding complete)"),
|
||||
EstimatedProfit: opportunity.NetProfit,
|
||||
ExecutionTime: time.Since(startTime),
|
||||
Timestamp: time.Now(),
|
||||
}, fmt.Errorf("not fully implemented")
|
||||
}
|
||||
|
||||
// encodeArbitragePath encodes an arbitrage path for the FlashLoanReceiver contract
|
||||
func (b *BalancerFlashLoanProvider) encodeArbitragePath(
|
||||
opportunity *types.ArbitrageOpportunity,
|
||||
config *ExecutionConfig,
|
||||
) ([]byte, error) {
|
||||
// Prepare path data for Solidity struct
|
||||
// struct ArbitragePath {
|
||||
// address[] tokens;
|
||||
// address[] exchanges;
|
||||
// uint24[] fees;
|
||||
// bool[] isV3;
|
||||
// uint256 minProfit;
|
||||
// }
|
||||
|
||||
numHops := len(opportunity.Path) - 1
|
||||
|
||||
// Extract exchange addresses and determine protocol versions
|
||||
exchanges := make([]common.Address, numHops)
|
||||
poolAddresses := make([]common.Address, 0)
|
||||
for _, poolStr := range opportunity.Pools {
|
||||
poolAddresses = append(poolAddresses, common.HexToAddress(poolStr))
|
||||
}
|
||||
fees := make([]*big.Int, numHops)
|
||||
isV3 := make([]bool, numHops)
|
||||
|
||||
for i := 0; i < numHops; i++ {
|
||||
// Use pool address from opportunity
|
||||
if i < len(poolAddresses) {
|
||||
exchanges[i] = poolAddresses[i]
|
||||
} else {
|
||||
exchanges[i] = common.Address{}
|
||||
}
|
||||
|
||||
// Check if Uniswap V3 based on protocol
|
||||
if opportunity.Protocol == "uniswap_v3" {
|
||||
isV3[i] = true
|
||||
fees[i] = big.NewInt(3000) // 0.3% fee tier
|
||||
} else {
|
||||
isV3[i] = false
|
||||
fees[i] = big.NewInt(0) // V2 doesn't use fee parameter
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate minimum acceptable profit (with slippage)
|
||||
minProfit := new(big.Int).Set(opportunity.NetProfit)
|
||||
slippageMultiplier := big.NewInt(int64((1.0 - config.MaxSlippage) * 10000))
|
||||
minProfit.Mul(minProfit, slippageMultiplier)
|
||||
minProfit.Div(minProfit, big.NewInt(10000))
|
||||
|
||||
// Pack the struct using ABI encoding
|
||||
// This is a simplified version - production would use go-ethereum's abi package
|
||||
b.logger.Info(fmt.Sprintf("Encoded path: %d hops, minProfit=%s", numHops, minProfit.String()))
|
||||
|
||||
// Return empty bytes for now - full ABI encoding implementation needed
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
// GetMaxLoanAmount returns maximum borrowable from Balancer
|
||||
func (b *BalancerFlashLoanProvider) GetMaxLoanAmount(ctx context.Context, token common.Address) (*big.Int, error) {
|
||||
// TODO: Query Balancer Vault reserves
|
||||
return new(big.Int).Mul(big.NewInt(500), big.NewInt(1e18)), nil // 500 ETH
|
||||
}
|
||||
|
||||
// GetFee calculates Balancer flash loan fee
|
||||
func (b *BalancerFlashLoanProvider) GetFee(ctx context.Context, amount *big.Int) (*big.Int, error) {
|
||||
// Balancer flash loans are FREE (0% fee)!
|
||||
return big.NewInt(0), nil
|
||||
}
|
||||
|
||||
// SupportsToken checks if Balancer Vault has the token
|
||||
func (b *BalancerFlashLoanProvider) SupportsToken(token common.Address) bool {
|
||||
// Balancer supports many tokens
|
||||
supportedTokens := map[common.Address]bool{
|
||||
common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"): true, // WETH
|
||||
common.HexToAddress("0xaf88d065e77c8cC2239327C5EDb3A432268e5831"): true, // USDC
|
||||
common.HexToAddress("0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9"): true, // USDT
|
||||
common.HexToAddress("0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f"): true, // WBTC
|
||||
common.HexToAddress("0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1"): true, // DAI
|
||||
}
|
||||
return supportedTokens[token]
|
||||
}
|
||||
Reference in New Issue
Block a user