forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalternative.go
189 lines (157 loc) · 4.92 KB
/
alternative.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package polling
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/cenkalti/backoff"
"github.com/pegnet/pegnet/common"
"github.com/zpatrick/go-config"
)
// AlternativeMeDataSource is the datasource at https://alternative.me/crypto/api/
type AlternativeMeDataSource struct {
config *config.Config
}
func NewAlternativeMeDataSource(config *config.Config) (*AlternativeMeDataSource, error) {
s := new(AlternativeMeDataSource)
s.config = config
return s, nil
}
func (d *AlternativeMeDataSource) Name() string {
return "AlternativeMe"
}
func (d *AlternativeMeDataSource) Url() string {
return "https://alternative.me/crypto/api/"
}
func (d *AlternativeMeDataSource) ApiUrl() string {
return "https://api.alternative.me/v2/"
}
func (d *AlternativeMeDataSource) SupportedPegs() []string {
// Does not have all the currencies, commodities, or crypto
return common.MergeLists(common.CryptoAssets)
}
// AssetMapping changes some asset symbols to others to match 1forge
func (d *AlternativeMeDataSource) AssetMapping() map[string]int {
return map[string]int{
"XBT": 1,
"ETH": 1027,
"LTC": 2,
"RVN": 2577,
"XBC": 1831,
"FCT": 1087,
"BNB": 1839,
"XLM": 512,
"ADA": 2010,
"XMR": 328,
"DASH": 131,
"ZEC": 1437,
"DCR": 1168,
}
}
func (d *AlternativeMeDataSource) FetchPegPrices() (peg PegAssets, err error) {
resp, err := d.CallAlternativeMe()
if err != nil {
return nil, err
}
peg = make(map[string]PegItem)
mapping := d.AssetMapping()
// Look for each asset we support
for _, asset := range d.SupportedPegs() {
id := mapping[asset]
index := fmt.Sprintf("%d", id)
currency, ok := resp.Data[index]
if !ok {
continue
}
// Find us quote
usdQuote, ok := currency.Quotes["USD"]
if !ok {
continue
}
timestamp := time.Unix(currency.LastUpdated, 0)
peg[asset] = PegItem{Value: usdQuote.Price, WhenUnix: timestamp.Unix(), When: timestamp}
}
return
}
func (d *AlternativeMeDataSource) FetchPegPrice(peg string) (i PegItem, err error) {
return FetchPegPrice(peg, d.FetchPegPrices)
}
func (d *AlternativeMeDataSource) CallAlternativeMe() (*AlternativeMeDataSourceResponse, error) {
var resp *AlternativeMeDataSourceResponse
operation := func() error {
data, err := d.FetchPeggedPrices()
if err != nil {
return err
}
resp, err = d.ParseFetchedPrices(data)
if err != nil {
return err
}
return nil
}
err := backoff.Retry(operation, PollingExponentialBackOff())
return resp, err
}
func (d *AlternativeMeDataSource) ParseFetchedPrices(data []byte) (*AlternativeMeDataSourceResponse, error) {
var resp AlternativeMeDataSourceResponse
err := json.Unmarshal(data, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func (d *AlternativeMeDataSource) FetchPeggedPrices() ([]byte, error) {
client := NewHTTPClient()
req, err := http.NewRequest("GET", d.ApiUrl()+"ticker", nil)
if err != nil {
return nil, err
}
mapping := d.AssetMapping()
var ids []string
for _, asset := range d.SupportedPegs() {
ids = append(ids, fmt.Sprintf("%d", mapping[asset]))
}
q := url.Values{}
//q.Add("id", strings.Join(ids, ","))
q.Add("convert", "USD") // We want usd prices
q.Add("limit", "300")
req.Header.Set("Accepts", "application/json")
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 AlternativeMeDataSourceResponse struct {
Data map[string]AlternativeMeDataSourceRate `json:"data"`
Metadata AlternativeMeDataSourceMetadata `json:"metadata"`
}
type AlternativeMeDataSourceMetadata struct {
Timestamp int `json:"timestamp"`
NumCryptocurrencies int `json:"num_cryptocurrencies"`
Error interface{} `json:"error"`
}
type AlternativeMeDataSourceRate struct {
ID int `json:"id"`
Name string `json:"name"`
Symbol string `json:"symbol"`
WebsiteSlug string `json:"website_slug"`
Rank int `json:"rank"`
CirculatingSupply float64 `json:"circulating_supply"`
TotalSupply float64 `json:"total_supply"`
MaxSupply float64 `json:"max_supply"`
Quotes map[string]AlternativeMeDataSourceQuote `json:"quotes"`
LastUpdated int64 `json:"last_updated"`
}
type AlternativeMeDataSourceQuote struct {
Price float64 `json:"price"`
Volume24H float64 `json:"volume_24h"`
MarketCap float64 `json:"market_cap"`
PercentageChange1H float64 `json:"percentage_change_1h"`
PercentageChange24H float64 `json:"percentage_change_24h"`
PercentageChange7D float64 `json:"percentage_change_7d"`
}