forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenexchangerates.go
119 lines (97 loc) · 3.22 KB
/
openexchangerates.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
// Copyright (c) of parts are held by the various contributors (see the CLA)
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
package polling
import (
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/pegnet/pegnet/common"
"github.com/cenkalti/backoff"
log "github.com/sirupsen/logrus"
"github.com/zpatrick/go-config"
)
// OpenExchangeRatesDataSource is the datasource at "https://openexchangerates.org/"
type OpenExchangeRatesDataSource struct {
config *config.Config
}
func NewOpenExchangeRatesDataSource(config *config.Config) (*OpenExchangeRatesDataSource, error) {
s := new(OpenExchangeRatesDataSource)
s.config = config
return s, nil
}
func (d *OpenExchangeRatesDataSource) Name() string {
return "OpenExchangeRates"
}
func (d *OpenExchangeRatesDataSource) Url() string {
return "https://openexchangerates.org/"
}
func (d *OpenExchangeRatesDataSource) SupportedPegs() []string {
return common.MergeLists(common.CurrencyAssets, common.CommodityAssets, []string{"XBT"})
}
func (d *OpenExchangeRatesDataSource) FetchPegPrices() (peg PegAssets, err error) {
resp, err := CallOpenExchangeRates(d.config)
if err != nil {
return nil, err
}
peg = make(map[string]PegItem)
timestamp := time.Unix(resp.Timestamp, 0)
for _, currencyISO := range d.SupportedPegs() {
// Price is inverted
if v, ok := resp.Rates[currencyISO]; ok {
peg[currencyISO] = PegItem{Value: 1 / v, When: timestamp, WhenUnix: timestamp.Unix()}
}
// Special case for btc
if currencyISO == "XBT" {
if v, ok := resp.Rates["BTC"]; ok {
peg[currencyISO] = PegItem{Value: 1 / v, When: timestamp, WhenUnix: timestamp.Unix()}
}
}
}
return
}
func (d *OpenExchangeRatesDataSource) FetchPegPrice(peg string) (i PegItem, err error) {
return FetchPegPrice(peg, d.FetchPegPrices)
}
// --
type OpenExchangeRatesResponse struct {
Disclaimer string `json:"disclaimer"`
License string `json:"license"`
Timestamp int64 `json:"timestamp"`
Base string `json:"base"`
Error bool `json:"error"`
Status int64 `json:"status"`
Message string `json:"message"`
Description string `json:"description"`
Rates map[string]float64 `json:"rates"`
}
func CallOpenExchangeRates(c *config.Config) (response OpenExchangeRatesResponse, err error) {
var OpenExchangeRatesResponse OpenExchangeRatesResponse
var apikey string
{
apikey, err = c.String("Oracle.OpenExchangeRatesKey")
check(err)
}
operation := func() error {
resp, err := http.Get("https://openexchangerates.org/api/latest.json?app_id=" + apikey)
if err != nil {
log.WithError(err).Warning("Failed to get response from OpenExchangeRates")
return err
}
defer resp.Body.Close()
if body, err := ioutil.ReadAll(resp.Body); err != nil {
return err
} else if err = json.Unmarshal(body, &OpenExchangeRatesResponse); err != nil {
return err
}
return nil
}
err = backoff.Retry(operation, PollingExponentialBackOff())
// Price is inverted
if err == nil {
for k, v := range OpenExchangeRatesResponse.Rates {
OpenExchangeRatesResponse.Rates[k] = v
}
}
return OpenExchangeRatesResponse, err
}