-
Notifications
You must be signed in to change notification settings - Fork 0
/
other.go
73 lines (68 loc) · 2.01 KB
/
other.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
package npapi
// EarningsItem stores information about the projected earnings for a specified interval.
type EarningsItem struct {
// Earned coins in ETH
Coins float64
// Earned bitcoins
Bitcoins float64
// Earned USD
Dollars float64
// Earned CNY
Yuan float64
// Earned EUR
Euros float64
// Earned RUR
Rubles float64
}
// EarningsReport stores approximated earnings for the intervals minute, hour, day, week and month.
type EarningsReport struct {
PerMinute, PerHour, PerDay, PerWeek, PerMonth EarningsItem
}
// PriceReport stores the price information.
type PriceReport struct {
// ETH price in USD
USDollar float64
// ETH price in EUR
Euro float64
// ETH price in RUR
Rubles float64
// ETH price in CNY
Yuan float64
// ETH price in BTC
Bitcoins float64
}
// ApproximatedEarnings calculates the approximated earnings projected by the hashrate.
func ApproximatedEarnings(hashrate float64) (EarningsReport, error) {
jsonReport := map[string]struct {
Coins float64 `json:"coins"`
Bitcoins float64 `json:"bitcoins"`
Dollars float64 `json:"dollars"`
Yuan float64 `json:"yuan"`
Euros float64 `json:"euros"`
Rubles float64 `json:"rubles"`
}{}
if err := fetch(&jsonReport, approximatedEarningsEndpoint, hashrate); err != nil {
return EarningsReport{}, err
}
return EarningsReport{
PerMinute: EarningsItem(jsonReport["minute"]),
PerHour: EarningsItem(jsonReport["hour"]),
PerDay: EarningsItem(jsonReport["day"]),
PerWeek: EarningsItem(jsonReport["week"]),
PerMonth: EarningsItem(jsonReport["month"]),
}, nil
}
// Prices fetches a price report from the server, storing the current exchange rates for ETH.
func Prices() (PriceReport, error) {
jsonPrices := struct {
USDollar float64 `json:"price_usd"`
Euro float64 `json:"price_eur"`
Rubles float64 `json:"price_rur"`
Yuan float64 `json:"price_cny"`
Bitcoins float64 `json:"price_btc"`
}{}
if err := fetch(&jsonPrices, pricesEndpoint); err != nil {
return PriceReport{}, err
}
return PriceReport(jsonPrices), nil
}