/
price.go
66 lines (55 loc) · 1.46 KB
/
price.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
package bitfinex
import (
"context"
"github.com/fox-one/pkg/logger"
"github.com/shopspring/decimal"
)
type (
Prices map[string]decimal.Decimal
)
func (b *bitfinexEx) getPrices(ctx context.Context) (Prices, error) {
log := logger.FromContext(ctx)
uri := "/tickers"
resp, err := Request(ctx).SetQueryParam("symbols", "ALL").Get(uri)
if err != nil {
log.WithError(err).Errorln("GET", uri)
return nil, err
}
var tickers [][]interface{}
if err := UnmarshalResponse(resp, &tickers); err != nil {
log.WithError(err).Errorln("getTicker.UnmarshalResponse")
return nil, err
}
prices := Prices{}
for _, d := range tickers {
if len(d) < 11 {
continue
}
if symbol, ok := d[0].(string); ok {
if price, ok := d[7].(float64); ok {
prices[symbol] = decimal.NewFromFloat(price)
}
}
}
return prices, nil
}
func (b *bitfinexEx) getPrice(ctx context.Context, symbol string) (decimal.Decimal, error) {
log := logger.FromContext(ctx)
uri := "/tickers"
resp, err := Request(ctx).SetQueryParam("symbols", symbol).Get(uri)
if err != nil {
log.WithError(err).Errorln("GET", uri)
return decimal.Zero, err
}
var tickers [][]interface{}
if err := UnmarshalResponse(resp, &tickers); err != nil {
log.WithError(err).Errorln("getTicker.UnmarshalResponse")
return decimal.Zero, err
}
if len(tickers) == 1 && len(tickers[0]) >= 11 {
if price, ok := tickers[0][7].(float64); ok {
return decimal.NewFromFloat(price), nil
}
}
return decimal.Zero, nil
}