-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
283 lines (252 loc) · 8.53 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
package api
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/1artashes97/near-go/account"
itypes "github.com/1artashes97/near-go/internal/types"
"github.com/1artashes97/near-go/types"
"github.com/1artashes97/near-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"`
}
// NodeStatusResponse holds information about node status.
type NodeStatusResponse struct {
SyncInfo *SyncInfo `json:"sync_info"`
}
// Client communicates with the NEAR API.
type Client struct {
config *types.Config
}
// NewClient creates a new Client.
func NewClient(config *types.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", itypes.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", itypes.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", itypes.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
}