-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrates.go
55 lines (46 loc) · 1.05 KB
/
rates.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
package ticker
import (
"encoding/json"
"strconv"
)
// exchangeRate represents the desired price data
type exchangeRate struct {
ID int64 `json:"id,omitempty"`
Ask json.Number `json:"ask"`
Bid json.Number `json:"bid"`
Last json.Number `json:"last"`
Type string `json:"type"`
}
// exchangeRates represents a map of symbols to rate data for that symbol
type exchangeRates map[string]exchangeRate
type rateFetcher interface {
fetch() (exchangeRates, error)
}
func mergeRates(allRates []exchangeRates) exchangeRates {
if len(allRates) == 0 {
return nil
}
base := allRates[0]
if len(allRates) == 1 {
return base
}
for _, rates := range allRates[1:] {
for k, v := range rates {
base[k] = v
}
}
return base
}
func invertAndFormatPrice(price json.Number) (json.Number, error) {
if price == "" {
return "", nil
}
priceAsFloat, err := price.Float64()
if err != nil {
return "", err
}
if priceAsFloat == 0 {
return json.Number("0"), nil
}
return json.Number(strconv.FormatFloat(1.0/priceAsFloat, 'f', -1, 32)), nil
}