-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnear.go
343 lines (313 loc) · 9.43 KB
/
near.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
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
335
336
337
338
339
340
341
342
343
// Package near allows to interact with the NEAR platform via RPC calls.
package near
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/aurora-is-near/go-jsonrpc/v3"
)
// Connection allows to do JSON-RPC to a NEAR endpoint.
type Connection struct {
c jsonrpc.RPCClient
}
// NewConnection returns a new connection for JSON-RPC calls to the NEAR
// endpoint with the given nodeURL.
func NewConnection(nodeURL string) *Connection {
return NewConnectionWithTimeout(nodeURL, 0)
}
// NewConnectionWithTimeout returns a new connection for JSON-RPC calls to the NEAR
// endpoint with the given nodeURL with the given timeout.
func NewConnectionWithTimeout(nodeURL string, timeout time.Duration) *Connection {
var c Connection
c.c = jsonrpc.NewClientWithOpts(nodeURL, &jsonrpc.RPCClientOpts{
HTTPClient: &http.Client{
Timeout: timeout,
},
})
return &c
}
// call uses the connection c to call the given method with params.
// It handles all possible error cases and returns the result (which cannot be nil).
func (c *Connection) call(method string, params ...interface{}) (interface{}, error) {
start := time.Now()
res, err := c.c.Call(method, params...)
if err != nil {
return nil, err
}
if res.Error != nil {
if res.Error.Data != nil {
return nil, fmt.Errorf("near: jsonrpc: %d: %s: %v (after %s)",
res.Error.Code, res.Error.Message, res.Error.Data, time.Since(start))
}
return nil, fmt.Errorf("near: jsonrpc: %d: %s (after %s)",
res.Error.Code, res.Error.Message, time.Since(start))
}
if res.Result == nil {
return nil, fmt.Errorf("near: JSON-RPC result is nil (after %s)", time.Since(start))
}
return res.Result, nil
}
// Call performs a generic call of the given NEAR JSON-RPC method with params.
// It handles all possible error cases and returns the result (which cannot be nil).
func (c *Connection) Call(method string, params ...interface{}) (interface{}, error) {
return c.call(method, params...)
}
// Block queries network and returns latest block.
//
// For details see https://docs.near.org/docs/interaction/rpc#block
func (c *Connection) Block() (map[string]interface{}, error) {
res, err := c.call("block", map[string]string{
"finality": "final",
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// Block with given blockHash queries network and returns block with given hash.
//
// For details see https://docs.near.org/api/rpc/block-chunk#block-details
func (c *Connection) GetBlockByHash(blockHash string) (map[string]interface{}, error) {
res, err := c.call("block", map[string]string{
"block_id": blockHash,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// Block with given blockId queries network and returns block with given ID.
//
// For details see https://docs.near.org/api/rpc/block-chunk#block-details
func (c *Connection) GetBlockByID(blockID uint64) (map[string]interface{}, error) {
res, err := c.call("block", map[string]interface{}{
"block_id": blockID,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetNodeStatus returns general status of a given node.
//
// For details see
// https://docs.near.org/docs/api/rpc/network#node-status
func (c *Connection) GetNodeStatus() (map[string]interface{}, error) {
res, err := c.call("status", map[string]string{})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetAccountState returns basic account information for given accountID.
//
// For details see
// https://docs.near.org/docs/api/rpc/contracts#view-account
func (c *Connection) GetAccountState(accountID string) (map[string]interface{}, error) {
res, err := c.call("query", map[string]string{
"request_type": "view_account",
"finality": "final",
"account_id": accountID,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetContractCode returns the contract code (Wasm binary) deployed to the account.
//
// For details see
// https://docs.near.org/docs/api/rpc/contracts#view-contract-code
func (c *Connection) GetContractCode(accountID string) (map[string]interface{}, error) {
res, err := c.call("query", map[string]string{
"request_type": "view_code",
"finality": "final",
"account_id": accountID,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// SendTransaction sends a signed transaction and waits until the transaction
// is fully complete. Has a 10 second timeout.
//
// For details see
// https://docs.near.org/docs/develop/front-end/rpc#send-transaction-await
func (c *Connection) SendTransaction(signedTransaction []byte) (map[string]interface{}, error) {
res, err := c.call("broadcast_tx_commit",
base64.StdEncoding.EncodeToString(signedTransaction))
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// SendTransactionAsync sends a signed transaction and immediately returns a
// transaction hash.
//
// For details see
// https://docs.near.org/docs/develop/front-end/rpc#send-transaction-async
func (c *Connection) SendTransactionAsync(signedTransaction []byte) (string, error) {
res, err := c.call("broadcast_tx_async",
base64.StdEncoding.EncodeToString(signedTransaction))
if err != nil {
return "", err
}
r, ok := res.(string)
if !ok {
return "", ErrNotString
}
return r, nil
}
// ViewAccessKey returns information about a single access key for given accountID and publicKey.
// The publicKey must have a signature algorithm prefix (like "ed25519:").
//
// For details see
// https://docs.near.org/docs/develop/front-end/rpc#view-access-key
func (c *Connection) ViewAccessKey(accountID, publicKey string) (map[string]interface{}, error) {
res, err := c.call("query", map[string]string{
"request_type": "view_access_key",
"finality": "final",
"account_id": accountID,
"public_key": publicKey,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// ViewAccessKeyList returns all access keys for the given accountID.
//
// For details see
// https://docs.near.org/docs/api/rpc/access-keys#view-access-key-list
func (c *Connection) ViewAccessKeyList(accountID string) (map[string]interface{}, error) {
res, err := c.call("query", map[string]string{
"request_type": "view_access_key_list",
"finality": "final",
"account_id": accountID,
})
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetTransactionDetails returns information about a single transaction.
//
// For details see
// https://docs.near.org/api/rpc/transactions#transaction-status
func (c *Connection) GetTransactionDetails(txHash, senderAccountId string) (map[string]interface{}, error) {
params := []interface{}{txHash, senderAccountId}
res, err := c.call("tx", params)
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetTransactionDetailsWithWait returns information about a single transaction after waiting for the specified execution status.
//
// For details see
// https://docs.near.org/api/rpc/transactions#transaction-status
func (c *Connection) GetTransactionDetailsWithWait(txHash, senderAccountId string, waitUntil TxExecutionStatus) (map[string]interface{}, error) {
params := map[string]interface{}{
"tx_hash": txHash,
"sender_account_id": senderAccountId,
"wait_until": waitUntil,
}
res, err := c.call("tx", params)
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetTransactionDetailsWithReceipts returns information about a single transaction with receipts
//
// For details see
// https://docs.near.org/api/rpc/transactions#transaction-status-with-receipts
func (c *Connection) GetTransactionDetailsWithReceipts(txHash, senderAccountId string) (map[string]interface{}, error) {
params := []interface{}{txHash, senderAccountId}
res, err := c.call("EXPERIMENTAL_tx_status", params)
if err != nil {
return nil, err
}
r, ok := res.(map[string]interface{})
if !ok {
return nil, ErrNotObject
}
return r, nil
}
// GetTransactionLastResult decodes the last transaction result from a JSON
// map and tries to deterimine if we have an error condition.
func GetTransactionLastResult(txResult map[string]interface{}) (interface{}, error) {
status, ok := txResult["status"].(map[string]interface{})
if ok {
enc, ok := status["SuccessValue"].(string)
if ok {
buf, err := base64.URLEncoding.DecodeString(enc)
if err != nil {
return nil, err
}
if len(buf) == 0 {
return nil, nil
}
var jsn interface{}
if err := json.Unmarshal(buf, &jsn); err != nil {
// if we cannot unmarshal as JSON just return the buffer as a string
return string(buf), nil
}
return jsn, nil
} else if status["Failure"] != nil {
jsn, err := json.MarshalIndent(status["Failure"], "", " ")
if err != nil {
return nil, err
}
return nil, fmt.Errorf("failure:\n%s", string(jsn))
}
}
return nil, nil
}