package events import ( "math/big" "testing" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/stretchr/testify/assert" ) func TestEventTypeString(t *testing.T) { assert.Equal(t, "Unknown", Unknown.String()) assert.Equal(t, "Swap", Swap.String()) assert.Equal(t, "AddLiquidity", AddLiquidity.String()) assert.Equal(t, "RemoveLiquidity", RemoveLiquidity.String()) assert.Equal(t, "NewPool", NewPool.String()) assert.Equal(t, "Unknown", EventType(999).String()) // Test unknown value } func TestNewEventParser(t *testing.T) { parser := NewEventParser() assert.NotNil(t, parser) assert.NotNil(t, parser.knownPools) assert.NotEmpty(t, parser.knownPools) } func TestIsDEXInteraction(t *testing.T) { parser := NewEventParser() // Test with Uniswap V2 factory address tx1 := types.NewTransaction(0, parser.UniswapV2Factory, big.NewInt(0), 0, big.NewInt(0), nil) assert.True(t, parser.IsDEXInteraction(tx1)) // Test with Uniswap V3 factory address tx2 := types.NewTransaction(0, parser.UniswapV3Factory, big.NewInt(0), 0, big.NewInt(0), nil) assert.True(t, parser.IsDEXInteraction(tx2)) // Test with SushiSwap factory address tx3 := types.NewTransaction(0, parser.SushiSwapFactory, big.NewInt(0), 0, big.NewInt(0), nil) assert.True(t, parser.IsDEXInteraction(tx3)) // Test with Uniswap V2 router address tx4 := types.NewTransaction(0, parser.UniswapV2Router02, big.NewInt(0), 0, big.NewInt(0), nil) assert.True(t, parser.IsDEXInteraction(tx4)) // Test with a known pool address poolAddr := common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640") parser.AddKnownPool(poolAddr, "UniswapV3") tx5 := types.NewTransaction(0, poolAddr, big.NewInt(0), 0, big.NewInt(0), nil) assert.True(t, parser.IsDEXInteraction(tx5)) // Test with a random address (should be false) randomAddr := common.HexToAddress("0x1234567890123456789012345678901234567890") tx6 := types.NewTransaction(0, randomAddr, big.NewInt(0), 0, big.NewInt(0), nil) assert.False(t, parser.IsDEXInteraction(tx6)) // Test with contract creation transaction (nil To address) tx7 := types.NewContractCreation(0, big.NewInt(0), 0, big.NewInt(0), nil) assert.False(t, parser.IsDEXInteraction(tx7)) } func TestIdentifyProtocol(t *testing.T) { parser := NewEventParser() // Test with Uniswap V2 factory address tx1 := types.NewTransaction(0, parser.UniswapV2Factory, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "UniswapV2", parser.identifyProtocol(tx1)) // Test with Uniswap V3 factory address tx2 := types.NewTransaction(0, parser.UniswapV3Factory, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "UniswapV3", parser.identifyProtocol(tx2)) // Test with SushiSwap factory address tx3 := types.NewTransaction(0, parser.SushiSwapFactory, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "SushiSwap", parser.identifyProtocol(tx3)) // Test with Uniswap V2 router address tx4 := types.NewTransaction(0, parser.UniswapV2Router02, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "UniswapV2", parser.identifyProtocol(tx4)) // Test with a known pool address poolAddr := common.HexToAddress("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640") parser.AddKnownPool(poolAddr, "UniswapV3") tx5 := types.NewTransaction(0, poolAddr, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "UniswapV3", parser.identifyProtocol(tx5)) // Test with a random address (should be Unknown) randomAddr := common.HexToAddress("0x1234567890123456789012345678901234567890") tx6 := types.NewTransaction(0, randomAddr, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "Unknown", parser.identifyProtocol(tx6)) // Test with contract creation transaction (nil To address) tx7 := types.NewContractCreation(0, big.NewInt(0), 0, big.NewInt(0), nil) assert.Equal(t, "Unknown", parser.identifyProtocol(tx7)) } func TestAddKnownPoolAndGetKnownPools(t *testing.T) { parser := NewEventParser() initialCount := len(parser.GetKnownPools()) // Add a new pool addr := common.HexToAddress("0x1234567890123456789012345678901234567890") parser.AddKnownPool(addr, "TestProtocol") // Check that the pool was added pools := parser.GetKnownPools() assert.Equal(t, initialCount+1, len(pools)) assert.Equal(t, "TestProtocol", pools[addr]) // Add another pool addr2 := common.HexToAddress("0xabcdefabcdefabcdefabcdefabcdefabcdefabcd") parser.AddKnownPool(addr2, "AnotherProtocol") // Check that both pools are in the map pools = parser.GetKnownPools() assert.Equal(t, initialCount+2, len(pools)) assert.Equal(t, "TestProtocol", pools[addr]) assert.Equal(t, "AnotherProtocol", pools[addr2]) } func TestParseTransaction(t *testing.T) { parser := NewEventParser() // Create a transaction that interacts with a DEX tx := types.NewTransaction(0, parser.UniswapV3Factory, big.NewInt(0), 0, big.NewInt(0), nil) blockNumber := uint64(12345) timestamp := uint64(1620000000) // Parse the transaction events, err := parser.ParseTransaction(tx, blockNumber, timestamp) assert.NoError(t, err) assert.Len(t, events, 1) // Check the parsed event event := events[0] assert.Equal(t, Swap, event.Type) assert.Equal(t, "UniswapV3", event.Protocol) assert.Equal(t, parser.UniswapV3Factory, event.PoolAddress) assert.Equal(t, blockNumber, event.BlockNumber) assert.Equal(t, timestamp, event.Timestamp) assert.Equal(t, tx.Hash(), event.TransactionHash) assert.NotNil(t, event.Amount0) assert.NotNil(t, event.Amount1) assert.NotNil(t, event.SqrtPriceX96) assert.NotNil(t, event.Liquidity) } func TestParseTransactionNonDEX(t *testing.T) { parser := NewEventParser() // Create a transaction that doesn't interact with a DEX randomAddr := common.HexToAddress("0x1234567890123456789012345678901234567890") tx := types.NewTransaction(0, randomAddr, big.NewInt(0), 0, big.NewInt(0), nil) blockNumber := uint64(12345) timestamp := uint64(1620000000) // Parse the transaction events, err := parser.ParseTransaction(tx, blockNumber, timestamp) assert.NoError(t, err) assert.Len(t, events, 0) }