-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_api.go
More file actions
334 lines (270 loc) · 8.4 KB
/
Copy pathnode_api.go
File metadata and controls
334 lines (270 loc) · 8.4 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package ethrpc
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"net/http"
)
type NodeAPI struct {
url string
client *http.Client
debug bool
}
// New create new rpc client with given url
func NewNodeAPI(url string, options ...func(x *NodeAPI)) *NodeAPI {
rpc := &NodeAPI{
url: url,
client: http.DefaultClient,
}
for _, option := range options {
option(rpc)
}
return rpc
}
func (x *NodeAPI) String() string {
return "node-" + x.url
}
func (x *NodeAPI) Debug(debug bool) {
x.debug = debug
}
func (x *NodeAPI) call(method string, target interface{}, params ...interface{}) error {
result, err := x.Call(method, params...)
if err != nil {
return err
}
if target == nil {
return nil
}
if bytes.Equal(result, []byte("null")) {
return fmt.Errorf("result null")
}
return json.Unmarshal(result, target)
}
//batch request
func (x *NodeAPI) BatchCall(reqs []EthRequest) ([]EthResponse, error) {
body, err := json.Marshal(reqs)
if err != nil {
return nil, err
}
response, err := x.client.Post(x.url, "application/json", bytes.NewBuffer(body))
if response != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http status code error %d", response.StatusCode)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if x.debug {
fmt.Printf("requests %s responses %s\n", body, data)
}
resps := &[]EthResponse{}
if err := json.Unmarshal(data, resps); err != nil {
return nil, err
}
return *resps, nil
}
// Call returns raw response of method call
func (x *NodeAPI) Call(method string, params ...interface{}) (json.RawMessage, error) {
request := EthRequest{
ID: 1,
Version: "2.0",
Method: method,
Params: params,
}
body, err := json.Marshal(request)
if err != nil {
return nil, err
}
response, err := x.client.Post(x.url, "application/json", bytes.NewBuffer(body))
if response != nil {
defer response.Body.Close()
}
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http status code error %d", response.StatusCode)
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
if x.debug {
fmt.Printf("request %s %s response %s\n", method, body, data)
}
resp := new(EthResponse)
if err := json.Unmarshal(data, resp); err != nil {
return nil, err
}
if resp.Error != nil {
return nil, *resp.Error
}
return resp.Result, nil
}
// Web3ClientVersion returns the current client version.
func (x *NodeAPI) Web3ClientVersion() (string, error) {
var clientVersion string
err := x.call("web3_clientVersion", &clientVersion)
return clientVersion, err
}
// NetVersion returns the current network protocol version.
func (x *NodeAPI) NetVersion() (string, error) {
var version string
err := x.call("net_version", &version)
return version, err
}
// NetListening returns true if client is actively listening for network connections.
func (x *NodeAPI) NetListening() (bool, error) {
var listening bool
err := x.call("net_listening", &listening)
return listening, err
}
// NetPeerCount returns number of peers currently connected to the client.
func (x *NodeAPI) NetPeerCount() (int, error) {
var response string
if err := x.call("net_peerCount", &response); err != nil {
return 0, err
}
return ParseInt(response)
}
// EthProtocolVersion returns the current ethereum protocol version.
func (x *NodeAPI) EthProtocolVersion() (string, error) {
var protocolVersion string
err := x.call("eth_protocolVersion", &protocolVersion)
return protocolVersion, err
}
// EthSyncing returns an object with data about the sync status or false.
func (x *NodeAPI) EthSyncing() (*Syncing, error) {
result, err := x.Call("eth_syncing")
if err != nil {
return nil, err
}
syncing := new(Syncing)
if bytes.Equal(result, []byte("false")) {
return syncing, nil
}
err = json.Unmarshal(result, syncing)
return syncing, err
}
// EthGasPrice returns the current price per gas in wei.
func (x *NodeAPI) EthGasPrice() (big.Int, error) {
var response string
if err := x.call("eth_gasPrice", &response); err != nil {
return big.Int{}, err
}
return ParseBigInt(response)
}
// EthBlockNumber returns the number of most recent block.
func (x *NodeAPI) EthBlockNumber() (int, error) {
var response string
if err := x.call("eth_blockNumber", &response); err != nil {
return 0, err
}
return ParseInt(response)
}
// EthGetBalance returns the balance of the account of given address in wei.
func (x *NodeAPI) EthGetBalance(address, block string) (big.Int, error) {
var response string
if err := x.call("eth_getBalance", &response, address, block); err != nil {
return big.Int{}, err
}
return ParseBigInt(response)
}
// EthGetStorageAt returns the value from a storage position at a given address.
func (x *NodeAPI) EthGetStorageAt(data string, position int, tag string) (string, error) {
var result string
err := x.call("eth_getStorageAt", &result, data, IntToHex(position), tag)
return result, err
}
// EthGetTransactionCount returns the number of transactions sent from an address.
func (x *NodeAPI) EthGetTransactionCount(address, block string) (int, error) {
var response string
if err := x.call("eth_getTransactionCount", &response, address, block); err != nil {
return 0, err
}
return ParseInt(response)
}
// EthGetBlockTransactionCountByNumber returns the number of transactions in a block from a block matching the given block
func (x *NodeAPI) EthGetBlockTransactionCountByNumber(number int) (int, error) {
var response string
if err := x.call("eth_getBlockTransactionCountByNumber", &response, IntToHex(number)); err != nil {
return 0, err
}
return ParseInt(response)
}
func (x *NodeAPI) getBlock(method string, withTransactions bool, params ...interface{}) (*Block, error) {
var response ProxyBlock
if withTransactions {
response = new(ProxyBlockWithTransactions)
} else {
response = new(ProxyBlockWithoutTransactions)
}
err := x.call(method, response, params...)
if err != nil {
return nil, err
}
block := response.ToBlock()
if len(block.Hash) == 0 {
return nil, fmt.Errorf("block not found")
}
return &block, nil
}
// EthGetBlockByNumber returns information about a block by block number.
func (x *NodeAPI) EthGetBlockByNumber(number int, withTransactions bool) (*Block, error) {
return x.getBlock("eth_getBlockByNumber", withTransactions, IntToHex(number), withTransactions)
}
func (x *NodeAPI) EthGetUncleByBlockNumberAndIndex(number int, pos int) (*Block, error) {
uncleBlock := new(UncleBlock)
err := x.call("eth_getUncleByBlockNumberAndIndex", uncleBlock, IntToHex(number), IntToHex(pos))
if err != nil {
return nil, err
}
block := uncleBlock.ToBlock()
if len(block.Hash) == 0 {
return nil, fmt.Errorf("block not found")
}
return &block, nil
}
func (x *NodeAPI) getTransaction(method string, params ...interface{}) (*Transaction, error) {
transaction := new(Transaction)
err := x.call(method, transaction, params...)
if len(transaction.Hash) == 0 {
return nil, fmt.Errorf("tx not found")
}
return transaction, err
}
// EthGetTransactionByHash returns the information about a transaction requested by transaction hash.
func (x *NodeAPI) EthGetTransactionByHash(hash string) (*Transaction, error) {
return x.getTransaction("eth_getTransactionByHash", hash)
}
// EthGetTransactionByBlockNumberAndIndex returns information about a transaction by block number and transaction index position.
func (x *NodeAPI) EthGetTransactionByBlockNumberAndIndex(blockNumber, transactionIndex int) (*Transaction, error) {
return x.getTransaction("eth_getTransactionByBlockNumberAndIndex", IntToHex(blockNumber), IntToHex(transactionIndex))
}
// EthGetTransactionReceipt returns the receipt of a transaction by transaction hash.
// Note That the receipt is not available for pending transactions.
func (x *NodeAPI) EthGetTransactionReceipt(hash string) (*TransactionReceipt, error) {
transactionReceipt := new(TransactionReceipt)
err := x.call("eth_getTransactionReceipt", transactionReceipt, hash)
if err != nil {
return nil, err
}
if len(transactionReceipt.TransactionHash) == 0 {
return nil, fmt.Errorf("receipt not found")
}
return transactionReceipt, nil
}
// EthGetLogs returns an array of all logs matching a given filter object.
func (x *NodeAPI) EthGetLogs(params FilterParams) ([]Log, error) {
var logs []Log
err := x.call("eth_getLogs", &logs, params)
return logs, err
}