forked from OpenBazaar/multiwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange_rates.go
165 lines (147 loc) · 3.87 KB
/
exchange_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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bitcoincash
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"sync"
"time"
"github.com/phoreproject/multiwallet/util"
"golang.org/x/net/proxy"
)
type ExchangeRateProvider struct {
fetchUrl string
cache map[string]float64
client *http.Client
decoder ExchangeRateDecoder
}
type ExchangeRateDecoder interface {
decode(dat interface{}, cache map[string]float64) (err error)
}
type OpenBazaarDecoder struct{}
type BitcoinCashPriceFetcher struct {
sync.Mutex
cache map[string]float64
providers []*ExchangeRateProvider
}
func NewBitcoinCashPriceFetcher(dialer proxy.Dialer) *BitcoinCashPriceFetcher {
b := BitcoinCashPriceFetcher{
cache: make(map[string]float64),
}
var client *http.Client
if dialer != nil {
dial := dialer.Dial
tbTransport := &http.Transport{Dial: dial}
client = &http.Client{Transport: tbTransport, Timeout: time.Minute}
} else {
client = &http.Client{Timeout: time.Minute}
}
b.providers = []*ExchangeRateProvider{
{"https://ticker.openbazaar.org/api", b.cache, client, OpenBazaarDecoder{}},
}
return &b
}
func (b *BitcoinCashPriceFetcher) GetExchangeRate(currencyCode string) (float64, error) {
b.Lock()
defer b.Unlock()
currencyCode = util.NormalizeCurrencyCode(currencyCode)
price, ok := b.cache[currencyCode]
if !ok {
return 0, errors.New("Currency not tracked")
}
return price, nil
}
func (b *BitcoinCashPriceFetcher) GetLatestRate(currencyCode string) (float64, error) {
b.fetchCurrentRates()
b.Lock()
defer b.Unlock()
currencyCode = util.NormalizeCurrencyCode(currencyCode)
price, ok := b.cache[currencyCode]
if !ok {
return 0, errors.New("Currency not tracked")
}
return price, nil
}
func (b *BitcoinCashPriceFetcher) GetAllRates(cacheOK bool) (map[string]float64, error) {
if !cacheOK {
err := b.fetchCurrentRates()
if err != nil {
return nil, err
}
}
b.Lock()
defer b.Unlock()
return b.cache, nil
}
func (b *BitcoinCashPriceFetcher) UnitsPerCoin() int64 {
return 100000000
}
func (b *BitcoinCashPriceFetcher) fetchCurrentRates() error {
b.Lock()
defer b.Unlock()
for _, provider := range b.providers {
err := provider.fetch()
if err == nil {
return nil
}
fmt.Println(err)
}
return errors.New("All exchange rate API queries failed")
}
func (b *BitcoinCashPriceFetcher) Run() {
b.fetchCurrentRates()
ticker := time.NewTicker(time.Minute * 15)
for range ticker.C {
b.fetchCurrentRates()
}
}
func (provider *ExchangeRateProvider) fetch() (err error) {
if len(provider.fetchUrl) == 0 {
err = errors.New("provider has no fetchUrl")
return err
}
resp, err := provider.client.Get(provider.fetchUrl)
if err != nil {
return err
}
decoder := json.NewDecoder(resp.Body)
var dataMap interface{}
err = decoder.Decode(&dataMap)
if err != nil {
return err
}
return provider.decoder.decode(dataMap, provider.cache)
}
func (b OpenBazaarDecoder) decode(dat interface{}, cache map[string]float64) (err error) {
data, ok := dat.(map[string]interface{})
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed")
}
bch, ok := data["BCH"]
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed, missing 'BCH' field")
}
val, ok := bch.(map[string]interface{})
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed")
}
bchRate, ok := val["last"].(float64)
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed, missing 'last' (float) field")
}
for k, v := range data {
if k != "timestamp" {
val, ok := v.(map[string]interface{})
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed")
}
price, ok := val["last"].(float64)
if !ok {
return errors.New(reflect.TypeOf(b).Name() + ".decode: Type assertion failed, missing 'last' (float) field")
}
cache[k] = price * (1 / bchRate)
}
}
return nil
}