/
price.go
132 lines (123 loc) · 3.31 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
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
package price
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
type PriceWatcher struct {
client *http.Client
UpdateInterval time.Duration
Currencies map[string]string
Exchanges map[string]func(string) (float64, error)
}
var (
Price map[string]float64
P *PriceWatcher
)
func NewPriceWatcher() *PriceWatcher {
pricewatcher := &PriceWatcher{
client: &http.Client{
Timeout: time.Second * time.Duration(5),
},
// attention: $ must come after other $-denominated currencies like R$
Currencies: map[string]string{
"EUR": "€",
"GBP": "£",
"JPY": "¥",
"BRL": "R$",
"MXN": "MX$",
"USD": "$",
"RUB": "₽",
"TRY": "₺",
"INR": "₹",
},
Exchanges: make(map[string]func(string) (float64, error), 0),
UpdateInterval: time.Second * time.Duration(30),
}
pricewatcher.Exchanges["coinbase"] = pricewatcher.GetCoinbasePrice
pricewatcher.Exchanges["bitfinex"] = pricewatcher.GetBitfinexPrice
Price = make(map[string]float64, 0)
log.Infof("[PriceWatcher] Watcher started")
P = pricewatcher
return pricewatcher
}
func (p *PriceWatcher) Start() {
go p.Watch()
}
func (p *PriceWatcher) Watch() error {
for {
for currency, _ := range p.Currencies {
avg_price := 0.0
n_responses := 0
for _, getPrice := range p.Exchanges {
fprice, err := getPrice(currency)
if err != nil {
// log.Debug(err)
// if one exchanges is down, use the next
continue
}
n_responses++
avg_price += fprice
// log.Debugf("[PriceWatcher] %s %s price: %f", exchange, currency, fprice)
time.Sleep(time.Second * time.Duration(2))
}
Price[currency] = avg_price / float64(n_responses)
// log.Debugf("[PriceWatcher] Average %s price: %f", currency, Price[currency])
}
time.Sleep(p.UpdateInterval)
}
}
func (p *PriceWatcher) GetCoinbasePrice(currency string) (float64, error) {
coinbaseEndpoint, err := url.Parse(fmt.Sprintf("https://api.coinbase.com/v2/prices/spot?currency=%s", currency))
response, err := p.client.Get(coinbaseEndpoint.String())
if err != nil {
return 0, err
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Debug(err)
return 0, err
}
price := gjson.Get(string(bodyBytes), "data.amount")
if len(price.String()) > 0 {
fprice, err := strconv.ParseFloat(strings.TrimSpace(price.String()), 64)
if err != nil {
log.Debug(err)
return 0, err
}
return fprice, nil
} else {
return 0, fmt.Errorf("no price")
}
}
func (p *PriceWatcher) GetBitfinexPrice(currency string) (float64, error) {
var bitfinexCurrencyToPair = map[string]string{"USD": "btcusd", "EUR": "btceur", "GBP": "btcusd", "JPY": "btcjpy"}
pair := bitfinexCurrencyToPair[currency]
bitfinexEndpoint, err := url.Parse(fmt.Sprintf("https://api.bitfinex.com/v1/pubticker/%s", pair))
response, err := p.client.Get(bitfinexEndpoint.String())
if err != nil {
return 0, err
}
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Debug(err)
return 0, err
}
price := gjson.Get(string(bodyBytes), "last_price")
if len(price.String()) > 0 {
fprice, err := strconv.ParseFloat(strings.TrimSpace(price.String()), 64)
if err != nil {
log.Debug(err)
return 0, err
}
return fprice, nil
} else {
return 0, fmt.Errorf("no price")
}
}