-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathethcmd.go
125 lines (111 loc) · 3.19 KB
/
ethcmd.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"tools/net"
utils "tools/util"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/urfave/cli/v2"
)
var (
logsCommand = cli.Command{
Name: "logs",
Usage: "Query eth logs with given address, from block and topics, `page` logs at a query",
Action: func(c *cli.Context) error {
if c.NArg() != 4 {
return errors.New("logs subcommand needs address, from block, topics and page args")
}
address := c.Args().Get(0)
fromBlock, _ := strconv.Atoi(c.Args().Get(1))
topics := strings.Split(c.Args().Get(2), ",")
page, _ := strconv.Atoi(c.Args().Get(3))
latestBlockNumber := int(getLatestBlockNumber())
if latestBlockNumber != -1 {
logs := make([]Log, 0)
bar := utils.NewBar(0, latestBlockNumber-fromBlock)
bar.Load()
for i := fromBlock; i < latestBlockNumber; i += page + 1 {
var params []GetLogsParam
params = append(params, GetLogsParam{
Address: address,
FromBlock: hexutil.EncodeUint64(uint64(i)),
ToBlock: hexutil.EncodeUint64(uint64(i + page)),
Topics: topics})
responseData := sendJsonRPCRequest("eth_getLogs", params)
var response GetLogsRPCResponse
err := json.Unmarshal(responseData, &response)
if err != nil {
return err
}
for _, log := range response.Result {
logs = append(logs, log)
}
bar.Add(page)
}
fmt.Print("\n")
for _, log := range logs {
logData, _ := json.Marshal(log)
fmt.Println(string(logData))
}
}
return nil
},
}
)
type RPCReq struct {
JsonRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
Id uint64 `json:"id"`
}
func sendJsonRPCRequest(method string, params interface{}) []byte {
req := RPCReq{JsonRPC: "2.0",
Method: method,
Params: params,
Id: 1}
if reqData, err := json.Marshal(&req); err == nil {
return net.Post("http://47.90.254.215:8545/", reqData)
}
return nil
}
func getLatestBlockNumber() int64 {
responseData := sendJsonRPCRequest("eth_blockNumber", nil)
var response SingleStringRPCResponse
err := json.Unmarshal(responseData, &response)
if err != nil {
return -1
}
blockNumberData, _ := hexutil.Decode(response.Result)
return new(big.Int).SetBytes(blockNumberData).Int64()
}
type SingleStringRPCResponse struct {
JsonRPC string `json:"jsonrpc"`
Result string `json:"result"`
Id uint64 `json:"id"`
}
type GetLogsParam struct {
Address string `json:"address"`
FromBlock string `json:"fromBlock"`
ToBlock string `json:"toBlock"`
Topics []string `json:"topics"`
}
type GetLogsRPCResponse struct {
JsonRPC string `json:"jsonrpc"`
Result []Log `json:"result"`
Id uint64 `json:"id"`
}
type Log struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data"`
BlockNumber string `json:"blockNumber"`
TransactionHash string `json:"transactionHash"`
TransactionIndex string `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
LogIndex string `json:"logIndex"`
Removed bool `json:"removed"`
}