-
Notifications
You must be signed in to change notification settings - Fork 3
/
strategies.go
230 lines (223 loc) · 7.77 KB
/
strategies.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
package apiclient
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/vocdoni/census3/api"
"go.vocdoni.io/dvote/log"
)
func (c *HTTPclient) Strategies(pageSize int, nextCursor, prevCursor string) (
[]*api.Strategy, error,
) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetStrategiesURI, 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: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
strategiesResponse := &api.Strategies{}
if err := json.NewDecoder(res.Body).Decode(strategiesResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return strategiesResponse.Strategies, nil
}
func (c *HTTPclient) Strategy(strategyID uint64) (*api.Strategy, error) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetStrategyURI, strategyID)
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: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
strategyResponse := &api.Strategy{}
if err := json.NewDecoder(res.Body).Decode(strategyResponse); err != nil {
return nil, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return strategyResponse, nil
}
// HoldersByStrategy method queries the API for the holders of a strategy, it
// receives the strategyID and returns the queueID of the task and an error if
// something went wrong. The status of the task can be checked with the
// HoldersByStrategyQueue method.
func (c *HTTPclient) HoldersByStrategy(strategyID uint64) (string, error) {
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetTokenHoldersByStrategyURI, strategyID)
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.MethodGet, u, nil)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
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.StatusOK {
return "", fmt.Errorf("%w: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
holdersResponse := &api.QueueResponse{}
if err := json.NewDecoder(res.Body).Decode(holdersResponse); err != nil {
return "", fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
return holdersResponse.QueueID, nil
}
// HoldersByStrategyQueue method checks the status of the query for the holders
// of a strategy from the API, it receives the strategyID and the queueID and
// returns a map of addresses and amounts, a boolean indicating if the queue
// task is completed and an error if something went wrong.
func (c *HTTPclient) HoldersByStrategyQueue(strategyID uint64, queueID string) (
map[common.Address]*big.Int, bool, error,
) {
if strategyID == 0 {
return nil, false, fmt.Errorf("%w: strategyID is required", ErrBadInputs)
}
if queueID == "" {
return nil, false, fmt.Errorf("%w: queueID is required", ErrBadInputs)
}
// construct the URL to the API with the given parameters
endpoint := fmt.Sprintf(GetTokenHoldersByStrategyQueueURI, strategyID, queueID)
u, err := c.constructURL(endpoint)
if err != nil {
return nil, false, 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, false, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
res, err := c.c.Do(req)
if err != nil {
return nil, false, 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, false, fmt.Errorf("%w: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
// decode the queue response
item := map[string]interface{}{}
if err := json.NewDecoder(res.Body).Decode(&item); err != nil {
return nil, false, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
// check if the item is done and if there is an error
if done, ok := item["done"].(bool); !ok || !done {
return nil, false, nil
}
if strErr, ok := item["error"].(string); ok && strErr != "" {
return nil, true, fmt.Errorf("error in queue item: %s", strErr)
}
// convert the data to a map of addresses and amounts
rawHolders, ok := item["data"].(map[string]interface{})
if !ok {
return nil, true, fmt.Errorf("error getting data from queue item")
}
holders := make(map[common.Address]*big.Int, len(rawHolders))
for k, v := range rawHolders {
strBalance, ok := v.(string)
if !ok {
continue
}
addr := common.HexToAddress(k)
amount := new(big.Int)
if _, ok := amount.SetString(strBalance, 10); !ok {
return nil, true, fmt.Errorf("error converting amount to big.Int")
}
holders[addr] = amount
}
return holders, true, nil
}
func (c *HTTPclient) CreateStrategy(request *api.Strategy) (uint64, error) {
// construct the URL to the API
u, err := c.constructURL(CreateStrategyURI)
if err != nil {
return 0, 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
body, err := json.Marshal(request)
if err != nil {
return 0, fmt.Errorf("%w: %w", ErrEncodingRequest, err)
}
req, err := http.NewRequest(http.MethodPost, u, bytes.NewBuffer(body))
if err != nil {
return 0, fmt.Errorf("%w: %w", ErrCreatingRequest, err)
}
req.Header.Set("Content-Type", "application/json")
res, err := c.c.Do(req)
if err != nil {
return 0, 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.StatusAccepted {
return 0, fmt.Errorf("%w: %s", ErrNoStatusOk,
fmt.Errorf("%d %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
responseStrategyID := map[string]uint64{}
if err := json.NewDecoder(res.Body).Decode(&responseStrategyID); err != nil {
return 0, fmt.Errorf("%w: %w", ErrDecodingResponse, err)
}
strategyID, ok := responseStrategyID["strategyID"]
if !ok {
return 0, fmt.Errorf("error getting strategyID from response")
}
return strategyID, nil
}