Files
mev-beta/pkg/dex/uniswap_v3.go

285 lines
8.6 KiB
Go

package dex
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
// UniswapV3Decoder implements DEXDecoder for Uniswap V3
type UniswapV3Decoder struct {
*BaseDecoder
poolABI abi.ABI
routerABI abi.ABI
}
// UniswapV3 Pool ABI (minimal)
const uniswapV3PoolABI = `[
{
"inputs": [],
"name": "slot0",
"outputs": [
{"internalType": "uint160", "name": "sqrtPriceX96", "type": "uint160"},
{"internalType": "int24", "name": "tick", "type": "int24"},
{"internalType": "uint16", "name": "observationIndex", "type": "uint16"},
{"internalType": "uint16", "name": "observationCardinality", "type": "uint16"},
{"internalType": "uint16", "name": "observationCardinalityNext", "type": "uint16"},
{"internalType": "uint8", "name": "feeProtocol", "type": "uint8"},
{"internalType": "bool", "name": "unlocked", "type": "bool"}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "liquidity",
"outputs": [{"internalType": "uint128", "name": "", "type": "uint128"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "token0",
"outputs": [{"internalType": "address", "name": "", "type": "address"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "token1",
"outputs": [{"internalType": "address", "name": "", "type": "address"}],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "fee",
"outputs": [{"internalType": "uint24", "name": "", "type": "uint24"}],
"stateMutability": "view",
"type": "function"
}
]`
// UniswapV3 Router ABI (minimal)
const uniswapV3RouterABI = `[
{
"inputs": [
{
"components": [
{"internalType": "address", "name": "tokenIn", "type": "address"},
{"internalType": "address", "name": "tokenOut", "type": "address"},
{"internalType": "uint24", "name": "fee", "type": "uint24"},
{"internalType": "address", "name": "recipient", "type": "address"},
{"internalType": "uint256", "name": "deadline", "type": "uint256"},
{"internalType": "uint256", "name": "amountIn", "type": "uint256"},
{"internalType": "uint256", "name": "amountOutMinimum", "type": "uint256"},
{"internalType": "uint160", "name": "sqrtPriceLimitX96", "type": "uint160"}
],
"internalType": "struct ISwapRouter.ExactInputSingleParams",
"name": "params",
"type": "tuple"
}
],
"name": "exactInputSingle",
"outputs": [{"internalType": "uint256", "name": "amountOut", "type": "uint256"}],
"stateMutability": "payable",
"type": "function"
}
]`
// NewUniswapV3Decoder creates a new UniswapV3 decoder
func NewUniswapV3Decoder(client *ethclient.Client) *UniswapV3Decoder {
poolABI, _ := abi.JSON(strings.NewReader(uniswapV3PoolABI))
routerABI, _ := abi.JSON(strings.NewReader(uniswapV3RouterABI))
return &UniswapV3Decoder{
BaseDecoder: NewBaseDecoder(ProtocolUniswapV3, client),
poolABI: poolABI,
routerABI: routerABI,
}
}
// DecodeSwap decodes a Uniswap V3 swap transaction
func (d *UniswapV3Decoder) DecodeSwap(tx *types.Transaction) (*SwapInfo, error) {
data := tx.Data()
if len(data) < 4 {
return nil, fmt.Errorf("transaction data too short")
}
method, err := d.routerABI.MethodById(data[:4])
if err != nil {
return nil, fmt.Errorf("failed to get method: %w", err)
}
if method.Name != "exactInputSingle" {
return nil, fmt.Errorf("unsupported method: %s", method.Name)
}
params := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(params, data[4:]); err != nil {
return nil, fmt.Errorf("failed to unpack params: %w", err)
}
paramsStruct := params["params"].(struct {
TokenIn common.Address
TokenOut common.Address
Fee *big.Int
Recipient common.Address
Deadline *big.Int
AmountIn *big.Int
AmountOutMinimum *big.Int
SqrtPriceLimitX96 *big.Int
})
return &SwapInfo{
Protocol: ProtocolUniswapV3,
TokenIn: paramsStruct.TokenIn,
TokenOut: paramsStruct.TokenOut,
AmountIn: paramsStruct.AmountIn,
AmountOut: paramsStruct.AmountOutMinimum,
Recipient: paramsStruct.Recipient,
Fee: paramsStruct.Fee,
Deadline: paramsStruct.Deadline,
}, nil
}
// GetPoolReserves fetches current pool reserves for Uniswap V3
func (d *UniswapV3Decoder) GetPoolReserves(ctx context.Context, client *ethclient.Client, poolAddress common.Address) (*PoolReserves, error) {
// Get slot0 (sqrtPriceX96, tick, etc.)
slot0Data, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["slot0"].ID,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to get slot0: %w", err)
}
var slot0 struct {
SqrtPriceX96 *big.Int
Tick int32
}
if err := d.poolABI.UnpackIntoInterface(&slot0, "slot0", slot0Data); err != nil {
return nil, fmt.Errorf("failed to unpack slot0: %w", err)
}
// Get liquidity
liquidityData, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["liquidity"].ID,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to get liquidity: %w", err)
}
liquidity := new(big.Int).SetBytes(liquidityData)
// Get token0
token0Data, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["token0"].ID,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to get token0: %w", err)
}
token0 := common.BytesToAddress(token0Data)
// Get token1
token1Data, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["token1"].ID,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to get token1: %w", err)
}
token1 := common.BytesToAddress(token1Data)
// Get fee
feeData, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["fee"].ID,
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to get fee: %w", err)
}
fee := new(big.Int).SetBytes(feeData)
return &PoolReserves{
Token0: token0,
Token1: token1,
Protocol: ProtocolUniswapV3,
PoolAddress: poolAddress,
SqrtPriceX96: slot0.SqrtPriceX96,
Tick: slot0.Tick,
Liquidity: liquidity,
Fee: fee,
}, nil
}
// CalculateOutput calculates expected output for Uniswap V3
func (d *UniswapV3Decoder) CalculateOutput(amountIn *big.Int, reserves *PoolReserves, tokenIn common.Address) (*big.Int, error) {
if reserves.SqrtPriceX96 == nil || reserves.Liquidity == nil {
return nil, fmt.Errorf("invalid reserves for UniswapV3")
}
// Simplified calculation - in production, would need tick math
// This is an approximation using sqrtPriceX96
sqrtPrice := new(big.Float).SetInt(reserves.SqrtPriceX96)
q96 := new(big.Float).SetInt(new(big.Int).Lsh(big.NewInt(1), 96))
price := new(big.Float).Quo(sqrtPrice, q96)
price.Mul(price, price) // Square to get actual price
amountInFloat := new(big.Float).SetInt(amountIn)
amountOutFloat := new(big.Float).Mul(amountInFloat, price)
// Apply fee (0.3% default)
feeFactor := new(big.Float).SetFloat64(0.997)
amountOutFloat.Mul(amountOutFloat, feeFactor)
amountOut, _ := amountOutFloat.Int(nil)
return amountOut, nil
}
// CalculatePriceImpact calculates price impact for Uniswap V3
func (d *UniswapV3Decoder) CalculatePriceImpact(amountIn *big.Int, reserves *PoolReserves, tokenIn common.Address) (float64, error) {
// For UniswapV3, price impact depends on liquidity depth at current tick
// This is a simplified calculation
if reserves.Liquidity.Sign() == 0 {
return 1.0, nil
}
amountInFloat := new(big.Float).SetInt(amountIn)
liquidityFloat := new(big.Float).SetInt(reserves.Liquidity)
impact := new(big.Float).Quo(amountInFloat, liquidityFloat)
impactValue, _ := impact.Float64()
return impactValue, nil
}
// GetQuote gets a price quote for Uniswap V3
func (d *UniswapV3Decoder) GetQuote(ctx context.Context, client *ethclient.Client, tokenIn, tokenOut common.Address, amountIn *big.Int) (*PriceQuote, error) {
// TODO: Implement actual pool lookup via factory
// For now, return error
return nil, fmt.Errorf("GetQuote not yet implemented for UniswapV3")
}
// IsValidPool checks if a pool is a valid Uniswap V3 pool
func (d *UniswapV3Decoder) IsValidPool(ctx context.Context, client *ethclient.Client, poolAddress common.Address) (bool, error) {
// Try to call slot0() - if it succeeds, it's a valid pool
_, err := client.CallContract(ctx, ethereum.CallMsg{
To: &poolAddress,
Data: d.poolABI.Methods["slot0"].ID,
}, nil)
return err == nil, nil
}