/
client.go
78 lines (63 loc) · 2.08 KB
/
client.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
package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
// JSONRPCCall calls JSONRPCCallWithContext with the default context
func JSONRPCCall(url, method string, params ...interface{}) (Response, error) {
return JSONRPCCallWithContext(context.Background(), url, method, params...)
}
// JSONRPCCallWithContext executes a 2.0 JSON RPC HTTP Post Request to the provided URL with
// the provided method and parameters, which is compatible with the Ethereum
// JSON RPC Server.
func JSONRPCCallWithContext(ctx context.Context, url, method string, parameters ...interface{}) (Response, error) {
httpReq, err := BuildJsonHTTPRequest(ctx, url, method, parameters...)
if err != nil {
return Response{}, err
}
httpRes, err := http.DefaultClient.Do(httpReq)
if err != nil {
return Response{}, err
}
if httpRes.Body != nil {
defer httpRes.Body.Close()
}
if httpRes.StatusCode != http.StatusOK {
return Response{}, fmt.Errorf("invalid status code, expected: %v, found: %v", http.StatusOK, httpRes.StatusCode)
}
var res Response
if err = json.NewDecoder(httpRes.Body).Decode(&res); err != nil {
return Response{}, err
}
return res, nil
}
// BuildJsonHTTPRequest creates JSON RPC http request using provided url, method and parameters
func BuildJsonHTTPRequest(ctx context.Context, url, method string, parameters ...interface{}) (*http.Request, error) {
params, err := json.Marshal(parameters)
if err != nil {
return nil, err
}
req := Request{
JSONRPC: "2.0",
ID: float64(1),
Method: method,
Params: params,
}
reqBody, err := json.Marshal(req)
if err != nil {
return nil, err
}
return BuildJsonHttpRequestWithBody(ctx, url, reqBody)
}
// BuildJsonHttpRequestWithBody creates JSON RPC http request using provided url and request body
func BuildJsonHttpRequestWithBody(ctx context.Context, url string, reqBody []byte) (*http.Request, error) {
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(reqBody))
if err != nil {
return nil, err
}
httpReq.Header.Add("Content-type", "application/json")
return httpReq, nil
}