/
get_logs.go
74 lines (64 loc) · 1.66 KB
/
get_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
71
72
73
74
package eth
import (
"fmt"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/lmittmann/w3/internal/module"
"github.com/lmittmann/w3/w3types"
)
// Logs requests the logs of the given ethereum.FilterQuery q.
func Logs(q ethereum.FilterQuery) w3types.RPCCallerFactory[[]types.Log] {
return &logsFactory{filterQuery: q}
}
type logsFactory struct {
// args
filterQuery ethereum.FilterQuery
// returns
returns *[]types.Log
}
func (f *logsFactory) Returns(logs *[]types.Log) w3types.RPCCaller {
f.returns = logs
return f
}
// CreateRequest implements the w3types.RequestCreator interface.
func (f *logsFactory) CreateRequest() (rpc.BatchElem, error) {
arg, err := toFilterArg(f.filterQuery)
if err != nil {
return rpc.BatchElem{}, err
}
return rpc.BatchElem{
Method: "eth_getLogs",
Args: []any{arg},
Result: f.returns,
}, nil
}
// HandleResponse implements the w3types.ResponseHandler interface.
func (f *logsFactory) HandleResponse(elem rpc.BatchElem) error {
if err := elem.Error; err != nil {
return err
}
return nil
}
func toFilterArg(q ethereum.FilterQuery) (any, error) {
arg := map[string]any{
"topics": q.Topics,
}
if len(q.Addresses) > 0 {
arg["address"] = q.Addresses
}
if q.BlockHash != nil {
arg["blockHash"] = *q.BlockHash
if q.FromBlock != nil || q.ToBlock != nil {
return nil, fmt.Errorf("cannot specify both BlockHash and FromBlock/ToBlock")
}
} else {
if q.FromBlock == nil {
arg["fromBlock"] = "0x0"
} else {
arg["fromBlock"] = module.BlockNumberArg(q.FromBlock)
}
arg["toBlock"] = module.BlockNumberArg(q.ToBlock)
}
return arg, nil
}