-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokens.go
294 lines (285 loc) · 11 KB
/
tokens.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
package apiclient
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"net/http"
"github.com/vocdoni/census3/api"
"go.vocdoni.io/dvote/log"
)
// Tokens method returns a list of tokens from the API, it accepts a
// pageSize, nextCursor and prevCursor. If the pageSize is -1 and cursors are
// empty, it will return all the tokens. If something goes wrong, it will return
// an error.
func (c *HTTPclient) Tokens(pageSize int, nextCursor, prevCursor string) ([]*api.TokenListItem, error) {
// construct the URL to the API with the pageSize, nextCursor and prevCursor
endpoint := fmt.Sprintf(GetTokensURI, pageSize, nextCursor, prevCursor)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the tokens
tokensResponse := &api.TokenList{}
if err := json.NewDecoder(res.Body).Decode(&tokensResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return tokensResponse.Tokens, nil
}
// Token method returns a token from the API, it accepts the tokenID, chainID
// and externalID. If something goes wrong, it will return an error.
func (c *HTTPclient) Token(tokenID string, chainID uint64, externalID string) (*api.Token, error) {
if tokenID == "" || chainID == 0 {
return nil, fmt.Errorf("%w: tokenID and chainID are required", ErrBadInputs)
}
// construct the URL to the API with the tokenID, chainID and externalID
// provided
endpoint := fmt.Sprintf(GetAndDeleteTokenURI, tokenID, chainID, externalID)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the token
tokenResponse := &api.Token{}
if err := json.NewDecoder(res.Body).Decode(&tokenResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return tokenResponse, nil
}
// CreateToken method creates a token in the API, it accepts a token to be
// created. The minimun required token attributes are: ID, type and chainID.
// Optional attributes: externalID, tags. If something goes wrong, it will
// return an error.
func (c *HTTPclient) CreateToken(token *api.Token) error {
if token == nil || token.ID == "" || token.Type == "" || token.ChainID == 0 {
return fmt.Errorf("%w: ID, Type and ChainID are required", ErrBadInputs)
}
// construct the URL to the API
u, err := c.constructURL(CreateTokensURI)
if err != nil {
return fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// encode the input token to JSON to be sent in the request body
reqBody, err := json.Marshal(token)
if err != nil {
return fmt.Errorf("%w: %w", ErrEncodingRequest, err)
}
// create the request and send it with the encoded body, if there is an
// error or the status code is not 201, return an error
req, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(reqBody))
if err != nil {
return fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
req.Header.Set("Content-Type", "application/json")
res, err := c.c.Do(req)
if err != nil {
return fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusCreated {
return fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
return nil
}
// DeleteToken method deletes a token in the API, it accepts the tokenID,
// chainID and externalID. If something goes wrong, it will return an error. It
// returns the queueID, that can be used to check the status of the deletion
// process using the DeleteTokenQueue method.
func (c *HTTPclient) DeleteToken(tokenID string, chainID uint64, externalID string) (string, error) {
if tokenID == "" || chainID == 0 {
return "", fmt.Errorf("%w: tokenID and chainID are required", ErrBadInputs)
}
// construct the URL to the API with the tokenID, chainID and externalID
// provided
endpoint := fmt.Sprintf(GetAndDeleteTokenURI, tokenID, chainID, externalID)
u, err := c.constructURL(endpoint)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return "", fmt.Errorf("error making DELETE request: %v", err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return "", fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the queue response
queueResponse := &api.QueueResponse{}
if err := json.NewDecoder(res.Body).Decode(&queueResponse); err != nil {
return "", fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return queueResponse.QueueID, nil
}
// DeleteTokenQueue method checks the status of the deletion process of a token
// in the API, it accepts the tokenID, queueID, chainID and externalID. If
// something goes wrong, it will return an error. It returns the delete queue
// response.
func (c *HTTPclient) DeleteTokenQueue(tokenID string, chainID uint64, externalID,
queueID string,
) (*api.DeleteTokenQueueResponse, error) {
if tokenID == "" || chainID == 0 || queueID == "" {
return nil, fmt.Errorf("%w: tokenID, chainID and queueID are required", ErrBadInputs)
}
// construct the URL to the API with the tokenID, queueID, chainID and
// externalID provided
endpoint := fmt.Sprintf(DeleteTokenQueueURI, tokenID, queueID, chainID, externalID)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("error making DELETE request: %v", err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the queue response
queueResponse := &api.DeleteTokenQueueResponse{}
if err := json.NewDecoder(res.Body).Decode(&queueResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return queueResponse, nil
}
// TokenHolder method returns the balance of a token holder from the API, it
// accepts the tokenID, chainID, externalID and holderID. If something goes
// wrong, it will return an error.
func (c *HTTPclient) TokenHolder(tokenID string, chainID uint64, externalID, holderID string) (*big.Int, error) {
if tokenID == "" || chainID == 0 || holderID == "" {
return nil, fmt.Errorf("%w: tokenID, chainID and holderID are required", ErrBadInputs)
}
// construct the URL to the API with the tokenID, holderID, chainID and
// externalID provided
endpoint := fmt.Sprintf(GetTokenHolderURI, tokenID, holderID, chainID, externalID)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the token holder response
holderResponse := &api.TokenHolderBalance{}
if err := json.NewDecoder(res.Body).Decode(&holderResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
bBalance, ok := new(big.Int).SetString(holderResponse.Balance, 10)
if !ok {
return nil, fmt.Errorf("error parsing balance: %v", holderResponse.Balance)
}
return bBalance, nil
}
// TokenTypes method returns the supported token types from the API. If
// something goes wrong, it will return an error. It returns the supported token
// types as a slice of strings.
func (c *HTTPclient) TokenTypes() ([]string, error) {
// construct the URL to the API
u, err := c.constructURL(GetTokenTypes)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrConstructingURL, err)
}
// create the request and send it, if there is an error or the status code
// is not 200, return an error
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrMakingRequest, err)
}
defer func() {
if err := res.Body.Close(); err != nil {
log.Errorf("error closing response body: %v", err)
}
}()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%w: %w", ErrNoStatusOk, fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the response and return the token types
tokenTypesResponse := &api.TokenTypes{}
if err := json.NewDecoder(res.Body).Decode(&tokenTypesResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return tokenTypesResponse.SupportedTypes, nil
}