-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmcprice.go
426 lines (409 loc) · 12.8 KB
/
cmcprice.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"encoding/json"
"fmt"
"github.com/elastos/Elastos.ORG.API.Misc/config"
"github.com/elastos/Elastos.ORG.API.Misc/db"
"github.com/pkg/errors"
"io/ioutil"
"math"
"net/http"
"os"
"strconv"
"time"
)
const (
CMC_ENDPOINT_URL = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?limit=%d&convert=%s"
BGX_ENDPOINT_URL = "https://www.gaex.com/svc/portal/api/v2/publicinfo"
HBG_ENDPOINT_URL = "https://api.huobi.pro/market/history/trade?symbol=elabtc"
)
type Status struct {
Timestamp string
Error_code int
Error_message string
Elapsed int
Credit_count int
}
type Price struct {
Price float64
Volume_24h float64
Percent_change_1h float64
Percent_change_24h float64
Percent_change_7d float64
Market_cap float64
Last_updated string
}
type Quote struct {
USD, CNY, BTC Price
}
type Plateform struct {
Id int64
Name string
Symbol string
Slug string
Token_Address string
}
type Data struct {
Id int64
Name string
Symbol string
Slug string
Circulating_supply float64
Total_supply float64
Max_supply float64
Date_added string
Num_market_pairs int64
Tags []string
Platform Plateform
Cmc_rank int
Last_updated string
Quote Quote
}
type CmcResponse struct {
Status Status
Data []Data
}
var dba *db.Dialect
var dbaforela *db.Dialect
func init() {
if config.Conf.Cmc.Enable {
dba = db.NewInstance()
dbaforela = db.NewInstance()
go func() {
i := -1
sleepy, err := time.ParseDuration(config.Conf.Cmc.Inteval)
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(-1)
}
for {
if i == len(config.Conf.Cmc.ApiKey)-1 {
i = 0
} else {
i++
}
cmcResponseUSD, err := fetchPrice(i, "USD")
if err != nil {
fmt.Printf("Error in cmc price %s\n", err.Error())
<-time.After(sleepy)
continue
}
cmcResponseCNY, err := fetchPrice(i, "CNY")
if err != nil {
fmt.Printf("Error in cmc price %s\n", err.Error())
<-time.After(sleepy)
continue
}
cmcResponseBTC, err := fetchPrice(i, "BTC")
if err != nil {
fmt.Printf("Error in cmc price %s\n", err.Error())
<-time.After(sleepy)
continue
}
cmcResponseBGX, err := fetchBGXPrice()
if err != nil {
fmt.Printf("Error in bgx price %s\n", err.Error())
}
err = saveToDb(cmcResponseUSD, cmcResponseCNY, cmcResponseBTC, cmcResponseBGX)
if err != nil {
fmt.Printf("Error in cmc price %s\n", err.Error())
<-time.After(sleepy)
continue
}
<-time.After(sleepy)
}
}()
go func() {
for {
<-time.After(time.Second * 10)
tx, err := dbaforela.Begin()
if err != nil {
fmt.Printf("Error fetching ela price from hbg: %s\n", err.Error())
tx.Rollback()
continue
}
btcPrice, err := getPriceFromHbg()
if err != nil {
tx.Rollback()
fmt.Printf("Error fetching ela price from hbg: %s\n", err.Error())
continue
}
_, err = tx.Exec("update chain_cmc_price set price_btc = '" + btcPrice + "', local_system_time=current_timestamp() where symbol = 'ELA' order by _id desc limit 1")
if err != nil {
tx.Rollback()
fmt.Printf("Error fetching ela price from hbg 111 : %s\n", err.Error())
continue
}
tx.Commit()
}
}()
}
}
type hbg_price struct {
Status string
Ch string
Ts int64
Data []hbg_price_data
}
type hbg_price_data struct {
Id int64
Ts int64
Data []hg_price_data_data
}
type hg_price_data_data struct {
Amount float64
Ts int64
Id float64
Price float64
Direction string
}
func getPriceFromHbg() (string, error) {
resp, err := http.Get(HBG_ENDPOINT_URL)
if err != nil {
fmt.Printf("Error fetching price from hbg\n")
return "", err
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var hbg_price hbg_price
err = json.Unmarshal(body, &hbg_price)
if err != nil {
return "", err
}
if len(hbg_price.Data) > 0 && len(hbg_price.Data[0].Data) > 0 {
return strconv.FormatFloat(hbg_price.Data[0].Data[0].Price, 'f', 8, 64), nil
}
return "", errors.New("Error fetching price from hbg, data structure is changed")
}
}
func saveToDb(cmcResponseUSD, cmcResponseCNY, cmcResponseBTC, cmcResponseBGX CmcResponse) error {
tx, err := dba.Begin()
if err != nil {
return err
}
data := cmcResponseUSD.Data
if len(cmcResponseCNY.Data) != len(cmcResponseUSD.Data) || len(cmcResponseUSD.Data) != len(cmcResponseBTC.Data) {
fmt.Printf("Invalid Key fetch Cmc Data m CNY :%v, BTC :%v, USD :%v", cmcResponseCNY, cmcResponseBTC, cmcResponseUSD)
return nil
}
tx.Exec("delete from chain_cmc_price")
for i := 0; i < len(data); i++ {
var btcPrice string
if data[i].Symbol == "ELA" {
btcPrice, err = getPriceFromHbg()
if err != nil {
dba.Rollback(tx)
return err
}
fmt.Printf("Getting Price From Hbg " + btcPrice + "\n")
} else {
btcPrice = strconv.FormatFloat(cmcResponseBTC.Data[i].Quote.BTC.Price, 'f', 8, 64)
}
_, err = tx.Exec("insert into chain_cmc_price(id,name,symbol,`rank`,price_usd,price_cny,price_btc,24h_volume_usd,market_cap_usd,available_supply,total_supply,max_supply,percent_change_1h,percent_change_24h,percent_change_7d,last_updated,24h_volume_btc,market_cap_btc,local_system_time,24h_volume_cny,market_cap_cny,platform_symbol,platform_token_address,num_market_pairs) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
strconv.Itoa(int(data[i].Id)),
data[i].Name,
data[i].Symbol,
strconv.Itoa(data[i].Cmc_rank),
strconv.FormatFloat(data[i].Quote.USD.Price, 'f', 8, 64),
strconv.FormatFloat(cmcResponseCNY.Data[i].Quote.CNY.Price, 'f', 8, 64),
btcPrice,
strconv.FormatFloat(data[i].Quote.USD.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(data[i].Quote.USD.Market_cap, 'f', 8, 64),
strconv.FormatFloat(data[i].Circulating_supply, 'f', 8, 64),
strconv.FormatFloat(data[i].Total_supply, 'f', 8, 64),
strconv.FormatFloat(data[i].Max_supply, 'f', 8, 64),
strconv.FormatFloat(data[i].Quote.USD.Percent_change_1h, 'f', 8, 64),
strconv.FormatFloat(data[i].Quote.USD.Percent_change_24h, 'f', 8, 64),
strconv.FormatFloat(data[i].Quote.USD.Percent_change_7d, 'f', 8, 64),
data[i].Quote.USD.Last_updated,
strconv.FormatFloat(cmcResponseBTC.Data[i].Quote.BTC.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBTC.Data[i].Quote.BTC.Market_cap, 'f', 8, 64),
time.Now(),
strconv.FormatFloat(cmcResponseCNY.Data[i].Quote.CNY.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseCNY.Data[i].Quote.CNY.Market_cap, 'f', 8, 64),
data[i].Platform.Symbol,
data[i].Platform.Token_Address,
data[i].Num_market_pairs)
if err != nil {
dba.Rollback(tx)
return err
}
// put price that not in the cmc at rank 100
if i == 99 && len(cmcResponseBGX.Data) > 0 {
_, err = tx.Exec("insert into chain_cmc_price(id,name,symbol,`rank`,price_usd,price_cny,price_btc,24h_volume_usd,market_cap_usd,available_supply,total_supply,max_supply,percent_change_1h,percent_change_24h,percent_change_7d,last_updated,24h_volume_btc,market_cap_btc,local_system_time,24h_volume_cny,market_cap_cny,platform_symbol,platform_token_address,num_market_pairs) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
strconv.Itoa(int(cmcResponseBGX.Data[0].Id)),
cmcResponseBGX.Data[0].Name,
cmcResponseBGX.Data[0].Symbol,
strconv.Itoa(cmcResponseBGX.Data[0].Cmc_rank),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Price, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.CNY.Price, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.BTC.Price, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Market_cap, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Circulating_supply, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Total_supply, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Max_supply, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Percent_change_1h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Percent_change_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.USD.Percent_change_7d, 'f', 8, 64),
data[i].Quote.USD.Last_updated,
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.BTC.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.BTC.Market_cap, 'f', 8, 64),
time.Now(),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.CNY.Volume_24h, 'f', 8, 64),
strconv.FormatFloat(cmcResponseBGX.Data[0].Quote.CNY.Market_cap, 'f', 8, 64),
cmcResponseBGX.Data[0].Platform.Symbol,
cmcResponseBGX.Data[0].Platform.Token_Address,
cmcResponseBGX.Data[0].Num_market_pairs)
}
if err != nil {
dba.Rollback(tx)
return err
}
}
dba.Commit(tx)
return nil
}
func fetchBGXPrice() (CmcResponse, error) {
url := fmt.Sprintf(BGX_ENDPOINT_URL)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return CmcResponse{}, err
}
req.Header["Accept-Language"] = []string{"*"}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return CmcResponse{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return CmcResponse{}, err
}
id := 5000
name := "BIT GAME EXCHANGE"
symbol := "BGX"
slug := "BIG GAME"
circulating_supply := 2000000000
total_supply := 5000000000
max_supply := 0
date_added := "2019-02-27T13:53:00.000Z"
num_market_pairs := 1
platform_symbol := "ETH"
platform_token_address := "0xbf3f09e4eba5f7805e5fac0ee09fd6ee8eebe4cb"
bgxRespMap := new(map[string]interface{})
err = json.Unmarshal(body, &bgxRespMap)
if err != nil {
return CmcResponse{}, err
}
data, ok := (*bgxRespMap)["data"].(map[string]interface{})
if !ok {
return CmcResponse{}, errors.New("BGX Price Error")
}
rate, ok := data["rate"].(map[string]interface{})
if !ok {
return CmcResponse{}, errors.New("BGX Price Error")
}
enUS, ok := rate["en_US"].(map[string]interface{})
if !ok {
return CmcResponse{}, errors.New("BGX Price Error")
}
zhCN, ok := rate["zh_CN"].(map[string]interface{})
if !ok {
return CmcResponse{}, errors.New("BGX Price Error")
}
bgxUs, ok1 := enUS["BGX"].(float64)
btcUs, ok2 := enUS["BTC"].(float64)
bgxCn, ok3 := zhCN["BGX"].(float64)
if !(ok1 && ok2 && ok3) {
return CmcResponse{}, errors.New("BGX Price Error")
}
bgxbtc := math.Round(bgxUs/btcUs*100000000) / 100000000
now := time.Now().Format("2006-01-02T15:04:05.000Z")
return CmcResponse{
Status: Status{
Timestamp: now,
Error_code: 0,
Error_message: "",
Elapsed: 0,
Credit_count: 0,
},
Data: []Data{
Data{
Id: int64(id),
Name: name,
Symbol: symbol,
Slug: slug,
Circulating_supply: float64(circulating_supply),
Total_supply: float64(total_supply),
Max_supply: float64(max_supply),
Date_added: date_added,
Num_market_pairs: int64(num_market_pairs),
Tags: nil,
Platform: Plateform{
Symbol: platform_symbol,
Token_Address: platform_token_address,
},
Cmc_rank: 0,
Last_updated: date_added,
Quote: Quote{
CNY: Price{
Price: bgxCn,
Volume_24h: float64(0),
Percent_change_1h: float64(0),
Percent_change_24h: float64(0),
Percent_change_7d: float64(0),
Market_cap: float64(0),
Last_updated: now,
},
USD: Price{
Price: bgxUs,
Volume_24h: float64(0),
Percent_change_1h: float64(0),
Percent_change_24h: float64(0),
Percent_change_7d: float64(0),
Market_cap: float64(0),
Last_updated: now,
},
BTC: Price{
Price: bgxbtc,
Volume_24h: float64(0),
Percent_change_1h: float64(0),
Percent_change_24h: float64(0),
Percent_change_7d: float64(0),
Market_cap: float64(0),
Last_updated: now,
},
},
},
},
}, nil
}
func fetchPrice(i int, curr string) (CmcResponse, error) {
url := fmt.Sprintf(CMC_ENDPOINT_URL, config.Conf.Cmc.NumOfCoin, curr)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return CmcResponse{}, err
}
println(config.Conf.Cmc.ApiKey[i])
req.Header = map[string][]string{
"X-CMC_PRO_API_KEY": []string{config.Conf.Cmc.ApiKey[i]},
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return CmcResponse{}, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return CmcResponse{}, err
}
cmcResp := CmcResponse{}
err = json.Unmarshal(body, &cmcResp)
if err != nil {
return CmcResponse{}, err
}
return cmcResp, nil
}