@icgio/icg-utils 1.7.11 → 1.7.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +7 -0
- package/.prettierrc.json +6 -0
- package/lib/coingecko.js +39 -33
- package/package.json +1 -1
package/.prettierignore
ADDED
package/.prettierrc.json
ADDED
package/lib/coingecko.js
CHANGED
|
@@ -11,14 +11,15 @@ const options = {
|
|
|
11
11
|
// 'authority': 'api.coingecko.com',
|
|
12
12
|
// 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
|
|
13
13
|
// 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
14
|
-
}
|
|
14
|
+
},
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
18
18
|
if (type === 'pair') {
|
|
19
19
|
let pair = pair_or_pairs_or_cur_curs
|
|
20
20
|
let [cur, base_cur] = parse_pair(pair)
|
|
21
|
-
let cur_id = coins_map[cur],
|
|
21
|
+
let cur_id = coins_map[cur],
|
|
22
|
+
vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
|
|
22
23
|
needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
|
|
23
24
|
if (err) {
|
|
24
25
|
console.error(pair, err)
|
|
@@ -29,10 +30,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
29
30
|
})
|
|
30
31
|
} else if (type === 'pairs') {
|
|
31
32
|
let pairs = pair_or_pairs_or_cur_curs
|
|
32
|
-
let cur_id_map = _.fromPairs(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
let cur_id_map = _.fromPairs(
|
|
34
|
+
pairs.map((pair) => {
|
|
35
|
+
let [cur, base_cur] = parse_pair(pair)
|
|
36
|
+
return [coins_map[cur], cur]
|
|
37
|
+
})
|
|
38
|
+
)
|
|
36
39
|
let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
|
|
37
40
|
needle.get(url, (err, res, body) => {
|
|
38
41
|
if (err) {
|
|
@@ -50,13 +53,14 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
50
53
|
for (let stable of stablecoins) {
|
|
51
54
|
result[stable] = {
|
|
52
55
|
USD: 1,
|
|
53
|
-
USDT: 1
|
|
56
|
+
USDT: 1,
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
cb(result)
|
|
57
60
|
})
|
|
58
61
|
} else if (type === 'cur') {
|
|
59
|
-
let cur = pair_or_pairs_or_cur_curs,
|
|
62
|
+
let cur = pair_or_pairs_or_cur_curs,
|
|
63
|
+
cur_id = coins_map[cur]
|
|
60
64
|
needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
|
|
61
65
|
if (err) {
|
|
62
66
|
console.error(cur, err)
|
|
@@ -67,10 +71,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
67
71
|
})
|
|
68
72
|
} else if (type === 'curs') {
|
|
69
73
|
let curs = pair_or_pairs_or_cur_curs
|
|
70
|
-
let cur_id_list = curs.map(cur => coins_map[cur])
|
|
71
|
-
let cur_id_map = _.fromPairs(
|
|
72
|
-
|
|
73
|
-
|
|
74
|
+
let cur_id_list = curs.map((cur) => coins_map[cur])
|
|
75
|
+
let cur_id_map = _.fromPairs(
|
|
76
|
+
curs.map((cur) => {
|
|
77
|
+
return [coins_map[cur], cur]
|
|
78
|
+
})
|
|
79
|
+
)
|
|
74
80
|
let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
|
|
75
81
|
needle.get(url, (err, res, body) => {
|
|
76
82
|
if (err) {
|
|
@@ -88,13 +94,16 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
88
94
|
for (let stable of stablecoins) {
|
|
89
95
|
result[stable] = {
|
|
90
96
|
USD: 1,
|
|
91
|
-
USDT: 1
|
|
97
|
+
USDT: 1,
|
|
92
98
|
}
|
|
93
99
|
}
|
|
94
100
|
cb(result)
|
|
95
101
|
})
|
|
96
102
|
} else {
|
|
97
|
-
let result = {},
|
|
103
|
+
let result = {},
|
|
104
|
+
quote_cur = 'USD',
|
|
105
|
+
page = 1,
|
|
106
|
+
per_page_nums = 250
|
|
98
107
|
let get_coingecko_rate = (page, cb) => {
|
|
99
108
|
let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
|
|
100
109
|
needle.get(url, options, (err, res, body) => {
|
|
@@ -102,30 +111,27 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
102
111
|
if (body && body.length > 0) {
|
|
103
112
|
for (let coin of body) {
|
|
104
113
|
let symbol = coin.symbol
|
|
105
|
-
if (!symbol || !coin.current_price)
|
|
106
|
-
|
|
107
|
-
|
|
114
|
+
if (!symbol || !coin.current_price) continue
|
|
115
|
+
|
|
108
116
|
symbol = symbol.toUpperCase()
|
|
109
|
-
|
|
110
|
-
if (symbol === 'BAT' && coin.id !== 'basic-attention-token')
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
continue
|
|
114
|
-
|
|
117
|
+
|
|
118
|
+
if (symbol === 'BAT' && coin.id !== 'basic-attention-token') continue
|
|
119
|
+
else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') continue
|
|
120
|
+
|
|
115
121
|
result[symbol] = {
|
|
116
|
-
|
|
117
|
-
|
|
122
|
+
USD: parseFloat(coin.current_price),
|
|
123
|
+
USDT: parseFloat(coin.current_price),
|
|
118
124
|
'24h_change': parseFloat(coin.price_change_percentage_24h),
|
|
119
125
|
'24h_volume_usd': parseFloat(coin.total_volume),
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
126
|
+
rank: parseInt(coin.market_cap_rank) || 10000,
|
|
127
|
+
market_cap: parseInt(coin.market_cap),
|
|
128
|
+
update_time: new Date(coin.last_updated),
|
|
123
129
|
}
|
|
124
130
|
}
|
|
125
131
|
if (!result.BTC || !result.ETH) {
|
|
126
|
-
setTimeout(() => {
|
|
132
|
+
setTimeout(() => {
|
|
127
133
|
console.log('fetching coingecko rate: page', page, 'no-btc-eth')
|
|
128
|
-
get_coingecko_rates(cb)
|
|
134
|
+
get_coingecko_rates(cb)
|
|
129
135
|
}, 5000)
|
|
130
136
|
} else {
|
|
131
137
|
for (let cur in result) {
|
|
@@ -139,11 +145,11 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
139
145
|
setTimeout(() => {
|
|
140
146
|
console.log('fetching coingecko rate: page', page, 'good')
|
|
141
147
|
get_coingecko_rate(page, cb)
|
|
142
|
-
},
|
|
148
|
+
}, 5000)
|
|
143
149
|
}
|
|
144
150
|
}
|
|
145
151
|
} else {
|
|
146
|
-
setTimeout(() => {
|
|
152
|
+
setTimeout(() => {
|
|
147
153
|
console.log('fetching coingecko rate: page', page, 'timeout')
|
|
148
154
|
get_coingecko_rates(cb)
|
|
149
155
|
}, 60000)
|
|
@@ -176,7 +182,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
176
182
|
|
|
177
183
|
// map['ETH'] = 'ethereum'
|
|
178
184
|
// map['MTR'] = 'meter-stable'
|
|
179
|
-
|
|
185
|
+
|
|
180
186
|
// console.log(JSON.stringify(map, null, 4))
|
|
181
187
|
// })
|
|
182
188
|
|