/
logs.go
70 lines (61 loc) · 1.97 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2019 the orbs-network-go authors
// This file is part of the orbs-network-go library in the Orbs project.
//
// This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.
// The above notice should be included in all copies or substantial portions of the software.
package adapter
import (
"bytes"
"context"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/orbs-network/orbs-spec/types/go/primitives"
"github.com/pkg/errors"
)
type TransactionLog struct {
ContractAddress []byte
PackedTopics [][]byte // indexed fields
Data []byte // non-indexed fields
RepackedData []byte
BlockNumber uint64
TxIndex uint32
}
func (c *connectorCommon) GetTransactionLogs(ctx context.Context, txHash primitives.Uint256, eventSignature []byte) ([]*TransactionLog, error) {
client, err := c.getContractCaller()
if err != nil {
return nil, err
}
receipt, err := client.TransactionReceipt(ctx, common.BytesToHash(txHash))
if err != nil {
return nil, errors.Wrapf(err, "error getting receipt for transaction with hash %s", txHash)
}
if receipt == nil {
return nil, errors.Wrapf(err, "got no logs for transaction with hash %s", txHash)
}
var eventLogs []*TransactionLog
for _, log := range receipt.Logs {
if matchesEvent(log, eventSignature) {
var topics [][]byte
for _, topic := range log.Topics {
topics = append(topics, topic.Bytes())
}
transactionLog := &TransactionLog{
PackedTopics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
TxIndex: uint32(log.TxIndex),
ContractAddress: log.Address.Bytes(),
}
eventLogs = append(eventLogs, transactionLog)
}
}
return eventLogs, nil
}
func matchesEvent(log *types.Log, eventSignature []byte) bool {
for _, topic := range log.Topics {
if bytes.Equal(topic.Bytes(), eventSignature) {
return true
}
}
return false
}