From d841cfeea567d76ee1908ffcaf91cf5dcc939074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20G=C3=B3rzy=C5=84ski?= Date: Tue, 17 Dec 2024 14:58:56 +0100 Subject: [PATCH] ethcoder: fix event decode byte16 (#152) --- ethcoder/events.go | 10 +++++++-- ethcoder/events_test.go | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/ethcoder/events.go b/ethcoder/events.go index 8c63ac5..60033ff 100644 --- a/ethcoder/events.go +++ b/ethcoder/events.go @@ -270,8 +270,14 @@ func (d *EventDecoder) DecodeLogAsHex(log types.Log) (ABISignature, []string, bo return eventDef, nil, false, fmt.Errorf("indexed argument out of range: %d > %d", idx+1, len(log.Topics)-1) } data := log.Topics[idx+1].Bytes() - arg := data[32-byteSize:] - eventValues = append(eventValues, HexEncode(arg)) + + var argVal []byte + if arg.Type.T == abi.BytesTy || arg.Type.T == abi.FixedBytesTy { + argVal = data[:byteSize] + } else { + argVal = data[32-byteSize:] + } + eventValues = append(eventValues, HexEncode(argVal)) idx++ } else { byteSize := abi.GetTypeSize(arg.Type) diff --git a/ethcoder/events_test.go b/ethcoder/events_test.go index b25f130..21f77c4 100644 --- a/ethcoder/events_test.go +++ b/ethcoder/events_test.go @@ -433,6 +433,54 @@ func TestDecodeTransactionLogByEventSig6(t *testing.T) { require.Equal(t, "0x00000000000000000000000000000000000000000000000000000000003fab53", eventHexValues[3]) } +func TestDecodeTransactionLogByEventSig7(t *testing.T) { + logTopics := []string{ + "0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a", + "0x98442bdf09d14ed3859c77b17990a0fc00000000000000000000000000000000", + } + logData := "0x" + + txnLog := types.Log{} + txnLog.Topics = []common.Hash{} + + for _, topic := range logTopics { + txnLog.Topics = append(txnLog.Topics, common.HexToHash(topic)) + } + txnLog.Data, _ = hexutil.Decode(logData) + + var eventSig = "JobExecuted(bytes16 indexed)" + + eventDef, eventValues, ok, err := ethcoder.DecodeTransactionLogByEventSig(txnLog, eventSig) + require.NoError(t, err) + require.True(t, ok) + + require.Equal(t, "0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a", eventDef.Hash) + require.Equal(t, "JobExecuted", eventDef.Name) + require.Equal(t, "JobExecuted(bytes16)", eventDef.Signature) + require.Equal(t, []string{"bytes16"}, eventDef.ArgTypes) + require.Equal(t, []bool{true}, eventDef.ArgIndexed) + + src := eventValues[0].([16]uint8) + dst := make([]byte, 16) + copy(dst[:], src[:]) + + require.Equal(t, 1, len(eventValues)) + require.Equal(t, "0x98442bdf09d14ed3859c77b17990a0fc", ethcoder.HexEncode(dst)) + + eventDef, eventValuesHex, ok, err := ethcoder.DecodeTransactionLogByEventSigAsHex(txnLog, eventSig) + require.NoError(t, err) + require.True(t, ok) + + require.Equal(t, "0x7732a1c54f54c543d17e9603255b42e04ad87aa8024a0a3637f532f2e2b6d22a", eventDef.Hash) + require.Equal(t, "JobExecuted", eventDef.Name) + require.Equal(t, "JobExecuted(bytes16)", eventDef.Signature) + require.Equal(t, []string{"bytes16"}, eventDef.ArgTypes) + require.Equal(t, []bool{true}, eventDef.ArgIndexed) + + require.Equal(t, 1, len(eventValuesHex)) + require.Equal(t, "0x98442bdf09d14ed3859c77b17990a0fc", eventValuesHex[0]) +} + func TestEventDecoder(t *testing.T) { ed := ethcoder.NewEventDecoder()