Skip to content

Commit 90321fb

Browse files
author
=
committed
feat: decode event logs
1 parent c48eec3 commit 90321fb

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

internal/common/log.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/hex"
55
"fmt"
66
"math/big"
7+
"sync"
78

89
"github.com/ethereum/go-ethereum/accounts/abi"
910
gethCommon "github.com/ethereum/go-ethereum/common"
@@ -39,6 +40,52 @@ type DecodedLog struct {
3940
Decoded DecodedLogData `json:"decodedData"`
4041
}
4142

43+
func DecodeLogs(chainId string, logs []Log) []*DecodedLog {
44+
decodedLogs := make([]*DecodedLog, len(logs))
45+
abis := make(map[string]*abi.ABI)
46+
47+
decodeLogFunc := func(eventLog *Log) *DecodedLog {
48+
decodedLog := DecodedLog{Log: *eventLog}
49+
abi, ok := abis[eventLog.Address]
50+
if !ok {
51+
abiResult, err := GetABIForContract(chainId, eventLog.Address)
52+
if err != nil {
53+
abis[eventLog.Address] = nil
54+
return &decodedLog
55+
} else {
56+
abis[eventLog.Address] = abiResult
57+
}
58+
abi = abiResult
59+
}
60+
61+
if abi == nil {
62+
return &decodedLog
63+
}
64+
65+
event, err := abi.EventByID(gethCommon.HexToHash(eventLog.Topics[0]))
66+
if err != nil {
67+
log.Debug().Msgf("failed to get method by id: %v", err)
68+
return &decodedLog
69+
}
70+
if event == nil {
71+
return &decodedLog
72+
}
73+
return eventLog.Decode(event)
74+
}
75+
76+
var wg sync.WaitGroup
77+
for idx, eventLog := range logs {
78+
wg.Add(1)
79+
go func(idx int, eventLog Log) {
80+
defer wg.Done()
81+
decodedLog := decodeLogFunc(&eventLog)
82+
decodedLogs[idx] = decodedLog
83+
}(idx, eventLog)
84+
}
85+
wg.Wait()
86+
return decodedLogs
87+
}
88+
4289
func (l *Log) Decode(eventABI *abi.Event) *DecodedLog {
4390

4491
decodedIndexed := make(map[string]interface{})

internal/common/transaction.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction
6060
decodeTxFunc := func(transaction *Transaction) *DecodedTransaction {
6161
decodedTransaction := DecodedTransaction{Transaction: *transaction}
6262
abi, ok := abis[transaction.ToAddress]
63-
// ABI not found yet
6463
if !ok {
6564
abiResult, err := GetABIForContract(chainId, transaction.ToAddress)
6665
if err != nil {

internal/handlers/logs_handlers.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ func handleLogsRequest(c *gin.Context, contractAddress, signature string, eventA
209209
}
210210
queryResult.Data = decodedLogs
211211
} else {
212-
queryResult.Data = logsResult.Data
212+
decodedLogs := common.DecodeLogs(chainId.String(), logsResult.Data)
213+
queryResult.Data = decodedLogs
213214
}
214215
queryResult.Meta.TotalItems = len(logsResult.Data)
215216
}

0 commit comments

Comments
 (0)