forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1forge.go
172 lines (138 loc) · 3.6 KB
/
1forge.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
166
167
168
169
170
171
172
package polling
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/pegnet/pegnet/common"
"github.com/zpatrick/go-config"
)
// OneForgeDataSource is the datasource at https://1forge.com
type OneForgeDataSource struct {
config *config.Config
apikey string
}
func NewOneForgeDataSourceDataSource(config *config.Config) (*OneForgeDataSource, error) {
s := new(OneForgeDataSource)
s.config = config
var err error
// Load api key
s.apikey, err = s.config.String(common.Config1ForgeKey)
if err != nil {
return nil, err
}
return s, nil
}
func (d *OneForgeDataSource) Name() string {
return "1Forge"
}
func (d *OneForgeDataSource) Url() string {
return "https://1forge.com"
}
func (d *OneForgeDataSource) ApiUrl() string {
return "https://forex.1forge.com/1.0.3/"
}
func (d *OneForgeDataSource) SupportedPegs() []string {
// Does not have all the currencies, commodities, or crypto
return common.MergeLists([]string{"EUR", "JPY", "GBP", "CAD", "CHF", "SGD", "HKD", "MXN"}, []string{"XAU", "XAG"})
}
// AssetMapping changes some asset symbols to others to match 1forge
func (d *OneForgeDataSource) AssetMapping() map[string]string {
return map[string]string{
"XBT": "BTC",
"XBC": "BCH",
"DASH": "DSH",
}
}
func (d *OneForgeDataSource) FetchPegPrices() (peg PegAssets, err error) {
resp, err := d.Call1Forge()
if err != nil {
return nil, err
}
peg = make(map[string]PegItem)
respRates := make(map[string]OneForgeDataSourceRate)
for _, r := range resp {
respRates[r.Symbol] = r
}
mapping := d.AssetMapping()
// Look for each asset we support
for _, asset := range d.SupportedPegs() {
assetSym := asset
if v, ok := mapping[asset]; ok {
assetSym = v
}
index := fmt.Sprintf("%sUSD", assetSym)
currency, ok := respRates[index]
if !ok {
continue
}
timestamp := time.Unix(currency.Timestamp, 0)
peg[asset] = PegItem{Value: currency.Price, WhenUnix: timestamp.Unix(), When: timestamp}
}
return
}
func (d *OneForgeDataSource) FetchPegPrice(peg string) (i PegItem, err error) {
return FetchPegPrice(peg, d.FetchPegPrices)
}
func (d *OneForgeDataSource) Call1Forge() ([]OneForgeDataSourceRate, error) {
var resp []OneForgeDataSourceRate
operation := func() error {
data, err := d.FetchPeggedPrices()
if err != nil {
return err
}
resp, err = d.ParseFetchedPrices(data)
if err != nil {
// Try the other variation
return err
}
return nil
}
err := backoff.Retry(operation, PollingExponentialBackOff())
return resp, err
}
func (d *OneForgeDataSource) ParseFetchedPrices(data []byte) ([]OneForgeDataSourceRate, error) {
var resp []OneForgeDataSourceRate
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
return resp, nil
}
func (d *OneForgeDataSource) FetchPeggedPrices() ([]byte, error) {
client := NewHTTPClient()
req, err := http.NewRequest("GET", d.ApiUrl()+"quotes", nil)
if err != nil {
return nil, err
}
mapping := d.AssetMapping()
var ids []string
for _, asset := range d.SupportedPegs() {
assetSym := asset
if v, ok := mapping[asset]; ok {
assetSym = v
}
ids = append(ids, assetSym+"USD")
}
q := url.Values{}
q.Add("pairs", strings.Join(ids, ","))
q.Add("api_key", d.apikey)
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
type OneForgeDataSourceRate struct {
Symbol string `json:"symbol"`
Bid float64 `json:"bid"`
Ask float64 `json:"ask"`
Price float64 `json:"price"`
Timestamp int64 `json:"timestamp"`
}