-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGlobalData.go
78 lines (63 loc) · 2.11 KB
/
GlobalData.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
package coinmarketcap
import (
"encoding/json"
"strings"
"time"
)
// GlobalData will describe all cryptocurrencies known on coinmarketcap.
type GlobalData struct {
BitcoinPercentage float64 `json:"bitcoin_percentage_of_market_cap"`
ActiveCurrencies int `json:"active_currencies"`
ActiveAssets int `json:"active_assets"`
ActiveMarkets int `json:"active_markets"`
LastUpdated time.Time `json:"last_updated"`
volume24h priceTable
cap priceTable
}
// Volume24H returns the traded volume in the last 24 hours. A currency must
// be provided.
func (g *GlobalData) Volume24H(currency string) (float64, error) {
return g.volume24h.get(currency)
}
// MarketCap returns the market cap. A currency must be provided.
func (g *GlobalData) MarketCap(currency string) (float64, error) {
return g.cap.get(currency)
}
// UnmarshalJSON implements json.Unmarshaler. We have to implement this because
// the json from the coinmarketcap API is not easily parsable using Go's JSON
// unmarshaller..
func (g *GlobalData) UnmarshalJSON(b []byte) error {
var proxy struct {
BitcoinPercentage float64 `json:"bitcoin_percentage_of_market_cap"`
ActiveCurrencies int `json:"active_currencies"`
ActiveAssets int `json:"active_assets"`
ActiveMarkets int `json:"active_markets"`
LastUpdated int64 `json:"last_updated"`
}
err := json.Unmarshal(b, &proxy)
if err != nil {
return err
}
g.BitcoinPercentage = proxy.BitcoinPercentage
g.ActiveCurrencies = proxy.ActiveCurrencies
g.ActiveAssets = proxy.ActiveAssets
g.ActiveMarkets = proxy.ActiveMarkets
g.LastUpdated = time.Unix(proxy.LastUpdated, 0)
g.volume24h = make(priceTable)
g.cap = make(priceTable)
var kv map[string]json.RawMessage
// This should never fail.
json.Unmarshal(b, &kv)
for key, value := range kv {
switch {
case strings.HasPrefix(key, "total_24h_volume_"):
err = g.volume24h.add(key, string(value), "total_24h_volume_")
case strings.HasPrefix(key, "total_market_cap_"):
err = g.cap.add(key, string(value), "total_market_cap_")
}
if err != nil {
return err
}
}
return err
}