Merge branch 'feature/v2/parsers/P2-018-curve-stableswap'
This commit is contained in:
231
pkg/parsers/curve.go
Normal file
231
pkg/parsers/curve.go
Normal file
@@ -0,0 +1,231 @@
|
||||
package parsers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"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/crypto"
|
||||
|
||||
"github.com/your-org/mev-bot/pkg/cache"
|
||||
mevtypes "github.com/your-org/mev-bot/pkg/types"
|
||||
)
|
||||
|
||||
// Curve StableSwap TokenExchange event signature:
|
||||
// event TokenExchange(address indexed buyer, int128 sold_id, uint256 tokens_sold, int128 bought_id, uint256 tokens_bought)
|
||||
var (
|
||||
// CurveTokenExchangeSignature is the event signature for Curve TokenExchange events
|
||||
CurveTokenExchangeSignature = crypto.Keccak256Hash([]byte("TokenExchange(address,int128,uint256,int128,uint256)"))
|
||||
|
||||
// CurveTokenExchangeUnderlyingSignature is for pools with underlying tokens
|
||||
CurveTokenExchangeUnderlyingSignature = crypto.Keccak256Hash([]byte("TokenExchangeUnderlying(address,int128,uint256,int128,uint256)"))
|
||||
)
|
||||
|
||||
// CurveParser implements the Parser interface for Curve StableSwap pools
|
||||
type CurveParser struct {
|
||||
cache cache.PoolCache
|
||||
logger mevtypes.Logger
|
||||
}
|
||||
|
||||
// NewCurveParser creates a new Curve parser
|
||||
func NewCurveParser(cache cache.PoolCache, logger mevtypes.Logger) *CurveParser {
|
||||
return &CurveParser{
|
||||
cache: cache,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// Protocol returns the protocol type this parser handles
|
||||
func (p *CurveParser) Protocol() mevtypes.ProtocolType {
|
||||
return mevtypes.ProtocolCurve
|
||||
}
|
||||
|
||||
// SupportsLog checks if this parser can handle the given log
|
||||
func (p *CurveParser) SupportsLog(log types.Log) bool {
|
||||
// Check if log has the TokenExchange or TokenExchangeUnderlying event signature
|
||||
if len(log.Topics) == 0 {
|
||||
return false
|
||||
}
|
||||
return log.Topics[0] == CurveTokenExchangeSignature ||
|
||||
log.Topics[0] == CurveTokenExchangeUnderlyingSignature
|
||||
}
|
||||
|
||||
// ParseLog parses a Curve TokenExchange event from a log
|
||||
func (p *CurveParser) ParseLog(ctx context.Context, log types.Log, tx *types.Transaction) (*mevtypes.SwapEvent, error) {
|
||||
// Verify this is a TokenExchange event
|
||||
if !p.SupportsLog(log) {
|
||||
return nil, fmt.Errorf("unsupported log")
|
||||
}
|
||||
|
||||
// Get pool info from cache to extract token addresses and decimals
|
||||
poolInfo, err := p.cache.GetByAddress(ctx, log.Address)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pool not found in cache: %w", err)
|
||||
}
|
||||
|
||||
// Parse event data
|
||||
// Data contains: sold_id, tokens_sold, bought_id, tokens_bought (non-indexed)
|
||||
// Topics contain: [signature, buyer] (indexed)
|
||||
if len(log.Topics) != 2 {
|
||||
return nil, fmt.Errorf("invalid number of topics: expected 2, got %d", len(log.Topics))
|
||||
}
|
||||
|
||||
// Define ABI for data decoding
|
||||
int128Type, err := abi.NewType("int128", "", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create int128 type: %w", err)
|
||||
}
|
||||
|
||||
uint256Type, err := abi.NewType("uint256", "", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create uint256 type: %w", err)
|
||||
}
|
||||
|
||||
arguments := abi.Arguments{
|
||||
{Type: int128Type, Name: "sold_id"},
|
||||
{Type: uint256Type, Name: "tokens_sold"},
|
||||
{Type: int128Type, Name: "bought_id"},
|
||||
{Type: uint256Type, Name: "tokens_bought"},
|
||||
}
|
||||
|
||||
// Decode data
|
||||
values, err := arguments.Unpack(log.Data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode event data: %w", err)
|
||||
}
|
||||
|
||||
if len(values) != 4 {
|
||||
return nil, fmt.Errorf("invalid number of values: expected 4, got %d", len(values))
|
||||
}
|
||||
|
||||
// Extract buyer from topics
|
||||
buyer := common.BytesToAddress(log.Topics[1].Bytes())
|
||||
|
||||
// Extract coin indices and amounts
|
||||
soldID := values[0].(*big.Int)
|
||||
tokensSold := values[1].(*big.Int)
|
||||
boughtID := values[2].(*big.Int)
|
||||
tokensBought := values[3].(*big.Int)
|
||||
|
||||
// Convert coin indices to uint
|
||||
soldIndex := int(soldID.Int64())
|
||||
boughtIndex := int(boughtID.Int64())
|
||||
|
||||
// Determine which token is token0 and token1
|
||||
// Curve pools typically have 2-4 coins, we'll handle the common case of 2 coins
|
||||
var token0, token1 common.Address
|
||||
var token0Decimals, token1Decimals uint8
|
||||
var amount0In, amount1In, amount0Out, amount1Out *big.Int
|
||||
|
||||
// Map coin indices to tokens
|
||||
// For simplicity, we assume sold_id < bought_id means token0 → token1
|
||||
if soldIndex == 0 && boughtIndex == 1 {
|
||||
// Selling token0 for token1
|
||||
token0 = poolInfo.Token0
|
||||
token1 = poolInfo.Token1
|
||||
token0Decimals = poolInfo.Token0Decimals
|
||||
token1Decimals = poolInfo.Token1Decimals
|
||||
amount0In = tokensSold
|
||||
amount1In = big.NewInt(0)
|
||||
amount0Out = big.NewInt(0)
|
||||
amount1Out = tokensBought
|
||||
} else if soldIndex == 1 && boughtIndex == 0 {
|
||||
// Selling token1 for token0
|
||||
token0 = poolInfo.Token0
|
||||
token1 = poolInfo.Token1
|
||||
token0Decimals = poolInfo.Token0Decimals
|
||||
token1Decimals = poolInfo.Token1Decimals
|
||||
amount0In = big.NewInt(0)
|
||||
amount1In = tokensSold
|
||||
amount0Out = tokensBought
|
||||
amount1Out = big.NewInt(0)
|
||||
} else {
|
||||
// For multi-coin pools (3+ coins), we need more complex logic
|
||||
// For now, we'll use the pool's token0 and token1 as defaults
|
||||
token0 = poolInfo.Token0
|
||||
token1 = poolInfo.Token1
|
||||
token0Decimals = poolInfo.Token0Decimals
|
||||
token1Decimals = poolInfo.Token1Decimals
|
||||
|
||||
// Assume if sold_id is 0, we're selling token0
|
||||
if soldIndex == 0 {
|
||||
amount0In = tokensSold
|
||||
amount1In = big.NewInt(0)
|
||||
amount0Out = big.NewInt(0)
|
||||
amount1Out = tokensBought
|
||||
} else {
|
||||
amount0In = big.NewInt(0)
|
||||
amount1In = tokensSold
|
||||
amount0Out = tokensBought
|
||||
amount1Out = big.NewInt(0)
|
||||
}
|
||||
}
|
||||
|
||||
// Scale amounts to 18 decimals for internal representation
|
||||
amount0InScaled := mevtypes.ScaleToDecimals(amount0In, token0Decimals, 18)
|
||||
amount1InScaled := mevtypes.ScaleToDecimals(amount1In, token1Decimals, 18)
|
||||
amount0OutScaled := mevtypes.ScaleToDecimals(amount0Out, token0Decimals, 18)
|
||||
amount1OutScaled := mevtypes.ScaleToDecimals(amount1Out, token1Decimals, 18)
|
||||
|
||||
// Create swap event
|
||||
event := &mevtypes.SwapEvent{
|
||||
TxHash: tx.Hash(),
|
||||
BlockNumber: log.BlockNumber,
|
||||
LogIndex: uint(log.Index),
|
||||
PoolAddress: log.Address,
|
||||
Protocol: mevtypes.ProtocolCurve,
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Token0Decimals: token0Decimals,
|
||||
Token1Decimals: token1Decimals,
|
||||
Amount0In: amount0InScaled,
|
||||
Amount1In: amount1InScaled,
|
||||
Amount0Out: amount0OutScaled,
|
||||
Amount1Out: amount1OutScaled,
|
||||
Sender: buyer,
|
||||
Recipient: buyer, // In Curve, buyer is both sender and recipient
|
||||
Fee: big.NewInt(int64(poolInfo.Fee)), // Curve pools have variable fees
|
||||
}
|
||||
|
||||
// Validate the parsed event
|
||||
if err := event.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("validation failed: %w", err)
|
||||
}
|
||||
|
||||
p.logger.Debug("parsed Curve swap event",
|
||||
"txHash", event.TxHash.Hex(),
|
||||
"pool", event.PoolAddress.Hex(),
|
||||
"soldID", soldIndex,
|
||||
"boughtID", boughtIndex,
|
||||
"tokensSold", tokensSold.String(),
|
||||
"tokensBought", tokensBought.String(),
|
||||
)
|
||||
|
||||
return event, nil
|
||||
}
|
||||
|
||||
// ParseReceipt parses all Curve TokenExchange events from a transaction receipt
|
||||
func (p *CurveParser) ParseReceipt(ctx context.Context, receipt *types.Receipt, tx *types.Transaction) ([]*mevtypes.SwapEvent, error) {
|
||||
var events []*mevtypes.SwapEvent
|
||||
|
||||
for _, log := range receipt.Logs {
|
||||
if p.SupportsLog(*log) {
|
||||
event, err := p.ParseLog(ctx, *log, tx)
|
||||
if err != nil {
|
||||
// Log error but continue processing other logs
|
||||
p.logger.Warn("failed to parse log",
|
||||
"txHash", tx.Hash().Hex(),
|
||||
"logIndex", log.Index,
|
||||
"error", err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
events = append(events, event)
|
||||
}
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
446
pkg/parsers/curve_test.go
Normal file
446
pkg/parsers/curve_test.go
Normal file
@@ -0,0 +1,446 @@
|
||||
package parsers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
|
||||
"github.com/your-org/mev-bot/pkg/cache"
|
||||
mevtypes "github.com/your-org/mev-bot/pkg/types"
|
||||
)
|
||||
|
||||
func TestNewCurveParser(t *testing.T) {
|
||||
cache := cache.NewPoolCache()
|
||||
logger := &mockLogger{}
|
||||
|
||||
parser := NewCurveParser(cache, logger)
|
||||
|
||||
if parser == nil {
|
||||
t.Fatal("NewCurveParser returned nil")
|
||||
}
|
||||
|
||||
if parser.cache != cache {
|
||||
t.Error("NewCurveParser cache not set correctly")
|
||||
}
|
||||
|
||||
if parser.logger != logger {
|
||||
t.Error("NewCurveParser logger not set correctly")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveParser_Protocol(t *testing.T) {
|
||||
parser := NewCurveParser(cache.NewPoolCache(), &mockLogger{})
|
||||
|
||||
if parser.Protocol() != mevtypes.ProtocolCurve {
|
||||
t.Errorf("Protocol() = %v, want %v", parser.Protocol(), mevtypes.ProtocolCurve)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveParser_SupportsLog(t *testing.T) {
|
||||
parser := NewCurveParser(cache.NewPoolCache(), &mockLogger{})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
log types.Log
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "valid TokenExchange event",
|
||||
log: types.Log{
|
||||
Topics: []common.Hash{CurveTokenExchangeSignature},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "valid TokenExchangeUnderlying event",
|
||||
log: types.Log{
|
||||
Topics: []common.Hash{CurveTokenExchangeUnderlyingSignature},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "empty topics",
|
||||
log: types.Log{
|
||||
Topics: []common.Hash{},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "wrong event signature",
|
||||
log: types.Log{
|
||||
Topics: []common.Hash{common.HexToHash("0x1234")},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "UniswapV2 event signature",
|
||||
log: types.Log{
|
||||
Topics: []common.Hash{SwapEventSignature},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := parser.SupportsLog(tt.log); got != tt.want {
|
||||
t.Errorf("SupportsLog() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveParser_ParseLog(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create pool cache and add test pool
|
||||
poolCache := cache.NewPoolCache()
|
||||
poolAddress := common.HexToAddress("0x1111111111111111111111111111111111111111")
|
||||
token0 := common.HexToAddress("0x2222222222222222222222222222222222222222") // USDC
|
||||
token1 := common.HexToAddress("0x3333333333333333333333333333333333333333") // USDT
|
||||
|
||||
testPool := &mevtypes.PoolInfo{
|
||||
Address: poolAddress,
|
||||
Protocol: mevtypes.ProtocolCurve,
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Token0Decimals: 6, // USDC
|
||||
Token1Decimals: 6, // USDT
|
||||
Reserve0: big.NewInt(1000000000000), // 1M USDC
|
||||
Reserve1: big.NewInt(1000000000000), // 1M USDT
|
||||
Fee: 4, // 0.04% typical Curve fee
|
||||
IsActive: true,
|
||||
AmpCoefficient: big.NewInt(2000), // Typical A parameter for stablecoin pools
|
||||
}
|
||||
|
||||
err := poolCache.Add(ctx, testPool)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add test pool: %v", err)
|
||||
}
|
||||
|
||||
parser := NewCurveParser(poolCache, &mockLogger{})
|
||||
|
||||
// Create test transaction
|
||||
tx := types.NewTransaction(
|
||||
0,
|
||||
poolAddress,
|
||||
big.NewInt(0),
|
||||
0,
|
||||
big.NewInt(0),
|
||||
[]byte{},
|
||||
)
|
||||
|
||||
buyer := common.HexToAddress("0x4444444444444444444444444444444444444444")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
soldID int128
|
||||
tokensSold *big.Int
|
||||
boughtID int128
|
||||
tokensBought *big.Int
|
||||
wantAmount0In *big.Int
|
||||
wantAmount1In *big.Int
|
||||
wantAmount0Out *big.Int
|
||||
wantAmount1Out *big.Int
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "swap token0 for token1 (USDC → USDT)",
|
||||
soldID: 0,
|
||||
tokensSold: big.NewInt(1000000), // 1 USDC (6 decimals)
|
||||
boughtID: 1,
|
||||
tokensBought: big.NewInt(999500), // 0.9995 USDT (6 decimals)
|
||||
wantAmount0In: mevtypes.ScaleToDecimals(big.NewInt(1000000), 6, 18),
|
||||
wantAmount1In: big.NewInt(0),
|
||||
wantAmount0Out: big.NewInt(0),
|
||||
wantAmount1Out: mevtypes.ScaleToDecimals(big.NewInt(999500), 6, 18),
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "swap token1 for token0 (USDT → USDC)",
|
||||
soldID: 1,
|
||||
tokensSold: big.NewInt(1000000), // 1 USDT (6 decimals)
|
||||
boughtID: 0,
|
||||
tokensBought: big.NewInt(999500), // 0.9995 USDC (6 decimals)
|
||||
wantAmount0In: big.NewInt(0),
|
||||
wantAmount1In: mevtypes.ScaleToDecimals(big.NewInt(1000000), 6, 18),
|
||||
wantAmount0Out: mevtypes.ScaleToDecimals(big.NewInt(999500), 6, 18),
|
||||
wantAmount1Out: big.NewInt(0),
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Encode event data: sold_id, tokens_sold, bought_id, tokens_bought
|
||||
data := make([]byte, 32*4) // 4 * 32 bytes
|
||||
|
||||
// int128 sold_id
|
||||
soldIDBig := big.NewInt(int64(tt.soldID))
|
||||
soldIDBig.FillBytes(data[0:32])
|
||||
|
||||
// uint256 tokens_sold
|
||||
tt.tokensSold.FillBytes(data[32:64])
|
||||
|
||||
// int128 bought_id
|
||||
boughtIDBig := big.NewInt(int64(tt.boughtID))
|
||||
boughtIDBig.FillBytes(data[64:96])
|
||||
|
||||
// uint256 tokens_bought
|
||||
tt.tokensBought.FillBytes(data[96:128])
|
||||
|
||||
log := types.Log{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
CurveTokenExchangeSignature,
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: data,
|
||||
BlockNumber: 1000,
|
||||
Index: 0,
|
||||
}
|
||||
|
||||
event, err := parser.ParseLog(ctx, log, tx)
|
||||
|
||||
if tt.wantErr {
|
||||
if err == nil {
|
||||
t.Error("ParseLog() expected error, got nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLog() unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if event == nil {
|
||||
t.Fatal("ParseLog() returned nil event")
|
||||
}
|
||||
|
||||
// Verify event fields
|
||||
if event.TxHash != tx.Hash() {
|
||||
t.Errorf("TxHash = %v, want %v", event.TxHash, tx.Hash())
|
||||
}
|
||||
|
||||
if event.Protocol != mevtypes.ProtocolCurve {
|
||||
t.Errorf("Protocol = %v, want %v", event.Protocol, mevtypes.ProtocolCurve)
|
||||
}
|
||||
|
||||
if event.Amount0In.Cmp(tt.wantAmount0In) != 0 {
|
||||
t.Errorf("Amount0In = %v, want %v", event.Amount0In, tt.wantAmount0In)
|
||||
}
|
||||
|
||||
if event.Amount1In.Cmp(tt.wantAmount1In) != 0 {
|
||||
t.Errorf("Amount1In = %v, want %v", event.Amount1In, tt.wantAmount1In)
|
||||
}
|
||||
|
||||
if event.Amount0Out.Cmp(tt.wantAmount0Out) != 0 {
|
||||
t.Errorf("Amount0Out = %v, want %v", event.Amount0Out, tt.wantAmount0Out)
|
||||
}
|
||||
|
||||
if event.Amount1Out.Cmp(tt.wantAmount1Out) != 0 {
|
||||
t.Errorf("Amount1Out = %v, want %v", event.Amount1Out, tt.wantAmount1Out)
|
||||
}
|
||||
|
||||
if event.Sender != buyer {
|
||||
t.Errorf("Sender = %v, want %v", event.Sender, buyer)
|
||||
}
|
||||
|
||||
if event.Recipient != buyer {
|
||||
t.Errorf("Recipient = %v, want %v (Curve uses buyer for both)", event.Recipient, buyer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveParser_ParseReceipt(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create pool cache and add test pool
|
||||
poolCache := cache.NewPoolCache()
|
||||
poolAddress := common.HexToAddress("0x1111111111111111111111111111111111111111")
|
||||
token0 := common.HexToAddress("0x2222222222222222222222222222222222222222")
|
||||
token1 := common.HexToAddress("0x3333333333333333333333333333333333333333")
|
||||
|
||||
testPool := &mevtypes.PoolInfo{
|
||||
Address: poolAddress,
|
||||
Protocol: mevtypes.ProtocolCurve,
|
||||
Token0: token0,
|
||||
Token1: token1,
|
||||
Token0Decimals: 6,
|
||||
Token1Decimals: 6,
|
||||
Reserve0: big.NewInt(1000000000000),
|
||||
Reserve1: big.NewInt(1000000000000),
|
||||
Fee: 4,
|
||||
IsActive: true,
|
||||
AmpCoefficient: big.NewInt(2000),
|
||||
}
|
||||
|
||||
err := poolCache.Add(ctx, testPool)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to add test pool: %v", err)
|
||||
}
|
||||
|
||||
parser := NewCurveParser(poolCache, &mockLogger{})
|
||||
|
||||
// Create test transaction
|
||||
tx := types.NewTransaction(
|
||||
0,
|
||||
poolAddress,
|
||||
big.NewInt(0),
|
||||
0,
|
||||
big.NewInt(0),
|
||||
[]byte{},
|
||||
)
|
||||
|
||||
// Encode minimal valid event data
|
||||
soldID := big.NewInt(0)
|
||||
tokensSold := big.NewInt(1000000)
|
||||
boughtID := big.NewInt(1)
|
||||
tokensBought := big.NewInt(999500)
|
||||
|
||||
data := make([]byte, 32*4)
|
||||
soldID.FillBytes(data[0:32])
|
||||
tokensSold.FillBytes(data[32:64])
|
||||
boughtID.FillBytes(data[64:96])
|
||||
tokensBought.FillBytes(data[96:128])
|
||||
|
||||
buyer := common.HexToAddress("0x4444444444444444444444444444444444444444")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
receipt *types.Receipt
|
||||
wantCount int
|
||||
}{
|
||||
{
|
||||
name: "receipt with single Curve swap event",
|
||||
receipt: &types.Receipt{
|
||||
Logs: []*types.Log{
|
||||
{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
CurveTokenExchangeSignature,
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: data,
|
||||
BlockNumber: 1000,
|
||||
Index: 0,
|
||||
},
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "receipt with multiple Curve swap events",
|
||||
receipt: &types.Receipt{
|
||||
Logs: []*types.Log{
|
||||
{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
CurveTokenExchangeSignature,
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: data,
|
||||
BlockNumber: 1000,
|
||||
Index: 0,
|
||||
},
|
||||
{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
CurveTokenExchangeUnderlyingSignature,
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: data,
|
||||
BlockNumber: 1000,
|
||||
Index: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
wantCount: 2,
|
||||
},
|
||||
{
|
||||
name: "receipt with mixed events",
|
||||
receipt: &types.Receipt{
|
||||
Logs: []*types.Log{
|
||||
{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
CurveTokenExchangeSignature,
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: data,
|
||||
BlockNumber: 1000,
|
||||
Index: 0,
|
||||
},
|
||||
{
|
||||
Address: poolAddress,
|
||||
Topics: []common.Hash{
|
||||
SwapEventSignature, // UniswapV2 signature
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
common.BytesToHash(buyer.Bytes()),
|
||||
},
|
||||
Data: []byte{},
|
||||
BlockNumber: 1000,
|
||||
Index: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
wantCount: 1, // Only the Curve event
|
||||
},
|
||||
{
|
||||
name: "empty receipt",
|
||||
receipt: &types.Receipt{
|
||||
Logs: []*types.Log{},
|
||||
},
|
||||
wantCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
events, err := parser.ParseReceipt(ctx, tt.receipt, tx)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("ParseReceipt() unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if len(events) != tt.wantCount {
|
||||
t.Errorf("ParseReceipt() returned %d events, want %d", len(events), tt.wantCount)
|
||||
}
|
||||
|
||||
// Verify all returned events are valid
|
||||
for i, event := range events {
|
||||
if event == nil {
|
||||
t.Errorf("Event %d is nil", i)
|
||||
continue
|
||||
}
|
||||
|
||||
if event.Protocol != mevtypes.ProtocolCurve {
|
||||
t.Errorf("Event %d Protocol = %v, want %v", i, event.Protocol, mevtypes.ProtocolCurve)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveTokenExchangeSignature(t *testing.T) {
|
||||
// Verify the event signature is correct
|
||||
expected := crypto.Keccak256Hash([]byte("TokenExchange(address,int128,uint256,int128,uint256)"))
|
||||
|
||||
if CurveTokenExchangeSignature != expected {
|
||||
t.Errorf("CurveTokenExchangeSignature = %v, want %v", CurveTokenExchangeSignature, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurveTokenExchangeUnderlyingSignature(t *testing.T) {
|
||||
// Verify the underlying event signature is correct
|
||||
expected := crypto.Keccak256Hash([]byte("TokenExchangeUnderlying(address,int128,uint256,int128,uint256)"))
|
||||
|
||||
if CurveTokenExchangeUnderlyingSignature != expected {
|
||||
t.Errorf("CurveTokenExchangeUnderlyingSignature = %v, want %v", CurveTokenExchangeUnderlyingSignature, expected)
|
||||
}
|
||||
}
|
||||
334
pkg/parsers/example_usage.go
Normal file
334
pkg/parsers/example_usage.go
Normal file
@@ -0,0 +1,334 @@
|
||||
package parsers
|
||||
|
||||
// This file demonstrates how to use the parser factory with multiple protocol parsers,
|
||||
// swap logging, and Arbiscan validation for MEV bot operations.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"math/big"
|
||||
"os"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
"github.com/your-org/mev-bot/pkg/cache"
|
||||
"github.com/your-org/mev-bot/pkg/observability"
|
||||
mevtypes "github.com/your-org/mev-bot/pkg/types"
|
||||
"github.com/your-org/mev-bot/pkg/validation"
|
||||
)
|
||||
|
||||
// ExampleSetup demonstrates complete parser setup with all supported protocols
|
||||
func ExampleSetup() {
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. Create logger
|
||||
logger := observability.NewLogger(slog.LevelInfo)
|
||||
|
||||
// 2. Create pool cache
|
||||
poolCache := cache.NewPoolCache()
|
||||
|
||||
// 3. Populate cache with known pools (would come from pool discovery in production)
|
||||
populatePoolCache(ctx, poolCache)
|
||||
|
||||
// 4. Create parser factory
|
||||
factory := NewFactory()
|
||||
|
||||
// 5. Register all protocol parsers
|
||||
uniswapV2Parser := NewUniswapV2Parser(poolCache, logger)
|
||||
uniswapV3Parser := NewUniswapV3Parser(poolCache, logger)
|
||||
curveParser := NewCurveParser(poolCache, logger)
|
||||
|
||||
factory.RegisterParser(mevtypes.ProtocolUniswapV2, uniswapV2Parser)
|
||||
factory.RegisterParser(mevtypes.ProtocolUniswapV3, uniswapV3Parser)
|
||||
factory.RegisterParser(mevtypes.ProtocolCurve, curveParser)
|
||||
|
||||
// 6. Create swap logger for testing and validation
|
||||
swapLogger, _ := NewSwapLogger("./logs/swaps", logger)
|
||||
|
||||
// 7. Create Arbiscan validator
|
||||
arbiscanAPIKey := os.Getenv("ARBISCAN_API_KEY")
|
||||
arbiscanValidator := NewArbiscanValidator(arbiscanAPIKey, logger, swapLogger)
|
||||
|
||||
// 8. Create validator with rules
|
||||
validationRules := validation.DefaultValidationRules()
|
||||
validator := validation.NewValidator(validationRules)
|
||||
|
||||
// Now ready to parse transactions
|
||||
fmt.Println("✅ Parser factory initialized with 3 protocols")
|
||||
fmt.Println("✅ Swap logging enabled")
|
||||
fmt.Println("✅ Arbiscan validation enabled")
|
||||
|
||||
// Example usage (see ExampleParseTransaction)
|
||||
_ = factory
|
||||
_ = validator
|
||||
_ = swapLogger
|
||||
_ = arbiscanValidator
|
||||
}
|
||||
|
||||
// ExampleParseTransaction shows how to parse a transaction with multiple swaps
|
||||
func ExampleParseTransaction(
|
||||
factory *factory,
|
||||
tx *types.Transaction,
|
||||
receipt *types.Receipt,
|
||||
validator validation.Validator,
|
||||
swapLogger *SwapLogger,
|
||||
) ([]*mevtypes.SwapEvent, error) {
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. Parse all swap events from the transaction
|
||||
events, err := factory.ParseTransaction(ctx, tx, receipt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse transaction: %w", err)
|
||||
}
|
||||
|
||||
// 2. Validate each event
|
||||
validEvents := validator.FilterValid(ctx, events)
|
||||
|
||||
// 3. Log valid swaps for testing/analysis
|
||||
if len(validEvents) > 0 {
|
||||
swapLogger.LogSwapBatch(ctx, validEvents, "multi-protocol")
|
||||
}
|
||||
|
||||
// 4. Return valid events for arbitrage detection
|
||||
return validEvents, nil
|
||||
}
|
||||
|
||||
// ExampleArbitrageDetection shows how to detect arbitrage opportunities
|
||||
func ExampleArbitrageDetection(events []*mevtypes.SwapEvent, poolCache cache.PoolCache) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Group events by token pairs
|
||||
type TokenPair struct {
|
||||
Token0, Token1 common.Address
|
||||
}
|
||||
|
||||
eventsByPair := make(map[TokenPair][]*mevtypes.SwapEvent)
|
||||
|
||||
for _, event := range events {
|
||||
pair := TokenPair{
|
||||
Token0: event.Token0,
|
||||
Token1: event.Token1,
|
||||
}
|
||||
eventsByPair[pair] = append(eventsByPair[pair], event)
|
||||
}
|
||||
|
||||
// For each token pair, compare prices across protocols
|
||||
for pair, pairEvents := range eventsByPair {
|
||||
if len(pairEvents) < 2 {
|
||||
continue // Need at least 2 events to compare
|
||||
}
|
||||
|
||||
// Compare V2 vs V3 prices
|
||||
for i, event1 := range pairEvents {
|
||||
for j, event2 := range pairEvents {
|
||||
if i >= j {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if protocols are different
|
||||
if event1.Protocol == event2.Protocol {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate implied prices
|
||||
price1 := calculateImpliedPrice(event1)
|
||||
price2 := calculateImpliedPrice(event2)
|
||||
|
||||
// Calculate price difference
|
||||
priceDiff := new(big.Float).Sub(price1, price2)
|
||||
priceDiff.Abs(priceDiff)
|
||||
|
||||
// If price difference > threshold, we have an arbitrage opportunity
|
||||
threshold := big.NewFloat(0.001) // 0.1%
|
||||
if priceDiff.Cmp(threshold) > 0 {
|
||||
fmt.Printf("🎯 Arbitrage opportunity found!\n")
|
||||
fmt.Printf(" Pair: %s/%s\n", pair.Token0.Hex()[:10], pair.Token1.Hex()[:10])
|
||||
fmt.Printf(" %s price: %s\n", event1.Protocol, price1.Text('f', 6))
|
||||
fmt.Printf(" %s price: %s\n", event2.Protocol, price2.Text('f', 6))
|
||||
fmt.Printf(" Difference: %s\n", priceDiff.Text('f', 6))
|
||||
|
||||
// Calculate potential profit
|
||||
profit := simulateArbitrage(ctx, event1, event2, poolCache)
|
||||
if profit.Sign() > 0 {
|
||||
fmt.Printf(" 💰 Estimated profit: %s ETH\n", profit.Text('f', 6))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ExampleMultiHopArbitrage shows how to detect multi-hop arbitrage (A→B→C→A)
|
||||
func ExampleMultiHopArbitrage(poolCache cache.PoolCache) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Example: WETH → USDC → DAI → WETH arbitrage on Uniswap V3
|
||||
|
||||
// Pool 1: WETH/USDC
|
||||
poolWETH_USDC, _ := poolCache.GetByAddress(ctx, common.HexToAddress("0x1111"))
|
||||
|
||||
// Pool 2: USDC/DAI
|
||||
poolUSDC_DAI, _ := poolCache.GetByAddress(ctx, common.HexToAddress("0x2222"))
|
||||
|
||||
// Pool 3: DAI/WETH
|
||||
poolDAI_WETH, _ := poolCache.GetByAddress(ctx, common.HexToAddress("0x3333"))
|
||||
|
||||
// Simulate route: 1 WETH → USDC → DAI → WETH
|
||||
startAmount := big.NewInt(1000000000000000000) // 1 WETH
|
||||
|
||||
// Step 1: WETH → USDC
|
||||
usdcAmount, priceAfter1, _ := CalculateSwapAmounts(
|
||||
poolWETH_USDC.SqrtPriceX96,
|
||||
poolWETH_USDC.Liquidity,
|
||||
startAmount,
|
||||
true, // WETH = token0
|
||||
3000, // 0.3% fee
|
||||
)
|
||||
|
||||
// Step 2: USDC → DAI
|
||||
daiAmount, priceAfter2, _ := CalculateSwapAmounts(
|
||||
poolUSDC_DAI.SqrtPriceX96,
|
||||
poolUSDC_DAI.Liquidity,
|
||||
usdcAmount,
|
||||
true, // USDC = token0
|
||||
500, // 0.05% fee (Curve-like)
|
||||
)
|
||||
|
||||
// Step 3: DAI → WETH
|
||||
finalWETH, priceAfter3, _ := CalculateSwapAmounts(
|
||||
poolDAI_WETH.SqrtPriceX96,
|
||||
poolDAI_WETH.Liquidity,
|
||||
daiAmount,
|
||||
false, // WETH = token1
|
||||
3000, // 0.3% fee
|
||||
)
|
||||
|
||||
// Calculate profit
|
||||
profit := new(big.Int).Sub(finalWETH, startAmount)
|
||||
|
||||
if profit.Sign() > 0 {
|
||||
fmt.Printf("🚀 Multi-hop arbitrage opportunity!\n")
|
||||
fmt.Printf(" Route: WETH → USDC → DAI → WETH\n")
|
||||
fmt.Printf(" Input: %s WETH\n", formatAmount(startAmount, 18))
|
||||
fmt.Printf(" Output: %s WETH\n", formatAmount(finalWETH, 18))
|
||||
fmt.Printf(" 💰 Profit: %s WETH\n", formatAmount(profit, 18))
|
||||
fmt.Printf(" Prices: %v → %v → %v\n", priceAfter1, priceAfter2, priceAfter3)
|
||||
} else {
|
||||
fmt.Printf("❌ No profit: %s WETH loss\n", formatAmount(new(big.Int).Abs(profit), 18))
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
func populatePoolCache(ctx context.Context, poolCache cache.PoolCache) {
|
||||
// Example pools (would come from discovery service in production)
|
||||
|
||||
// Uniswap V2: WETH/USDC
|
||||
poolCache.Add(ctx, &mevtypes.PoolInfo{
|
||||
Address: common.HexToAddress("0xC31E54c7a869B9FcBEcc14363CF510d1c41fa443"),
|
||||
Protocol: mevtypes.ProtocolUniswapV2,
|
||||
Token0: common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), // WETH
|
||||
Token1: common.HexToAddress("0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8"), // USDC
|
||||
Token0Decimals: 18,
|
||||
Token1Decimals: 6,
|
||||
Fee: 30, // 0.3%
|
||||
IsActive: true,
|
||||
})
|
||||
|
||||
// Uniswap V3: WETH/USDC 0.05%
|
||||
poolCache.Add(ctx, &mevtypes.PoolInfo{
|
||||
Address: common.HexToAddress("0xC31E54c7a869B9FcBEcc14363CF510d1c41fa444"),
|
||||
Protocol: mevtypes.ProtocolUniswapV3,
|
||||
Token0: common.HexToAddress("0x82aF49447D8a07e3bd95BD0d56f35241523fBab1"), // WETH
|
||||
Token1: common.HexToAddress("0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8"), // USDC
|
||||
Token0Decimals: 18,
|
||||
Token1Decimals: 6,
|
||||
Fee: 500, // 0.05%
|
||||
SqrtPriceX96: new(big.Int).Lsh(big.NewInt(1), 96),
|
||||
Liquidity: big.NewInt(1000000000000),
|
||||
IsActive: true,
|
||||
})
|
||||
|
||||
// Curve: USDC/USDT
|
||||
poolCache.Add(ctx, &mevtypes.PoolInfo{
|
||||
Address: common.HexToAddress("0x7f90122BF0700F9E7e1F688fe926940E8839F353"),
|
||||
Protocol: mevtypes.ProtocolCurve,
|
||||
Token0: common.HexToAddress("0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8"), // USDC
|
||||
Token1: common.HexToAddress("0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9"), // USDT
|
||||
Token0Decimals: 6,
|
||||
Token1Decimals: 6,
|
||||
Fee: 4, // 0.04%
|
||||
AmpCoefficient: big.NewInt(2000),
|
||||
IsActive: true,
|
||||
})
|
||||
}
|
||||
|
||||
func calculateImpliedPrice(event *mevtypes.SwapEvent) *big.Float {
|
||||
// Calculate price as amountOut / amountIn
|
||||
var amountIn, amountOut *big.Int
|
||||
|
||||
if event.Amount0In.Sign() > 0 {
|
||||
amountIn = event.Amount0In
|
||||
amountOut = event.Amount1Out
|
||||
} else {
|
||||
amountIn = event.Amount1In
|
||||
amountOut = event.Amount0Out
|
||||
}
|
||||
|
||||
if amountIn.Sign() == 0 {
|
||||
return big.NewFloat(0)
|
||||
}
|
||||
|
||||
amountInFloat := new(big.Float).SetInt(amountIn)
|
||||
amountOutFloat := new(big.Float).SetInt(amountOut)
|
||||
|
||||
price := new(big.Float).Quo(amountOutFloat, amountInFloat)
|
||||
return price
|
||||
}
|
||||
|
||||
func simulateArbitrage(
|
||||
ctx context.Context,
|
||||
event1, event2 *mevtypes.SwapEvent,
|
||||
poolCache cache.PoolCache,
|
||||
) *big.Float {
|
||||
// Simplified arbitrage simulation
|
||||
// In production, this would:
|
||||
// 1. Calculate optimal trade size
|
||||
// 2. Account for gas costs
|
||||
// 3. Account for slippage
|
||||
// 4. Check liquidity constraints
|
||||
|
||||
// For now, return mock profit
|
||||
return big.NewFloat(0.05) // 0.05 ETH profit
|
||||
}
|
||||
|
||||
func formatAmount(amount *big.Int, decimals uint8) string {
|
||||
// Convert to float and format
|
||||
amountFloat := new(big.Float).SetInt(amount)
|
||||
divisor := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimals)), nil))
|
||||
result := new(big.Float).Quo(amountFloat, divisor)
|
||||
return result.Text('f', 6)
|
||||
}
|
||||
|
||||
// ExampleRealTimeMonitoring shows how to monitor pending transactions
|
||||
func ExampleRealTimeMonitoring() {
|
||||
fmt.Println("📡 Real-time MEV bot monitoring pattern:")
|
||||
fmt.Println("")
|
||||
fmt.Println("1. Subscribe to pending transactions (mempool)")
|
||||
fmt.Println("2. Parse swaps using factory.ParseTransaction()")
|
||||
fmt.Println("3. Validate using validator.FilterValid()")
|
||||
fmt.Println("4. Detect arbitrage across protocols")
|
||||
fmt.Println("5. Calculate profitability (profit - gas)")
|
||||
fmt.Println("6. Execute if profitable (front-run, sandwich, or arbitrage)")
|
||||
fmt.Println("7. Log results with swapLogger for analysis")
|
||||
fmt.Println("8. Validate accuracy with arbiscanValidator")
|
||||
fmt.Println("")
|
||||
fmt.Println("Performance targets:")
|
||||
fmt.Println(" - Parse: < 5ms")
|
||||
fmt.Println(" - Validate: < 2ms")
|
||||
fmt.Println(" - Detect: < 10ms")
|
||||
fmt.Println(" - Execute: < 30ms")
|
||||
fmt.Println(" - Total: < 50ms end-to-end")
|
||||
}
|
||||
Reference in New Issue
Block a user