-
Notifications
You must be signed in to change notification settings - Fork 2
/
nearclient.go
120 lines (104 loc) · 3 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
package nearclient
import (
"context"
"encoding/json"
"github.com/akme/nearclient/types"
"github.com/ethereum/go-ethereum/rpc"
)
// Client defines typed wrappers for the Ethereum RPC API.
type Client struct {
c *rpc.Client
}
// Dial connects a client to the given URL.
func Dial(rawurl string) (*Client, error) {
return DialContext(context.Background(), rawurl)
}
// DialContext with context
func DialContext(ctx context.Context, rawurl string) (*Client, error) {
c, err := rpc.DialContext(ctx, rawurl)
if err != nil {
return nil, err
}
return NewClient(c), nil
}
// NewClient creates a client that uses the given RPC client.
func NewClient(c *rpc.Client) *Client {
return &Client{c}
}
// Close client
func (nc *Client) Close() {
nc.c.Close()
}
// Validators gets list of validators
func (nc *Client) Validators(ctx context.Context, params interface{}) (*types.ValidatorsResponse, error) {
var raw json.RawMessage
if err := nc.c.CallContext(ctx, &raw, "validators", params); err != nil {
return nil, err
}
var resp *types.ValidatorsResponse
if err := json.Unmarshal(raw, &resp); err != nil {
return nil, err
}
return resp, nil
}
// GasPrice get the price of Gas
func (nc *Client) GasPrice(ctx context.Context, params interface{}) (*types.GasPrice, error) {
var raw json.RawMessage
if err := nc.c.CallContext(ctx, &raw, "gas_price", params); err != nil {
return nil, err
}
var gasprice types.GasPrice
if err := json.Unmarshal(raw, &gasprice); err != nil {
return nil, err
}
return &gasprice, nil
}
// Status gets status of the network
func (nc *Client) Status(ctx context.Context) (*types.Status, error) {
var raw json.RawMessage
var params interface{}
if err := nc.c.CallContext(ctx, &raw, "status", params); err != nil {
return nil, err
}
var status types.Status
if err := json.Unmarshal(raw, &status); err != nil {
return nil, err
}
return &status, nil
}
// TransactionStatus gets status of transaction
func (nc *Client) TransactionStatus(ctx context.Context, txHash, AccountID string) (*types.TransactionStatus, error) {
var raw json.RawMessage
if err := nc.c.CallContext(ctx, &raw, "tx", txHash, AccountID); err != nil {
return nil, err
}
var txstatus types.TransactionStatus
if err := json.Unmarshal(raw, &txstatus); err != nil {
return nil, err
}
return &txstatus, nil
}
// Block get block information
func (nc *Client) Block(ctx context.Context, params interface{}) (*types.Block, error) {
var raw json.RawMessage
if err := nc.c.CallContext(ctx, &raw, "block", params); err != nil {
return nil, err
}
var block types.Block
if err := json.Unmarshal(raw, &block); err != nil {
return nil, err
}
return &block, nil
}
// Chunk gets chunk info
func (nc *Client) Chunk(ctx context.Context, params interface{}) (*types.ChunkResponse, error) {
var raw json.RawMessage
if err := nc.c.CallContext(ctx, &raw, "chunk", params); err != nil {
return nil, err
}
var chunk types.ChunkResponse
if err := json.Unmarshal(raw, &chunk); err != nil {
return nil, err
}
return &chunk, nil
}