-
Notifications
You must be signed in to change notification settings - Fork 0
/
nearclient.go
134 lines (110 loc) · 2.85 KB
/
nearclient.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
126
127
128
129
130
131
132
133
134
package nearclient
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type Request struct {
Version string `json:"jsonrpc,omitempty"`
Id string `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
}
type FunctionCallParams struct {
Request_type string `json:"request_type"`
Finality string `json:"finality"`
Account_id string `json:"account_id"`
Method_name string `json:"method_name"`
Args_base64 string `json:"args_base64"`
}
type Response struct {
Version string `json:"jsonrpc,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Id string `json:"id,omitempty"`
}
type FunctionCallResponse struct {
Result json.RawMessage `json:"result,omitempty"`
Error string `json:"error,omitempty"`
Logs []string `json:"logs,omitempty"`
BlockHeight int64 `json:"block_height,omitempty"`
BlockHash string `json:"block_hash,omitempty"`
}
type Client struct {
URL string
}
func RequestJSON(method string, params interface{}) ([]byte, error) {
enc_params, err := json.Marshal(params)
if err != nil {
return nil, err
}
request := Request{
Version: "2.0",
Id: "dontcare",
Method: method,
Params: enc_params,
}
enc, err_r := json.Marshal(request)
if err != nil {
return nil, err_r
}
return enc, nil
}
func (c *Client) MakeRequest(data []byte) ([]byte, error) {
body := bytes.NewBuffer(data)
//Leverage Go's HTTP Post function to make request
resp, err := http.Post(c.URL, "application/json", body)
//Handle Error
if err != nil {
log.Fatalf("An Error Occured %v", err)
return nil, err
}
defer resp.Body.Close()
//Read the response body
resp_body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil, err
}
var resp_dec Response
if err := json.Unmarshal(resp_body, &resp_dec); err != nil {
return nil, err
}
return resp_dec.Result, nil
}
func (c *Client) FunctionCall(account_id string, method_name string, args string) (*FunctionCallResponse, error) {
params := FunctionCallParams{
Request_type: "call_function",
Finality: "final",
Account_id: account_id,
Method_name: method_name,
Args_base64: args,
}
data, err := RequestJSON("query", params)
if err != nil {
return nil, err
}
resp, err := c.MakeRequest(data)
if err != nil {
return nil, err
}
var resp_dec FunctionCallResponse
if err := json.Unmarshal(resp, &resp_dec); err != nil {
return nil, err
}
return &resp_dec, nil
}
//Returns general status of current validator nodes.
func (c *Client) Status() ([]byte, error) {
params := make([]string, 0)
data, err := RequestJSON("status", params)
if err != nil {
return nil, err
}
resp, err := c.MakeRequest(data)
if err != nil {
return nil, err
}
return resp, nil
}