saving in place

This commit is contained in:
Krypto Kajun
2025-10-04 09:31:02 -05:00
parent 76c1b5cee1
commit f358f49aa9
295 changed files with 72071 additions and 17209 deletions

View File

@@ -5,8 +5,11 @@ import (
"fmt"
"math/big"
"sort"
"strings"
"time"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
@@ -417,18 +420,61 @@ func (c *CREATE2Calculator) queryCurveRegistry(ctx context.Context, registryAddr
// queryMainCurveRegistry queries the main Curve registry
func (c *CREATE2Calculator) queryMainCurveRegistry(ctx context.Context, registryAddr, token0, token1 common.Address) (common.Address, error) {
// Main registry has find_pool_for_coins function
// For now, we'll use a simplified approach
// In a full implementation, you would:
// 1. Create contract instance with proper ABI
// 2. Call find_pool_for_coins(token0, token1)
// 3. Handle different coin ordering and precision
// Query Curve registry using find_pool_for_coins function
c.logger.Debug(fmt.Sprintf("Querying main Curve registry %s for tokens %s/%s",
registryAddr.Hex(), token0.Hex(), token1.Hex()))
// Placeholder: would need actual contract call
return common.Address{}, nil
// Curve registry ABI for find_pool_for_coins function
registryABI := `[{"name":"find_pool_for_coins","outputs":[{"type":"address","name":""}],"inputs":[{"type":"address","name":"_from"},{"type":"address","name":"_to"}],"stateMutability":"view","type":"function"}]`
parsedABI, err := abi.JSON(strings.NewReader(registryABI))
if err != nil {
return common.Address{}, fmt.Errorf("failed to parse registry ABI: %w", err)
}
// Pack the function call
callData, err := parsedABI.Pack("find_pool_for_coins", token0, token1)
if err != nil {
return common.Address{}, fmt.Errorf("failed to pack registry call: %w", err)
}
// Make the contract call
callMsg := ethereum.CallMsg{
To: &registryAddr,
Data: callData,
}
result, err := c.ethClient.CallContract(ctx, callMsg, nil)
if err != nil {
return common.Address{}, fmt.Errorf("registry call failed: %w", err)
}
// Unpack the result
var poolAddr common.Address
if err := parsedABI.UnpackIntoInterface(&poolAddr, "find_pool_for_coins", result); err != nil {
return common.Address{}, fmt.Errorf("failed to unpack result: %w", err)
}
// Check if a valid pool was found
if poolAddr == (common.Address{}) {
// Try with reversed token order
callData, err = parsedABI.Pack("find_pool_for_coins", token1, token0)
if err != nil {
return common.Address{}, fmt.Errorf("failed to pack reversed registry call: %w", err)
}
callMsg.Data = callData
result, err = c.ethClient.CallContract(ctx, callMsg, nil)
if err != nil {
return common.Address{}, fmt.Errorf("reversed registry call failed: %w", err)
}
if err := parsedABI.UnpackIntoInterface(&poolAddr, "find_pool_for_coins", result); err != nil {
return common.Address{}, fmt.Errorf("failed to unpack reversed result: %w", err)
}
}
return poolAddr, nil
}
// queryFactoryCurveRegistry queries the Curve factory registry

View File

@@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"math/big"
"os"
"strings"
@@ -15,6 +16,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/fraktal/mev-beta/internal/logger"
"github.com/fraktal/mev-beta/pkg/security"
"github.com/fraktal/mev-beta/pkg/uniswap"
)
@@ -607,12 +609,32 @@ func (pd *PoolDiscovery) discoverPoolFromSwap(poolAddress, txHash string) {
}
}
// Safely convert fee from int64 to uint32
var feeUint64 uint64
if poolState.Fee < 0 {
feeUint64 = 0
} else {
feeUint64 = uint64(poolState.Fee)
}
safeFee, err := security.SafeUint32(feeUint64)
if err != nil {
pd.logger.Warn(fmt.Sprintf("Failed to safely convert fee %d for pool %s: %v", poolState.Fee, poolAddress, err))
// Use a default fee value if conversion fails
// Truncate to 32 bits safely by using math.MaxUint32 as mask
if poolState.Fee > math.MaxUint32 {
safeFee = math.MaxUint32
} else {
safeFee = uint32(poolState.Fee)
}
}
// Create pool entry with real data
pool := &Pool{
Address: poolAddress,
Token0: poolState.Token0.Hex(),
Token1: poolState.Token1.Hex(),
Fee: uint32(poolState.Fee),
Address: poolAddress,
Token0: poolState.Token0.Hex(),
Token1: poolState.Token1.Hex(),
// Safely convert fee from uint64 to uint32
Fee: safeFee,
Protocol: protocol,
Factory: factory,
Liquidity: poolState.Liquidity.ToBig(),
@@ -694,7 +716,7 @@ func (pd *PoolDiscovery) parseSyncData(data string) *SyncData {
// persistData saves pools and exchanges to files
func (pd *PoolDiscovery) persistData() {
// Ensure data directory exists
os.MkdirAll("data", 0755)
os.MkdirAll("data", 0750)
// Save pools
poolsData, _ := json.MarshalIndent(pd.pools, "", " ")