|
4 | 4 | "encoding/hex" |
5 | 5 | "math/big" |
6 | 6 | "strings" |
| 7 | + "sync" |
7 | 8 |
|
8 | 9 | "github.com/ethereum/go-ethereum/accounts/abi" |
9 | 10 | "github.com/rs/zerolog/log" |
@@ -52,6 +53,49 @@ type DecodedTransaction struct { |
52 | 53 | Decoded DecodedTransactionData `json:"decodedData"` |
53 | 54 | } |
54 | 55 |
|
| 56 | +func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction { |
| 57 | + decodedTxs := make([]*DecodedTransaction, len(txs)) |
| 58 | + abiCache := make(map[string]*abi.ABI) |
| 59 | + decodeTxFunc := func(transaction *Transaction) *DecodedTransaction { |
| 60 | + decodedTransaction := DecodedTransaction{Transaction: *transaction} |
| 61 | + abi := GetABIForContractWithCache(chainId, transaction.ToAddress, abiCache) |
| 62 | + if abi == nil { |
| 63 | + return &decodedTransaction |
| 64 | + } |
| 65 | + |
| 66 | + decodedData, err := hex.DecodeString(strings.TrimPrefix(transaction.Data, "0x")) |
| 67 | + if err != nil { |
| 68 | + return &decodedTransaction |
| 69 | + } |
| 70 | + |
| 71 | + if len(decodedData) < 4 { |
| 72 | + return &decodedTransaction |
| 73 | + } |
| 74 | + methodID := decodedData[:4] |
| 75 | + method, err := abi.MethodById(methodID) |
| 76 | + if err != nil { |
| 77 | + log.Debug().Msgf("failed to get method by id: %v", err) |
| 78 | + return &decodedTransaction |
| 79 | + } |
| 80 | + if method == nil { |
| 81 | + return &decodedTransaction |
| 82 | + } |
| 83 | + return transaction.Decode(method) |
| 84 | + } |
| 85 | + |
| 86 | + var wg sync.WaitGroup |
| 87 | + for idx, transaction := range txs { |
| 88 | + wg.Add(1) |
| 89 | + go func(idx int, transaction Transaction) { |
| 90 | + defer wg.Done() |
| 91 | + decodedTx := decodeTxFunc(&transaction) |
| 92 | + decodedTxs[idx] = decodedTx |
| 93 | + }(idx, transaction) |
| 94 | + } |
| 95 | + wg.Wait() |
| 96 | + return decodedTxs |
| 97 | +} |
| 98 | + |
55 | 99 | func (t *Transaction) Decode(functionABI *abi.Method) *DecodedTransaction { |
56 | 100 | decodedData, err := hex.DecodeString(strings.TrimPrefix(t.Data, "0x")) |
57 | 101 | if err != nil { |
|
0 commit comments