This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
320 lines (278 loc) · 8.2 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"sort"
"time"
log "github.com/sirupsen/logrus"
)
// FlexpoolAPIURL constant to store Flexpool API URL
const FlexpoolAPIURL = "https://api.flexpool.io/v2"
// MaxIterations to avoid infinite loop while requesting paged routes on Flexpool API
const MaxIterations = 10
// UserAgent to identify ourselves on the Flexpool API
var UserAgent = fmt.Sprintf("flexassistant/%s", AppVersion)
// FlexpoolClient to store the HTTP client
type FlexpoolClient struct {
client *http.Client
}
// NewFlexpoolClient to create a client to manage Flexpool API calls
func NewFlexpoolClient() *FlexpoolClient {
return &FlexpoolClient{
client: &http.Client{Timeout: time.Second * 3},
}
}
// request to create an HTTPS request, call the Flexpool API, detect errors and return the result in bytes
func (f *FlexpoolClient) request(url string) ([]byte, error) {
log.Debugf("Requesting %s", url)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
request.Header.Set("User-Agent", UserAgent)
resp, err := f.client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
jsonBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
json.Unmarshal(jsonBody, &result)
if result["error"] != nil {
return nil, fmt.Errorf("Flexpool API error: %s", result["error"].(string))
}
return jsonBody, nil
}
// BalanceResponse represents the JSON structure of the Flexpool API response for balance
type BalanceResponse struct {
Error string `json:"error"`
Result struct {
Balance float64 `json:"balance"`
} `json:"result"`
}
// MinerBalance returns the current unpaid balance
func (f *FlexpoolClient) MinerBalance(coin string, address string) (float64, error) {
body, err := f.request(fmt.Sprintf("%s/miner/balance?coin=%s&address=%s", FlexpoolAPIURL, coin, address))
if err != nil {
return 0, err
}
var response BalanceResponse
json.Unmarshal(body, &response)
return response.Result.Balance, nil
}
// PaymentsResponse represents the JSON structure of the Flexpool API response for payments
type PaymentsResponse struct {
Error string `json:"error"`
Result struct {
TotalPages int `json:"totalPages"`
Data []struct {
Hash string `json:"hash"`
Value float64 `json:"value"`
Timestamp int64 `json:"timestamp"`
} `json:"data"`
} `json:"result"`
}
// MinerPayments returns an ordered list of payments
func (f *FlexpoolClient) MinerPayments(coin string, address string, limit int) (payments []*Payment, err error) {
page := 0
totalPages := 0
for page <= MaxIterations && len(payments) < limit {
body, err := f.request(fmt.Sprintf("%s/miner/payments/?coin=%s&address=%s&page=%d", FlexpoolAPIURL, coin, address, page))
if err != nil {
return nil, err
}
var response PaymentsResponse
json.Unmarshal(body, &response)
if totalPages == 0 {
totalPages = response.Result.TotalPages
}
for _, result := range response.Result.Data {
payment := NewPayment(
result.Hash,
result.Value,
result.Timestamp,
)
payments = append(payments, payment)
if len(payments) >= limit {
break
}
}
page++
if page >= totalPages {
break
}
}
if page > MaxIterations {
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
}
// Sort by timestamp
sort.Slice(payments, func(p1, p2 int) bool {
return payments[p1].Timestamp > payments[p2].Timestamp
})
return payments, nil
}
// LastMinerPayment return the last payment of a miner
func (f *FlexpoolClient) LastMinerPayment(miner *Miner) (*Payment, error) {
log.Debugf("Fetching last payment of %s", miner)
payments, err := f.MinerPayments(miner.Coin, miner.Address, 1)
if err != nil {
return nil, err
}
return payments[0], nil
}
// WorkersResponse represents the JSON structure of the Flexpool API response for workers
type WorkersResponse struct {
Error string `json:"error"`
Result []struct {
Name string `json:"name"`
IsOnline bool `json:"isOnline"`
LastSteen int64 `json:"lastSeen"`
} `json:"result"`
}
// MinerWorkers returns a list of workers given a miner address
func (f *FlexpoolClient) MinerWorkers(coin string, address string) (workers []*Worker, err error) {
body, err := f.request(fmt.Sprintf("%s/miner/workers?coin=%s&address=%s", FlexpoolAPIURL, coin, address))
if err != nil {
return nil, err
}
var response WorkersResponse
json.Unmarshal(body, &response)
for _, result := range response.Result {
worker := NewWorker(
address,
result.Name,
result.IsOnline,
time.Unix(result.LastSteen, 0),
)
workers = append(workers, worker)
}
return workers, nil
}
// BlocksResponse represents the JSON structure of the Flexpool API response for blocks
type BlocksResponse struct {
Error string `json:"error"`
Result struct {
TotalPages int `json:"totalPages"`
Data []struct {
Hash string `json:"hash"`
Number uint64 `json:"number"`
Reward float64 `json:"reward"`
} `json:"data"`
} `json:"result"`
}
// PoolBlocks returns an ordered list of blocks
func (f *FlexpoolClient) PoolBlocks(coin string, limit int) (blocks []*Block, err error) {
page := 0
totalPages := 0
for page <= MaxIterations && len(blocks) < limit {
body, err := f.request(fmt.Sprintf("%s/pool/blocks/?coin=%s&page=%d", FlexpoolAPIURL, coin, page))
if err != nil {
return nil, err
}
var response BlocksResponse
json.Unmarshal(body, &response)
if totalPages == 0 {
totalPages = response.Result.TotalPages
}
for _, result := range response.Result.Data {
block := NewBlock(
result.Hash,
result.Number,
result.Reward,
)
blocks = append(blocks, block)
if len(blocks) >= limit {
break
}
}
page++
if page >= totalPages {
break
}
}
if page > MaxIterations {
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
}
// Sort by number
sort.Slice(blocks, func(b1, b2 int) bool {
return blocks[b1].Number < blocks[b2].Number
})
return blocks, nil
}
// LastPoolBlock return the last discovered block for a given pool
func (f *FlexpoolClient) LastPoolBlock(pool *Pool) (*Block, error) {
blocks, err := f.PoolBlocks(pool.Coin, 1)
if err != nil {
return nil, err
}
return blocks[0], nil
}
// CoinsResponse represents the JSON structure of the Flexpool API response for pool coins
type CoinsResponse struct {
Error string `json:"error"`
Result struct {
Coins []struct {
Ticker string `json:"ticker"`
Name string `json:"name"`
} `json:"coins"`
} `json:"result"`
}
// RandomPool returns a random pool from the API
func (f *FlexpoolClient) RandomPool() (*Pool, error) {
log.Debug("Fetching a random pool")
body, err := f.request(fmt.Sprintf("%s/pool/coins", FlexpoolAPIURL))
if err != nil {
return nil, err
}
var response CoinsResponse
json.Unmarshal(body, &response)
randomIndex := rand.Intn(len(response.Result.Coins))
if err != nil {
return nil, err
}
randomCoin := response.Result.Coins[randomIndex]
return NewPool(randomCoin.Ticker), nil
}
// TopMinersResponse represents the JSON structure of the Flexpool API response for pool top miners
type TopMinersResponse struct {
Error string `json:"error"`
Result []struct {
Address string `json:"address"`
} `json:"result"`
}
// RandomMiner returns a random miner from the API
func (f *FlexpoolClient) RandomMiner(pool *Pool) (*Miner, error) {
log.Debug("Fetching a random miner")
body, err := f.request(fmt.Sprintf("%s/pool/topMiners?coin=%s", FlexpoolAPIURL, pool.Coin))
if err != nil {
return nil, err
}
var response TopMinersResponse
json.Unmarshal(body, &response)
randomResult := response.Result[rand.Intn(len(response.Result))]
randomMiner, err := NewMiner(randomResult.Address, pool.Coin)
if err != nil {
return nil, err
}
randomBalance, err := f.MinerBalance(pool.Coin, randomMiner.Address)
if err != nil {
return nil, err
}
randomMiner.Balance = randomBalance
return randomMiner, nil
}
// RandomWorker returns a random worker from the API
func (f *FlexpoolClient) RandomWorker(miner *Miner) (*Worker, error) {
log.Debug("Fetching a random worker")
workers, err := f.MinerWorkers(miner.Coin, miner.Address)
if err != nil {
return nil, err
}
return workers[rand.Intn(len(workers))], nil
}