-
Notifications
You must be signed in to change notification settings - Fork 44
/
volume.go
145 lines (139 loc) · 2.99 KB
/
volume.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
package poloniex
import (
"encoding/json"
"strconv"
)
type Volume map[string]float64
type VolumeCollection struct {
TotalBNB float64 `json:"totalBNB,string"`
TotalBTC float64 `json:"totalBTC,string"`
TotalBUSD float64 `json:"totalBUSD,string"`
TotalDAI float64 `json:"totalDAI,string"`
TotalETH float64 `json:"totalETH,string"`
TotalPAX float64 `json:"totalPAX,string"`
TotalTRX float64 `json:"totalTRX,string"`
TotalTUSD float64 `json:"totalTUSD,string"`
TotalUSDC float64 `json:"totalUSDC,string"`
TotalUSDJ float64 `json:"totalUSDJ,string"`
TotalUSDT float64 `json:"totalUSDT,string"`
TotalXMR float64 `json:"totalXMR,string"`
TotalXUSD float64 `json:"totalXUSD,string"`
Volumes map[string]Volume
}
func (tc *VolumeCollection) UnmarshalJSON(b []byte) error {
m := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &m); err != nil {
return err
}
tc.Volumes = make(map[string]Volume)
for k, v := range m {
switch k {
case "totalBNB":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalBNB = f
case "totalBTC":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalBTC = f
case "totalBUSD":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalBUSD = f
case "totalDAI":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalDAI = f
case "totalETH":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalETH = f
case "totalPAX":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalPAX = f
case "totalTRX":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalTRX = f
case "totalTUSD":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalTUSD = f
case "totalUSDC":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalUSDC = f
case "totalUSDJ":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalUSDJ = f
case "totalUSDT":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalUSDT = f
case "totalXMR":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalXMR = f
case "totalXUSD":
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
tc.TotalXUSD = f
default:
t := make(Volume)
if err := json.Unmarshal(v, &t); err != nil {
return err
}
tc.Volumes[k] = t
}
}
return nil
}
func (t *Volume) UnmarshalJSON(b []byte) error {
m := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &m); err != nil {
return err
}
for k, v := range m {
f, err := parseJSONFloatString(v)
if err != nil {
return err
}
(*t)[k] = f
}
return nil
}
func parseJSONFloatString(b json.RawMessage) (float64, error) {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}