@icgio/icg-utils 1.7.13 → 1.7.15
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/constants/forex-rate.js +11 -3
- package/lib/coingecko.js +37 -25
- package/lib/forex-rate.js +3 -3
- package/package.json +2 -2
package/constants/forex-rate.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
// const { get_forex_rate } = require('../lib/forex-rate.js')
|
|
2
|
+
|
|
3
|
+
// for (let pair of ['USDCNY', 'USDKRW', 'USDTHB']) {
|
|
4
|
+
// get_forex_rate(pair, (res) => {
|
|
5
|
+
// console.log(pair, res)
|
|
6
|
+
// })
|
|
7
|
+
// }
|
|
8
|
+
|
|
1
9
|
module.exports = {
|
|
2
|
-
USDCNY: 7.
|
|
3
|
-
USDKRW:
|
|
4
|
-
USDTHB:
|
|
10
|
+
USDCNY: 7.17,
|
|
11
|
+
USDKRW: 1395.24,
|
|
12
|
+
USDTHB: 36.48,
|
|
5
13
|
}
|
package/lib/coingecko.js
CHANGED
|
@@ -5,6 +5,13 @@ const needle = require('needle')
|
|
|
5
5
|
const parse_pair = require('./utils.js').parse_pair
|
|
6
6
|
const stablecoins = require('../constants/stablecoins')
|
|
7
7
|
|
|
8
|
+
const Limiter = require('./rate-limiter.js')
|
|
9
|
+
|
|
10
|
+
let cmc_limiter = new Limiter({
|
|
11
|
+
max_concurrent: 1,
|
|
12
|
+
interval_concurrent: 3000,
|
|
13
|
+
})
|
|
14
|
+
|
|
8
15
|
const options = {
|
|
9
16
|
headers: {
|
|
10
17
|
// 'authority': 'api.coingecko.com',
|
|
@@ -17,29 +24,35 @@ let coins_map = {}
|
|
|
17
24
|
let get_coins_map = (cb) => {
|
|
18
25
|
needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
|
|
19
26
|
if (body && Array.isArray(body)) {
|
|
20
|
-
let
|
|
21
|
-
let
|
|
22
|
-
for (let line of list) {
|
|
27
|
+
let result = {}
|
|
28
|
+
for (let line of body) {
|
|
23
29
|
let cur = line.symbol.toUpperCase()
|
|
24
|
-
_.set(
|
|
30
|
+
_.set(result, [cur], line.id)
|
|
25
31
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
cb({ success: true, body })
|
|
32
|
+
result['ETH'] = 'ethereum'
|
|
33
|
+
result['MTR'] = 'meter-stable'
|
|
34
|
+
cb({ success: true, body: result })
|
|
29
35
|
} else {
|
|
30
36
|
cb({ success: false, body })
|
|
31
37
|
}
|
|
32
38
|
})
|
|
33
39
|
}
|
|
34
40
|
|
|
41
|
+
let lock = false
|
|
35
42
|
let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
43
|
+
if (lock) {
|
|
44
|
+
console.error('avoid calling get_coingecko_rates multiple times in a short time')
|
|
45
|
+
return
|
|
46
|
+
}
|
|
36
47
|
function get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
|
|
48
|
+
lock = true
|
|
37
49
|
if (type === 'pair') {
|
|
38
50
|
let pair = pair_or_pairs_or_cur_curs
|
|
39
51
|
let [cur, base_cur] = parse_pair(pair)
|
|
40
52
|
let cur_id = coins_map[cur],
|
|
41
53
|
vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
|
|
42
54
|
needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
|
|
55
|
+
lock = false
|
|
43
56
|
if (err) {
|
|
44
57
|
console.error(pair, err)
|
|
45
58
|
} else if (!body[cur_id]) {
|
|
@@ -57,6 +70,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
57
70
|
)
|
|
58
71
|
let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
|
|
59
72
|
needle.get(url, (err, res, body) => {
|
|
73
|
+
lock = false
|
|
60
74
|
if (err) {
|
|
61
75
|
console.error(err)
|
|
62
76
|
return
|
|
@@ -81,6 +95,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
81
95
|
let cur = pair_or_pairs_or_cur_curs,
|
|
82
96
|
cur_id = coins_map[cur]
|
|
83
97
|
needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
|
|
98
|
+
lock = false
|
|
84
99
|
if (err) {
|
|
85
100
|
console.error(cur, err)
|
|
86
101
|
} else if (!body[cur_id]) {
|
|
@@ -98,6 +113,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
98
113
|
)
|
|
99
114
|
let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
|
|
100
115
|
needle.get(url, (err, res, body) => {
|
|
116
|
+
lock = false
|
|
101
117
|
if (err) {
|
|
102
118
|
console.error(err)
|
|
103
119
|
return
|
|
@@ -123,9 +139,9 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
123
139
|
quote_cur = 'USD',
|
|
124
140
|
page = 1,
|
|
125
141
|
per_page_nums = 250
|
|
126
|
-
let
|
|
142
|
+
let get_coingecko_rate_page = (page, cb) => {
|
|
127
143
|
let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
|
|
128
|
-
needle.get
|
|
144
|
+
cmc_limiter.process(needle.get, url, options, (err, res, body) => {
|
|
129
145
|
console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
|
|
130
146
|
if (body && body.length > 0) {
|
|
131
147
|
for (let coin of body) {
|
|
@@ -148,23 +164,19 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
148
164
|
}
|
|
149
165
|
}
|
|
150
166
|
if (!result.BTC || !result.ETH) {
|
|
151
|
-
|
|
152
|
-
console.log('fetching coingecko rate: page', page, 'no-btc-eth')
|
|
153
|
-
get_coingecko_rates(cb)
|
|
154
|
-
}, 5000)
|
|
167
|
+
get_coingecko_rates(cb)
|
|
155
168
|
} else {
|
|
156
169
|
for (let cur in result) {
|
|
157
170
|
result[cur].BTC = result[cur].USD / result.BTC.USD
|
|
158
171
|
result[cur].ETH = result[cur].USD / result.ETH.USD
|
|
159
172
|
}
|
|
160
173
|
if (body.length < per_page_nums) {
|
|
174
|
+
lock = false
|
|
161
175
|
cb(result)
|
|
162
176
|
} else {
|
|
163
177
|
page++
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
get_coingecko_rate(page, cb)
|
|
167
|
-
}, 5000)
|
|
178
|
+
console.log('fetching coingecko rate: page', page, 'good')
|
|
179
|
+
get_coingecko_rate_page(page, cb)
|
|
168
180
|
}
|
|
169
181
|
}
|
|
170
182
|
} else {
|
|
@@ -176,7 +188,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
176
188
|
})
|
|
177
189
|
}
|
|
178
190
|
console.log('fetching coingecko rate: page', page, 'first')
|
|
179
|
-
|
|
191
|
+
get_coingecko_rate_page(page, cb)
|
|
180
192
|
}
|
|
181
193
|
}
|
|
182
194
|
if (_.isEmpty(coins_map)) {
|
|
@@ -189,13 +201,13 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
|
|
|
189
201
|
}
|
|
190
202
|
}
|
|
191
203
|
|
|
192
|
-
// get_coingecko_rates(
|
|
193
|
-
//
|
|
194
|
-
//
|
|
195
|
-
//
|
|
196
|
-
//
|
|
197
|
-
//
|
|
198
|
-
//
|
|
204
|
+
// get_coingecko_rates(
|
|
205
|
+
// (res) => {
|
|
206
|
+
// console.log(res)
|
|
207
|
+
// },
|
|
208
|
+
// ['RED', 'PIAS', 'PRMX'],
|
|
209
|
+
// 'curs'
|
|
210
|
+
// )
|
|
199
211
|
|
|
200
212
|
// get_coins_map((res) => {
|
|
201
213
|
// console.log(res)
|
package/lib/forex-rate.js
CHANGED
|
@@ -3,9 +3,9 @@ const utils = require('./utils.js')
|
|
|
3
3
|
|
|
4
4
|
let get_forex_rate = function (pair, cb) {
|
|
5
5
|
let [cur, base_cur] = utils.parse_pair(pair)
|
|
6
|
-
needle.get(`
|
|
7
|
-
if (body.
|
|
8
|
-
cb(body.
|
|
6
|
+
needle.get(`https://api.exchangerate.host/latest`, (err, res, body) => {
|
|
7
|
+
if (body.rates) {
|
|
8
|
+
cb(body.rates[base_cur])
|
|
9
9
|
}
|
|
10
10
|
})
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@icgio/icg-utils",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.15",
|
|
4
4
|
"description": "Utils package.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"influx": "^5.9.3",
|
|
27
27
|
"lodash": "^4.17.21",
|
|
28
28
|
"mailgun-js": "^0.18.1",
|
|
29
|
-
"needle": "^2.
|
|
29
|
+
"needle": "^2.9.1",
|
|
30
30
|
"nodemailer": "^4.7.0",
|
|
31
31
|
"qrcode-terminal": "^0.12.0",
|
|
32
32
|
"websocket": "^1.0.33",
|