forked from dcb9/janus
-
Notifications
You must be signed in to change notification settings - Fork 27
/
eth_getLogs.go
104 lines (88 loc) · 2.87 KB
/
eth_getLogs.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package transformer
import (
"context"
"encoding/json"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/conversion"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
"github.com/qtumproject/janus/pkg/utils"
)
// ProxyETHGetLogs implements ETHProxy
type ProxyETHGetLogs struct {
*qtum.Qtum
}
func (p *ProxyETHGetLogs) Method() string {
return "eth_getLogs"
}
func (p *ProxyETHGetLogs) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var req eth.GetLogsRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
// TODO: Graph Node is sending the topic
// if len(req.Topics) != 0 {
// return nil, errors.New("topics is not supported yet")
// }
// Calls ToRequest in order transform ETH-Request to a Qtum-Request
qtumreq, err := p.ToRequest(c.Request().Context(), &req)
if err != nil {
return nil, err
}
return p.request(c.Request().Context(), qtumreq)
}
func (p *ProxyETHGetLogs) request(ctx context.Context, req *qtum.SearchLogsRequest) (*eth.GetLogsResponse, eth.JSONRPCError) {
receipts, err := conversion.SearchLogsAndFilterExtraTopics(ctx, p.Qtum, req)
if err != nil {
return nil, err
}
logs := make([]eth.Log, 0)
for _, receipt := range receipts {
r := qtum.TransactionReceipt(receipt)
logs = append(logs, conversion.ExtractETHLogsFromTransactionReceipt(r, r.Log)...)
}
resp := eth.GetLogsResponse(logs)
return &resp, nil
}
func (p *ProxyETHGetLogs) ToRequest(ctx context.Context, ethreq *eth.GetLogsRequest) (*qtum.SearchLogsRequest, eth.JSONRPCError) {
//transform EthRequest fromBlock to QtumReq fromBlock:
from, err := getBlockNumberByRawParam(ctx, p.Qtum, ethreq.FromBlock, true)
if err != nil {
return nil, err
}
//transform EthRequest toBlock to QtumReq toBlock:
to, err := getBlockNumberByRawParam(ctx, p.Qtum, ethreq.ToBlock, true)
if err != nil {
return nil, err
}
//transform EthReq address to QtumReq address:
var addresses []string
if ethreq.Address != nil {
if isBytesOfString(ethreq.Address) {
var addr string
if jsonErr := json.Unmarshal(ethreq.Address, &addr); jsonErr != nil {
return nil, eth.NewInvalidParamsError(jsonErr.Error())
}
addresses = append(addresses, addr)
} else {
if jsonErr := json.Unmarshal(ethreq.Address, &addresses); jsonErr != nil {
return nil, eth.NewInvalidParamsError(jsonErr.Error())
}
}
for i := range addresses {
addresses[i] = utils.RemoveHexPrefix(addresses[i])
}
}
//transform EthReq topics to QtumReq topics:
topics, topicsErr := eth.TranslateTopics(ethreq.Topics)
if topicsErr != nil {
return nil, eth.NewCallbackError(topicsErr.Error())
}
return &qtum.SearchLogsRequest{
Addresses: addresses,
FromBlock: from,
ToBlock: to,
Topics: qtum.NewSearchLogsTopics(topics),
}, nil
}