-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclient.go
122 lines (102 loc) · 2.83 KB
/
client.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
package cnc
import (
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/go-resty/resty/v2"
)
type Client struct {
c *resty.Client
}
func NewClient(baseURL string, options ...Option) (*Client, error) {
c := &Client{
c: resty.New(),
}
c.c.SetBaseURL(baseURL)
for _, option := range options {
if err := option(c); err != nil {
return nil, err
}
}
return c, nil
}
func (c *Client) Header(ctx context.Context, height uint64) /* Header */ error {
_ = headerPath()
return errors.New("method Header not implemented")
}
func (c *Client) Balance(ctx context.Context) error {
_ = balanceEndpoint
return errors.New("method Balance not implemented")
}
func (c *Client) SubmitTx(ctx context.Context, tx []byte) /* TxResponse */ error {
_ = submitTxEndpoint
return errors.New("method SubmitTx not implemented")
}
func (c *Client) SubmitPFB(ctx context.Context, namespace Namespace, data []byte, fee int64, gasLimit uint64) (*TxResponse, error) {
req := SubmitPFBRequest{
NamespaceID: hex.EncodeToString(namespace.Bytes()),
Data: hex.EncodeToString(data),
Fee: fee,
GasLimit: gasLimit,
}
var res TxResponse
var rpcErr string
_, err := c.c.R().
SetContext(ctx).
SetBody(req).
SetResult(&res).
SetError(&rpcErr).
Post(submitPFBEndpoint)
if err != nil {
return nil, err
}
if rpcErr != "" {
return nil, errors.New(rpcErr)
}
return &res, nil
}
func (c *Client) NamespacedShares(ctx context.Context, namespace Namespace, height uint64) ([][]byte, error) {
var res struct {
Shares [][]byte `json:"shares"`
Height uint64 `json:"height"`
}
err := c.callNamespacedEndpoint(ctx, namespace, height, namespacedSharesEndpoint, &res)
if err != nil {
return nil, err
}
return res.Shares, nil
}
func (c *Client) NamespacedData(ctx context.Context, namespace Namespace, height uint64) ([][]byte, error) {
var res struct {
Data [][]byte `json:"data"`
Height uint64 `json:"height"`
}
err := c.callNamespacedEndpoint(ctx, namespace, height, namespacedDataEndpoint, &res)
if err != nil {
return nil, err
}
return res.Data, nil
}
// callNamespacedEndpoint fetches result of /namespaced_{type} family of endpoints into result (this should be pointer!)
func (c *Client) callNamespacedEndpoint(ctx context.Context, namespace Namespace, height uint64, endpoint string, result interface{}) error {
var rpcErr string
_, err := c.c.R().
SetContext(ctx).
SetResult(result).
SetError(&rpcErr).
Get(namespacedPath(endpoint, namespace, height))
if err != nil {
return err
}
if rpcErr != "" {
return errors.New(rpcErr)
}
return nil
}
func headerPath() string {
return fmt.Sprintf("%s/%s", headerEndpoint, heightKey)
}
func namespacedPath(endpoint string, namespace Namespace, height uint64) string {
return fmt.Sprintf("%s/%s/height/%d", endpoint, hex.EncodeToString(namespace.Bytes()), height)
}