-
-
Notifications
You must be signed in to change notification settings - Fork 277
/
types.go
127 lines (102 loc) · 1.97 KB
/
types.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
package glassnodeapi
import (
"encoding/json"
"time"
)
type Interval string
const (
Interval1h Interval = "1h"
Interval24h Interval = "24h"
Interval10m Interval = "10m"
Interval1w Interval = "1w"
Interval1m Interval = "1month"
)
type Format string
const (
FormatJSON Format = "JSON"
FormatCSV Format = "CSV"
)
type Timestamp time.Time
func (t Timestamp) Unix() float64 {
return float64(time.Time(t).Unix())
}
func (t Timestamp) String() string {
return time.Time(t).String()
}
func (t *Timestamp) UnmarshalJSON(o []byte) error {
var timestamp int64
if err := json.Unmarshal(o, ×tamp); err != nil {
return err
}
*t = Timestamp(time.Unix(timestamp, 0))
return nil
}
/*
In Glassnode API, there are two types of response, for example:
/v1/metrics/market/marketcap_usd
[
{
"t": 1614556800,
"v": 927789865185.0476
},
...
]
and
/v1/metrics/market/price_usd_ohlc
[
{
"t": 1614556800,
"o": {
"c": 49768.16035012147,
"h": 49773.18922304233,
"l": 45159.50305252744,
"o": 45159.50305252744
}
},
...
]
both can be stored into the Response structure.
Note: use `HasOptions` to verify the type of response.
*/
type Response []Data
type Data struct {
Timestamp Timestamp `json:"t"`
Value float64 `json:"v"`
Options map[string]float64 `json:"o"`
}
func (s Response) IsEmpty() bool {
if len(s) == 0 {
return true
}
return false
}
func (s Response) First() Data {
if s.IsEmpty() {
return Data{}
}
return s[0]
}
func (s Response) FirstValue() float64 {
return s.First().Value
}
func (s Response) FirstOptions() map[string]float64 {
return s.First().Options
}
func (s Response) Last() Data {
if s.IsEmpty() {
return Data{}
}
return s[len(s)-1]
}
func (s Response) LastValue() float64 {
return s.Last().Value
}
func (s Response) LastOptions() map[string]float64 {
return s.Last().Options
}
func (s Response) HasOptions() bool {
if len(s.First().Options) == 0 {
return false
}
return true
}