forked from textileio/near-api-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
376 lines (332 loc) · 11.9 KB
/
api.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package api
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/rpc"
"github.com/gateway-fm/near-api-go/models"
itypes "github.com/gateway-fm/near-api-go/types"
"github.com/gateway-fm/near-api-go/account"
"github.com/gateway-fm/near-api-go/config"
"github.com/gateway-fm/near-api-go/util"
)
// CallFunctionResponse holds information about the result of a function call.
type CallFunctionResponse struct {
Result []byte `json:"result"`
Logs []string `json:"logs"`
BlockHeight int `json:"block_height"`
BlockHash string `json:"block_hash"`
}
// ViewCodeResponse holds information about contract code.
type ViewCodeResponse struct {
CodeBase64 string `json:"code_base64"`
Hash string `json:"hash"`
BlockHeight int `json:"block_height"`
BlockHash string `json:"block_hash"`
}
// Change holds information about a state change of a key-value pair.
type Change struct {
AccountID string `json:"account_id"`
KeyBase64 string `json:"key_base64"`
ValueBase64 string `json:"value_base64"`
}
// Cause holds information about the cause of a state change.
type Cause struct {
Type string `json:"type"`
ReceiptHash string `json:"receipt_hash"`
}
// ChangeData holds information about a state change.
type ChangeData struct {
Cause Cause `json:"cause"`
Type string `json:"type"`
Change Change `json:"change"`
}
// DataChangesResponse holds information about all data changes in a block.
type DataChangesResponse struct {
BlockHash string `json:"block_hash"`
Changes []ChangeData `json:"changes"`
}
// SyncInfo holds information about the sync status of a node.
type SyncInfo struct {
LatestBlockHash string `json:"latest_block_hash"`
LatestBlockHeight int `json:"latest_block_height"`
// TODO: make this time.Time and use custom json conversion.
LatestBlockTime string `json:"latest_block_time"`
LatestStateRoot string `json:"latest_state_root"`
Syncing bool `json:"syncing"`
}
// Version struct
type Version struct {
Build string `json:"build"`
Version string `json:"version"`
}
// Validators array
type Validators []Validator
// ValidatorsResponse struct
type ValidatorsResponse struct {
CurrentFishermans []Fisherman `json:"current_fishermen,omitempty"`
NextFishermans []Fisherman `json:"next_fishermen,omitempty"`
CurrentValidators Validators `json:"current_validators,omitempty"`
NextValidators Validators `json:"next_validators,omitempty"`
CurrentProposal string `json:"current_proposal,omitempty"`
EpochStartHeight int64 `json:"epoch_start_height"`
PrevEpochKickout []EpochKickout `json:"prev_epoch_kickout"`
}
// EpochKickout struct
type EpochKickout struct {
AccountID string `json:"account_id"`
Reason map[string]interface{} `json:"reason"`
}
// Fisherman struct
type Fisherman struct {
AccountID string `json:"account_id"`
PublicKey string `json:"public_key"`
Stake string `json:"stake"`
}
// Validator struct
type Validator struct {
AccountID string `json:"account_id"`
IsSlashed bool `json:"is_slashed"`
ExpectedBlocksNum int64 `json:"num_expected_blocks,omitempty"`
ProducedBlocksNum int64 `json:"num_produced_blocks,omitempty"`
PublicKey string `json:"public_key,omitempty"`
Shards []int64 `json:"shards,omitempty"`
Stake string `json:"stake,omitempty"`
}
// NodeStatusResponse holds information about node status.
type NodeStatusResponse struct {
ChainID string `json:"chain_id"`
RPCAddr string `json:"rpc_addr"`
SyncInfo SyncInfo `json:"sync_info"`
Validators Validators `json:"validators"`
Version Version `json:"version"`
}
// Client communicates with the NEAR API.
type Client struct {
config *config.Config
}
// NewClient creates a new Client.
func NewClient(config *config.Config) (*Client, error) {
return &Client{
config: config,
}, nil
}
// Account provides an API for the provided account ID.
func (c *Client) Account(accountID string) *account.Account {
return account.NewAccount(c.config, accountID)
}
// CallFunctionOption controls the behavior when calling CallFunction.
type CallFunctionOption func(*itypes.QueryRequest) error
// CallFunctionWithFinality specifies the finality to be used when calling the function.
func CallFunctionWithFinality(finalaity string) CallFunctionOption {
return func(qr *itypes.QueryRequest) error {
qr.Finality = finalaity
return nil
}
}
// CallFunctionWithBlockHeight specifies the block height to call the function for.
func CallFunctionWithBlockHeight(blockHeight int) CallFunctionOption {
return func(qr *itypes.QueryRequest) error {
qr.BlockID = blockHeight
return nil
}
}
// CallFunctionWithBlockHash specifies the block hash to call the function for.
func CallFunctionWithBlockHash(blockHash string) CallFunctionOption {
return func(qr *itypes.QueryRequest) error {
qr.BlockID = blockHash
return nil
}
}
// CallFunctionWithArgs specified the args to call the function with.
// Should be a JSON encodable object.
func CallFunctionWithArgs(args interface{}) CallFunctionOption {
return func(qr *itypes.QueryRequest) error {
if args == nil {
args = make(map[string]interface{})
}
bytes, err := json.Marshal(args)
if err != nil {
return err
}
qr.ArgsBase64 = base64.StdEncoding.EncodeToString(bytes)
return nil
}
}
// CallFunction calls a function on a contract.
func (c *Client) CallFunction(
ctx context.Context,
accountID string,
methodName string,
opts ...CallFunctionOption,
) (*CallFunctionResponse, error) {
req := &itypes.QueryRequest{
RequestType: "call_function",
AccountID: accountID,
MethodName: methodName,
}
for _, opt := range opts {
if err := opt(req); err != nil {
return nil, err
}
}
if req.BlockID == nil && req.Finality == "" {
return nil, fmt.Errorf(
"you must provide ViewAccountWithBlockHeight, ViewAccountWithBlockHash or ViewAccountWithFinality",
)
}
if req.BlockID != nil && req.Finality != "" {
return nil, fmt.Errorf(
"you must provide one of ViewAccountWithBlockHeight, ViewAccountWithBlockHash or ViewAccountWithFinality",
)
}
var res CallFunctionResponse
if err := c.config.RPCClient.CallContext(ctx, &res, "query", rpc.NewNamedParams(req)); err != nil {
return nil, fmt.Errorf("calling query rpc: %v", util.MapRPCError(err))
}
return &res, nil
}
// DataChangesOption controls behavior when calling DataChanges.
type DataChangesOption func(*itypes.ChangesRequest)
// DataChangesWithPrefix sets the data key prefix to query for.
func DataChangesWithPrefix(prefix string) DataChangesOption {
return func(cr *itypes.ChangesRequest) {
cr.KeyPrefixBase64 = base64.StdEncoding.EncodeToString([]byte(prefix))
}
}
// DataChangesWithFinality specifies the finality to be used when querying data changes.
func DataChangesWithFinality(finalaity string) DataChangesOption {
return func(qr *itypes.ChangesRequest) {
qr.Finality = finalaity
}
}
// DataChangesWithBlockHeight specifies the block id to query data changes for.
func DataChangesWithBlockHeight(blockHeight int) DataChangesOption {
return func(qr *itypes.ChangesRequest) {
qr.BlockID = blockHeight
}
}
// DataChangesWithBlockHash specifies the block id to query data changes for.
func DataChangesWithBlockHash(blockHash string) DataChangesOption {
return func(qr *itypes.ChangesRequest) {
qr.BlockID = blockHash
}
}
// DataChanges queries changes to contract data changes.
func (c *Client) DataChanges(
ctx context.Context,
accountIDs []string,
opts ...DataChangesOption,
) (*DataChangesResponse, error) {
req := &itypes.ChangesRequest{
ChangesType: "data_changes",
AccountIDs: accountIDs,
}
for _, opt := range opts {
opt(req)
}
if req.BlockID == nil && req.Finality == "" {
return nil, fmt.Errorf(
"you must provide DataChangesWithBlockHeight, DataChangesWithBlockHash or DataChangesWithFinality",
)
}
if req.BlockID != nil && req.Finality != "" {
return nil, fmt.Errorf(
"you must provide one of DataChangesWithBlockHeight, DataChangesWithBlockHash or DataChangesWithFinality",
)
}
var res DataChangesResponse
if err := c.config.RPCClient.CallContext(ctx, &res, "EXPERIMENTAL_changes", rpc.NewNamedParams(req)); err != nil {
return nil, fmt.Errorf("calling changes rpc: %v", util.MapRPCError(err))
}
return &res, nil
}
// ViewCodeOption controls the behavior when calling ViewCode.
type ViewCodeOption func(*itypes.QueryRequest)
// ViewCodeWithFinality specifies the finality to be used when calling the function.
func ViewCodeWithFinality(finalaity string) ViewCodeOption {
return func(qr *itypes.QueryRequest) {
qr.Finality = finalaity
}
}
// ViewCodeWithBlockHeight specifies the block height to call the function for.
func ViewCodeWithBlockHeight(blockHeight int) ViewCodeOption {
return func(qr *itypes.QueryRequest) {
qr.BlockID = blockHeight
}
}
// ViewCodeWithBlockHash specifies the block hash to call the function for.
func ViewCodeWithBlockHash(blockHash string) ViewCodeOption {
return func(qr *itypes.QueryRequest) {
qr.BlockID = blockHash
}
}
// ViewCode returns the smart contract code for the provided account id.
func (c *Client) ViewCode(ctx context.Context, accountID string, opts ...ViewCodeOption) (*ViewCodeResponse, error) {
req := &itypes.QueryRequest{
RequestType: "view_code",
AccountID: accountID,
Finality: "final",
}
for _, opt := range opts {
opt(req)
}
if req.BlockID == nil && req.Finality == "" {
return nil, fmt.Errorf(
"you must provide ViewAccountWithBlockHeight, ViewAccountWithBlockHash or ViewAccountWithFinality",
)
}
if req.BlockID != nil && req.Finality != "" {
return nil, fmt.Errorf(
"you must provide one of ViewAccountWithBlockHeight, ViewAccountWithBlockHash or ViewAccountWithFinality",
)
}
var viewCodeRes ViewCodeResponse
if err := c.config.RPCClient.CallContext(ctx, &viewCodeRes, "query", rpc.NewNamedParams(req)); err != nil {
return nil, fmt.Errorf("calling query rpc: %v", util.MapRPCError(err))
}
return &viewCodeRes, nil
}
// NodeStatus returns the node status.
func (c *Client) NodeStatus(ctx context.Context) (*NodeStatusResponse, error) {
var nodeStatusRes NodeStatusResponse
if err := c.config.RPCClient.CallContext(ctx, &nodeStatusRes, "status"); err != nil {
return nil, fmt.Errorf("calling status rpc: %v", util.MapRPCError(err))
}
return &nodeStatusRes, nil
}
// NetworkInfo returns the current state of node network
// connections (active peers, transmitted data, etc.)
func (c *Client) NetworkInfo(ctx context.Context) (*models.NetworkInfo, error) {
var networkInfo models.NetworkInfo
if err := c.config.RPCClient.CallContext(ctx, &networkInfo, "network_info"); err != nil {
return nil, fmt.Errorf("calling network info rpc: %v", util.MapRPCError(err))
}
return &networkInfo, nil
}
func (c *Client) GetBlockResult(ctx context.Context) (*itypes.BlockResult, error) {
var blockresultinfo itypes.BlockResult
if err := c.config.RPCClient.CallContext(ctx, &blockresultinfo, "block", rpc.NewNamedParams(itypes.BlockRequest{Finality: "final"})); err != nil {
return nil, fmt.Errorf("getting block returned an error: %w", err)
}
return &blockresultinfo, nil
}
func (c *Client) GetProtocolConfig(ctx context.Context) (*models.ProtocolConfig, error) {
var protocolConfig models.ProtocolConfig
if err := c.config.RPCClient.CallContext(ctx, &protocolConfig, "EXPERIMENTAL_protocol_config", rpc.NewNamedParams(itypes.BlockRequest{Finality: "final"})); err != nil {
return nil, fmt.Errorf("calling protocol config rpc: %v", util.MapRPCError(err))
}
return &protocolConfig, nil
}
type GasPriceOpts struct {
Blockheight int64
Blockhash string
}
func (c *Client) GetGasPrice(ctx context.Context, gp *GasPriceOpts) (*itypes.BlockHeader, error) {
var gasprice itypes.BlockHeader
if err := c.config.RPCClient.CallContext(ctx, &gasprice, "gas_price", gp); err != nil {
return nil, fmt.Errorf("getting gasprice returned an error: %w", err)
}
return &gasprice, nil
}