-
Notifications
You must be signed in to change notification settings - Fork 3
/
exchange.go
282 lines (240 loc) Β· 11.2 KB
/
exchange.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
)
type Currency string
const (
USD Currency = "USD"
)
// Exchange is an interface for getting current and historical prices
// for a given coin and currency. Supports multiple exchanges.
type Exchange interface {
// CurrentPrice returns the current price of a coin
CurrentPrice(coin ChainType, currency Currency) (ExchangePrice, error)
// GetHistoricalPrice returns the price of a coin at a given time
HistoricalPrice(coin ChainType, currency Currency, received time.Time) (ExchangePrice, error)
}
type ExchangePrice struct {
Value float64 `json:"price,omitempty"`
Currency string `json:"currency,omitempty"`
Coin string `json:"coin,omitempty"`
Time time.Time `json:"time,omitempty"`
}
type CryptoCompareExchange struct {
BaseUrl string
ApiKey string
Version int
}
type CryptoCompareTickerResponse struct {
Data struct {
ETHUSD struct {
Type string `json:"TYPE"`
Market string `json:"MARKET"`
Instrument string `json:"INSTRUMENT"`
Ccseq int `json:"CCSEQ"`
Value float64 `json:"VALUE"`
ValueFlag string `json:"VALUE_FLAG"`
ValueLastUpdateTs int `json:"VALUE_LAST_UPDATE_TS"`
ValueLastUpdateNs int `json:"VALUE_LAST_UPDATE_NS"`
LastUpdateQuantity float64 `json:"LAST_UPDATE_QUANTITY"`
LastUpdateQuoteQuantity float64 `json:"LAST_UPDATE_QUOTE_QUANTITY"`
LastUpdateCcseq int `json:"LAST_UPDATE_CCSEQ"`
CurrentHourVolume float64 `json:"CURRENT_HOUR_VOLUME"`
CurrentHourQuoteVolume float64 `json:"CURRENT_HOUR_QUOTE_VOLUME"`
CurrentHourOpen float64 `json:"CURRENT_HOUR_OPEN"`
CurrentHourHigh float64 `json:"CURRENT_HOUR_HIGH"`
CurrentHourLow float64 `json:"CURRENT_HOUR_LOW"`
CurrentHourTotalIndexUpdates int `json:"CURRENT_HOUR_TOTAL_INDEX_UPDATES"`
CurrentHourChange float64 `json:"CURRENT_HOUR_CHANGE"`
CurrentHourChangePercentage float64 `json:"CURRENT_HOUR_CHANGE_PERCENTAGE"`
CurrentDayVolume float64 `json:"CURRENT_DAY_VOLUME"`
CurrentDayQuoteVolume float64 `json:"CURRENT_DAY_QUOTE_VOLUME"`
CurrentDayOpen float64 `json:"CURRENT_DAY_OPEN"`
CurrentDayHigh float64 `json:"CURRENT_DAY_HIGH"`
CurrentDayLow float64 `json:"CURRENT_DAY_LOW"`
CurrentDayTotalIndexUpdates int `json:"CURRENT_DAY_TOTAL_INDEX_UPDATES"`
CurrentDayChange float64 `json:"CURRENT_DAY_CHANGE"`
CurrentDayChangePercentage float64 `json:"CURRENT_DAY_CHANGE_PERCENTAGE"`
CurrentWeekVolume float64 `json:"CURRENT_WEEK_VOLUME"`
CurrentWeekQuoteVolume float64 `json:"CURRENT_WEEK_QUOTE_VOLUME"`
CurrentWeekOpen float64 `json:"CURRENT_WEEK_OPEN"`
CurrentWeekHigh float64 `json:"CURRENT_WEEK_HIGH"`
CurrentWeekLow float64 `json:"CURRENT_WEEK_LOW"`
CurrentWeekTotalIndexUpdates int `json:"CURRENT_WEEK_TOTAL_INDEX_UPDATES"`
CurrentWeekChange float64 `json:"CURRENT_WEEK_CHANGE"`
CurrentWeekChangePercentage float64 `json:"CURRENT_WEEK_CHANGE_PERCENTAGE"`
CurrentMonthVolume float64 `json:"CURRENT_MONTH_VOLUME"`
CurrentMonthQuoteVolume float64 `json:"CURRENT_MONTH_QUOTE_VOLUME"`
CurrentMonthOpen float64 `json:"CURRENT_MONTH_OPEN"`
CurrentMonthHigh float64 `json:"CURRENT_MONTH_HIGH"`
CurrentMonthLow float64 `json:"CURRENT_MONTH_LOW"`
CurrentMonthTotalIndexUpdates int `json:"CURRENT_MONTH_TOTAL_INDEX_UPDATES"`
CurrentMonthChange float64 `json:"CURRENT_MONTH_CHANGE"`
CurrentMonthChangePercentage float64 `json:"CURRENT_MONTH_CHANGE_PERCENTAGE"`
CurrentYearVolume float64 `json:"CURRENT_YEAR_VOLUME"`
CurrentYearQuoteVolume float64 `json:"CURRENT_YEAR_QUOTE_VOLUME"`
CurrentYearOpen float64 `json:"CURRENT_YEAR_OPEN"`
CurrentYearHigh float64 `json:"CURRENT_YEAR_HIGH"`
CurrentYearLow float64 `json:"CURRENT_YEAR_LOW"`
CurrentYearTotalIndexUpdates int `json:"CURRENT_YEAR_TOTAL_INDEX_UPDATES"`
CurrentYearChange float64 `json:"CURRENT_YEAR_CHANGE"`
CurrentYearChangePercentage float64 `json:"CURRENT_YEAR_CHANGE_PERCENTAGE"`
Moving24HourVolume float64 `json:"MOVING_24_HOUR_VOLUME"`
Moving24HourQuoteVolume float64 `json:"MOVING_24_HOUR_QUOTE_VOLUME"`
Moving24HourOpen float64 `json:"MOVING_24_HOUR_OPEN"`
Moving24HourHigh float64 `json:"MOVING_24_HOUR_HIGH"`
Moving24HourLow float64 `json:"MOVING_24_HOUR_LOW"`
Moving24HourTotalIndexUpdates int `json:"MOVING_24_HOUR_TOTAL_INDEX_UPDATES"`
Moving24HourChange float64 `json:"MOVING_24_HOUR_CHANGE"`
Moving24HourChangePercentage float64 `json:"MOVING_24_HOUR_CHANGE_PERCENTAGE"`
Moving7DayVolume float64 `json:"MOVING_7_DAY_VOLUME"`
Moving7DayQuoteVolume float64 `json:"MOVING_7_DAY_QUOTE_VOLUME"`
Moving7DayOpen float64 `json:"MOVING_7_DAY_OPEN"`
Moving7DayHigh float64 `json:"MOVING_7_DAY_HIGH"`
Moving7DayLow float64 `json:"MOVING_7_DAY_LOW"`
Moving7DayTotalIndexUpdates int `json:"MOVING_7_DAY_TOTAL_INDEX_UPDATES"`
Moving7DayChange float64 `json:"MOVING_7_DAY_CHANGE"`
Moving7DayChangePercentage float64 `json:"MOVING_7_DAY_CHANGE_PERCENTAGE"`
Moving30DayVolume float64 `json:"MOVING_30_DAY_VOLUME"`
Moving30DayQuoteVolume float64 `json:"MOVING_30_DAY_QUOTE_VOLUME"`
Moving30DayOpen float64 `json:"MOVING_30_DAY_OPEN"`
Moving30DayHigh float64 `json:"MOVING_30_DAY_HIGH"`
Moving30DayLow float64 `json:"MOVING_30_DAY_LOW"`
Moving30DayTotalIndexUpdates int `json:"MOVING_30_DAY_TOTAL_INDEX_UPDATES"`
Moving30DayChange float64 `json:"MOVING_30_DAY_CHANGE"`
Moving30DayChangePercentage float64 `json:"MOVING_30_DAY_CHANGE_PERCENTAGE"`
Moving90DayVolume float64 `json:"MOVING_90_DAY_VOLUME"`
Moving90DayQuoteVolume float64 `json:"MOVING_90_DAY_QUOTE_VOLUME"`
Moving90DayOpen float64 `json:"MOVING_90_DAY_OPEN"`
Moving90DayHigh float64 `json:"MOVING_90_DAY_HIGH"`
Moving90DayLow float64 `json:"MOVING_90_DAY_LOW"`
Moving90DayTotalIndexUpdates int `json:"MOVING_90_DAY_TOTAL_INDEX_UPDATES"`
Moving90DayChange float64 `json:"MOVING_90_DAY_CHANGE"`
Moving90DayChangePercentage float64 `json:"MOVING_90_DAY_CHANGE_PERCENTAGE"`
Moving180DayVolume float64 `json:"MOVING_180_DAY_VOLUME"`
Moving180DayQuoteVolume float64 `json:"MOVING_180_DAY_QUOTE_VOLUME"`
Moving180DayOpen float64 `json:"MOVING_180_DAY_OPEN"`
Moving180DayHigh float64 `json:"MOVING_180_DAY_HIGH"`
Moving180DayLow float64 `json:"MOVING_180_DAY_LOW"`
Moving180DayTotalIndexUpdates int `json:"MOVING_180_DAY_TOTAL_INDEX_UPDATES"`
Moving180DayChange float64 `json:"MOVING_180_DAY_CHANGE"`
Moving180DayChangePercentage float64 `json:"MOVING_180_DAY_CHANGE_PERCENTAGE"`
Moving365DayVolume float64 `json:"MOVING_365_DAY_VOLUME"`
Moving365DayQuoteVolume float64 `json:"MOVING_365_DAY_QUOTE_VOLUME"`
Moving365DayOpen float64 `json:"MOVING_365_DAY_OPEN"`
Moving365DayHigh float64 `json:"MOVING_365_DAY_HIGH"`
Moving365DayLow float64 `json:"MOVING_365_DAY_LOW"`
Moving365DayTotalIndexUpdates int `json:"MOVING_365_DAY_TOTAL_INDEX_UPDATES"`
Moving365DayChange float64 `json:"MOVING_365_DAY_CHANGE"`
Moving365DayChangePercentage float64 `json:"MOVING_365_DAY_CHANGE_PERCENTAGE"`
LifetimeFirstUpdateTs int `json:"LIFETIME_FIRST_UPDATE_TS"`
LifetimeVolume float64 `json:"LIFETIME_VOLUME"`
LifetimeQuoteVolume float64 `json:"LIFETIME_QUOTE_VOLUME"`
LifetimeOpen float64 `json:"LIFETIME_OPEN"`
LifetimeHigh float64 `json:"LIFETIME_HIGH"`
LifetimeHighTs int `json:"LIFETIME_HIGH_TS"`
LifetimeLow float64 `json:"LIFETIME_LOW"`
LifetimeLowTs int `json:"LIFETIME_LOW_TS"`
LifetimeTotalIndexUpdates int `json:"LIFETIME_TOTAL_INDEX_UPDATES"`
LifetimeChange float64 `json:"LIFETIME_CHANGE"`
LifetimeChangePercentage float64 `json:"LIFETIME_CHANGE_PERCENTAGE"`
} `json:"ETH-USD"`
} `json:"Data"`
Err struct {
} `json:"Err"`
}
func NewHttpClientWithTimeout(time time.Duration) (*http.Client, error) {
client := &http.Client{
Timeout: time,
}
return client, nil
}
func NewCryptoCompareExchange(apiKey string) (Exchange, error) {
if apiKey == "" {
return nil, errors.New("api key is required")
}
return CryptoCompareExchange{
ApiKey: apiKey,
BaseUrl: "https://data-api.cryptocompare.com", // v2 api
}, nil
}
func (c CryptoCompareExchange) CurrentPrice(coin ChainType, currency Currency) (ExchangePrice, error) {
price := ExchangePrice{
Coin: coin.Short(),
Currency: currency.String(),
Time: time.Now(),
}
httpClient, err := NewHttpClientWithTimeout(time.Duration(2 * time.Second))
if err != nil {
return price, err
}
path := "index/cc/v1/latest/tick"
url := fmt.Sprintf("%s/%s?market=ccix&instruments=%s-%s", c.BaseUrl, path, coin.Short(), currency.String())
req, err := httpClient.Get(url)
if err != nil {
return price, err
}
defer req.Body.Close()
res, err := io.ReadAll(req.Body)
if err != nil {
return price, err
}
var response CryptoCompareTickerResponse
err = json.Unmarshal(res, &response)
if err != nil {
return price, err
}
price.Value = response.Data.ETHUSD.CurrentDayOpen
return price, nil
}
func (c CryptoCompareExchange) HistoricalPrice(coin ChainType, currency Currency, received time.Time) (ExchangePrice, error) {
price := ExchangePrice{
Coin: coin.Short(),
Currency: currency.String(),
Time: received,
}
httpClient, err := NewHttpClientWithTimeout(time.Duration(2 * time.Second))
if err != nil {
return price, err
}
path := "index/cc/v1/historical/minutes"
limit := 20
fmt.Println(received)
// conver the time.Time to unix timestamp
unixTime := received.UTC().Unix()
url := fmt.Sprintf("%s/%s?market=ccix&instrument=%s-%s&limit=%d&to_ts=%d", c.BaseUrl, path, coin.Short(), currency.String(), limit, unixTime)
fmt.Println(unixTime)
fmt.Println(url)
req, err := httpClient.Get(url)
if err != nil {
return price, err
}
defer req.Body.Close()
res, err := io.ReadAll(req.Body)
if err != nil {
return price, err
}
var response CryptoCompareTickerResponse
err = json.Unmarshal(res, &response)
if err != nil {
return price, err
}
price.Value = response.Data.ETHUSD.CurrentDayOpen
return price, nil
}
func (c ChainType) Short() string {
switch c {
case Ethereum:
return "ETH"
default:
return ""
}
}
func (c Currency) String() string {
return string(c)
}