-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.go
401 lines (369 loc) · 11 KB
/
miner.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package npapi
import (
"strconv"
"time"
)
type Time time.Time
func (t *Time) UnmarshalJSON(b []byte) error {
secs, err := strconv.ParseInt(string(b), 10, 64)
if err != nil {
return err
}
*t = Time(time.Unix(secs, 0))
return nil
}
// Payment is a nanopool.org payment.
type Payment struct {
// Payment date
Date Time
// Payment transaction hash
TxHash string
// Payment amount
Amount float64
// Payment status
Confirmed bool
}
// Worker is a nanopool.org worker. It represents one mining machine.
type Worker struct {
// Worker ID
ID string
// Worker Hashrate [MH/s]
Hashrate float64
// Last Share date of Worker
LastShare Time
// Worker Rating
Rating uint
// Average hashrates
AverageHashrates HashrateReport
}
// HashrateItem stores an association between a worker and an (averaged) hashrate.
type HashrateItem struct {
// Worker ID
ID string
// Worker Hashrate [MH/s]
Hashrate float64
}
// HashrateReport storing the (average) hashrates in the last one, six, three, twelve and twentyfour hours.
type HashrateReport struct {
LastHour, LastThreeHours, LastSixHours, LastTwelveHours, LastDay float64
}
// toHashrateReport parses a hashrate map to a well defined report.
func toHashrateReport(data map[string]float64) HashrateReport {
return HashrateReport{
LastHour: data["h1"],
LastThreeHours: data["h3"],
LastSixHours: data["h6"],
LastTwelveHours: data["h12"],
LastDay: data["h24"],
}
}
// WorkerHashrateReport is a nanopool worker report.
type WorkerHashrateReport struct {
LastHour, LastThreeHours, LastSixHours, LastTwelveHours, LastDay []HashrateItem
}
// User is a nanopool.org user identified by his address. A user can have multiple workers.
type User struct {
// Account address
Address string
// Account balance
Balance float64
// Account unconfirmed balance
UnconfirmedBalance float64
// Account hashrate [MH/s]
Hashrate float64
// Average hashrate [MH/s]
AverageHashrates HashrateReport
// Workers
Workers []Worker
}
// ChartItem stores hashrate metrics of a specific point in time.
type ChartItem struct {
// Date
Date Time
// Number of shares for last 10 minutes
Shares uint
// Miner reported hashrate [MH/s]
Hashrate float64
}
// HistoryItem stores hashrate history metrics.
type HistoryItem struct {
// Item date
Date Time
// Miner hashrate [MH/s]
Hashrate float64
}
// ShareItem stores share history metrics.
type ShareItem struct {
// Item date
Date Time
// Number of shares for last 10 minutes
Shares uint
}
// json balance struct
type jsonBalance struct {
Status bool `json:"status"`
Balance float64 `json:"data"`
}
// json worker hasrate
type jsonWorkerHashrate struct {
ID string `json:"worker"`
Hashrate float64 `json:"hashrate"`
}
// UserInfo retrieves a complete set of user information including workers and hashrate statistics.
func UserInfo(addr string) (*User, error) {
var user struct {
Balance string `json:"balance"`
UnconfirmedBalance string `json:"unconfirmed_balance"`
Hashrate string `json:"hashrate"`
AverageHashrates map[string]string `json:"avghashrate"`
Workers []struct {
ID string `json:"id"`
Hashrate string `json:"hashrate"`
LastShare Time `json:"lastShare"`
AvgOneHour string `json:"avg_h1"`
AvgThreeHours string `json:"avg_h3"`
AvgSixHours string `json:"avg_h6"`
AvgTwelveHours string `json:"avg_h12"`
AvgTwentyfourHours string `json:"avg_h24"`
} `json:"worker"`
}
if err := fetch(&user, userEndpoint, addr); err != nil {
return nil, err
}
workers := make([]Worker, len(user.Workers))
for i, w := range user.Workers {
averageHashratesMap, err := parseStringMapToFloat(map[string]string{
"h1": w.AvgOneHour,
"h3": w.AvgThreeHours,
"h6": w.AvgSixHours,
"h12": w.AvgTwelveHours,
"h24": w.AvgTwentyfourHours,
})
if err != nil {
return nil, err
}
currentHashrate, err := strconv.ParseFloat(w.Hashrate, 64)
if err != nil {
return nil, err
}
workers[i] = Worker{
ID: w.ID,
Hashrate: currentHashrate,
LastShare: w.LastShare,
AverageHashrates: toHashrateReport(averageHashratesMap),
}
}
averageHashratesMap, err := parseStringMapToFloat(user.AverageHashrates)
if err != nil {
return nil, err
}
balance, err := strconv.ParseFloat(user.Balance, 64)
if err != nil {
return nil, err
}
unconfirmedBalance, err := strconv.ParseFloat(user.UnconfirmedBalance, 64)
if err != nil {
return nil, err
}
currentHashrate, err := strconv.ParseFloat(user.Hashrate, 64)
if err != nil {
return nil, err
}
return &User{
Address: addr,
Balance: balance,
UnconfirmedBalance: unconfirmedBalance,
Hashrate: currentHashrate,
AverageHashrates: toHashrateReport(averageHashratesMap),
Workers: workers,
}, nil
}
// Balance retrieves the accounts balance.
func Balance(addr string) (float64, error) {
var balance float64
if err := fetch(&balance, accountBalanceEndpoint, addr); err != nil {
return balance, err
}
return balance, nil
}
// AverageHashrateIn retrieves the average hashrate in the last x hours.
func AverageHashrateIn(addr string, hours uint) (float64, error) {
var hashrate float64
if err := fetch(&hashrate, averageHashrateLimitedEndpoint, addr, hours); err != nil {
return hashrate, err
}
return hashrate, nil
}
// AverageHashrate retrieves the average hashrate in the last one to twentyfour hours.
func AverageHashrate(addr string) (HashrateReport, error) {
avgs := map[string]float64{}
if err := fetch(&avgs, averageHashrateEndpoint, addr); err != nil {
return HashrateReport{}, err
}
return toHashrateReport(avgs), nil
}
// HashrateChart retrieves the hashrate chart data.
func HashrateChart(addr string) ([]ChartItem, error) {
jsonItems := []struct {
Date Time `json:"date"`
Shares uint `json:"shares"`
Hashrate float64 `json:"hashrate"`
}{}
if err := fetch(&jsonItems, hashrateChartEndpoint, addr); err != nil {
return nil, err
}
items := make([]ChartItem, len(jsonItems))
for i := range jsonItems {
items[i] = ChartItem(jsonItems[i])
}
return items, nil
}
// Exists checks if the account exists.
func Exists(addr string) error {
var data string
if err := fetch(&data, accountExistEndpoint, addr); err != nil {
return err
}
return nil
}
// CurrentHashrate retrieves the current calculated hashrate.
func CurrentHashrate(addr string) (float64, error) {
var hashrate float64
if err := fetch(&hashrate, currentHashrateEndpoint, addr); err != nil {
return hashrate, err
}
return hashrate, nil
}
// HashrateHistory fetches the latest hashrate history.
func HashrateHistory(addr string) ([]HistoryItem, error) {
jsonHistory := []struct {
Date Time `json:"date"`
Hashrate float64 `json:"hashrate"`
}{}
if err := fetch(&jsonHistory, historyEndpoint, addr); err != nil {
return nil, err
}
history := make([]HistoryItem, len(jsonHistory))
for i := range jsonHistory {
history[i] = HistoryItem(jsonHistory[i])
}
return history, nil
}
// HashrateAndBalance retrieves the current hashrate and balance.
func HashrateAndBalance(addr string) (float64, float64, error) {
data := struct {
Hashrate float64 `json:"hashrate"`
Balance float64 `json:"balance"`
}{}
if err := fetch(&data, balanceHashrateEndpoint, addr); err != nil {
return data.Hashrate, data.Balance, err
}
return data.Hashrate, data.Balance, nil
}
// ReportedHashrate retrieves the last reported hashrate.
func ReportedHashrate(addr string) (float64, error) {
var hashrate float64
if err := fetch(&hashrate, reportedHashrateEndpoint, addr); err != nil {
return hashrate, err
}
return hashrate, nil
}
// Workers retrieves a list of workers bound to this account.
func Workers(addr string) ([]Worker, error) {
jsonWorkers := []struct {
ID string `json:"id"`
Hashrate float64 `json:"hashrate"`
LastShare Time `json:"lastShare"`
Rating uint `json:"rating"`
}{}
if err := fetch(&jsonWorkers, workersEndpoint, addr); err != nil {
return nil, err
}
workers := make([]Worker, len(jsonWorkers))
for i, w := range jsonWorkers {
workers[i] = Worker{
ID: w.ID,
Hashrate: w.Hashrate,
LastShare: w.LastShare,
Rating: w.Rating,
}
}
return workers, nil
}
// Payments retrieves a list of occured payments from nanopool to the user.
func Payments(addr string) ([]Payment, error) {
jsonPayments := []struct {
Date Time `json:"date"`
TxHash string `json:"txhash"`
Amount float64 `json:"amount"`
Confirmed bool `json:"confirmed"`
}{}
if err := fetch(&jsonPayments, paymentsEndpoint, addr); err != nil {
return nil, err
}
payments := make([]Payment, len(jsonPayments))
for i, p := range jsonPayments {
payments[i] = Payment(p)
}
return payments, nil
}
// ShareHistory retrieves a history of share rate metrics.
func ShareHistory(addr string) ([]ShareItem, error) {
jsonHistory := []struct {
Date Time `json:"date"`
Shares uint `json:"shares"`
}{}
if err := fetch(&jsonHistory, sharerateHistoryEndpoint, addr); err != nil {
return nil, err
}
history := make([]ShareItem, len(jsonHistory))
for i, s := range jsonHistory {
history[i] = ShareItem(s)
}
return history, nil
}
// WorkersAverageHashrateIn retrieves a list of workers, each associated with its hashrate in the given interval.
func WorkersAverageHashrateIn(addr string, interval uint) ([]HashrateItem, error) {
jsonWorkers := []jsonWorkerHashrate{}
if err := fetch(&jsonWorkers, workersAverageHashrateLimitedEndpoint, addr, interval); err != nil {
return nil, err
}
workers := make([]HashrateItem, len(jsonWorkers))
for i, w := range jsonWorkers {
workers[i] = HashrateItem(w)
}
return workers, nil
}
// WorkerAverageHashrate retrieves a list of workers, each associated with its hashrates.
func WorkersAverageHashrate(addr string) (WorkerHashrateReport, error) {
toHashrateItemList := func(jsonWorkers []jsonWorkerHashrate) []HashrateItem {
workers := make([]HashrateItem, len(jsonWorkers))
for i, w := range jsonWorkers {
workers[i] = HashrateItem(w)
}
return workers
}
jsonIntervals := map[string][]jsonWorkerHashrate{}
if err := fetch(&jsonIntervals, workersAverageHashrateEndpoint, addr); err != nil {
return WorkerHashrateReport{}, err
}
return WorkerHashrateReport{
LastHour: toHashrateItemList(jsonIntervals["h1"]),
LastThreeHours: toHashrateItemList(jsonIntervals["h3"]),
LastSixHours: toHashrateItemList(jsonIntervals["h6"]),
LastTwelveHours: toHashrateItemList(jsonIntervals["h12"]),
LastDay: toHashrateItemList(jsonIntervals["h24"]),
}, nil
}
// WorkersReportedHashrate retrieves the last reported hashrate associated with each worker.
func WorkersReportedHashrate(addr string) ([]HashrateItem, error) {
jsonWorkers := []jsonWorkerHashrate{}
if err := fetch(&jsonWorkers, workersReportedHashrateEndpoint, addr); err != nil {
return nil, err
}
workers := make([]HashrateItem, len(jsonWorkers))
for i, w := range jsonWorkers {
workers[i] = HashrateItem(w)
}
return workers, nil
}