@icgio/icg-utils 1.7.14 → 1.7.16

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.
@@ -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.28,
3
- USDKRW: 1416.53,
4
- USDTHB: 37.76,
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',
@@ -15,7 +22,7 @@ const options = {
15
22
 
16
23
  let coins_map = {}
17
24
  let get_coins_map = (cb) => {
18
- needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
25
+ cmc_limiter.process(needle.get, 'https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
19
26
  if (body && Array.isArray(body)) {
20
27
  let result = {}
21
28
  for (let line of body) {
@@ -31,14 +38,21 @@ let get_coins_map = (cb) => {
31
38
  })
32
39
  }
33
40
 
41
+ let lock = false
34
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
+ }
35
47
  function get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
48
+ lock = true
36
49
  if (type === 'pair') {
37
50
  let pair = pair_or_pairs_or_cur_curs
38
51
  let [cur, base_cur] = parse_pair(pair)
39
52
  let cur_id = coins_map[cur],
40
53
  vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
41
- needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
54
+ cmc_limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
55
+ lock = false
42
56
  if (err) {
43
57
  console.error(pair, err)
44
58
  } else if (!body[cur_id]) {
@@ -55,7 +69,8 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
55
69
  })
56
70
  )
57
71
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
58
- needle.get(url, (err, res, body) => {
72
+ cmc_limiter.process(needle.get, url, (err, res, body) => {
73
+ lock = false
59
74
  if (err) {
60
75
  console.error(err)
61
76
  return
@@ -79,7 +94,8 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
79
94
  } else if (type === 'cur') {
80
95
  let cur = pair_or_pairs_or_cur_curs,
81
96
  cur_id = coins_map[cur]
82
- needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
97
+ cmc_limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
98
+ lock = false
83
99
  if (err) {
84
100
  console.error(cur, err)
85
101
  } else if (!body[cur_id]) {
@@ -96,7 +112,8 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
96
112
  })
97
113
  )
98
114
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
99
- needle.get(url, (err, res, body) => {
115
+ cmc_limiter.process(needle.get, url, (err, res, body) => {
116
+ lock = false
100
117
  if (err) {
101
118
  console.error(err)
102
119
  return
@@ -122,9 +139,9 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
122
139
  quote_cur = 'USD',
123
140
  page = 1,
124
141
  per_page_nums = 250
125
- let get_coingecko_rate = (page, cb) => {
142
+ let get_coingecko_rate_page = (page, cb) => {
126
143
  let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
127
- needle.get(url, options, (err, res, body) => {
144
+ cmc_limiter.process(needle.get, url, options, (err, res, body) => {
128
145
  console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
129
146
  if (body && body.length > 0) {
130
147
  for (let coin of body) {
@@ -147,23 +164,19 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
147
164
  }
148
165
  }
149
166
  if (!result.BTC || !result.ETH) {
150
- setTimeout(() => {
151
- console.log('fetching coingecko rate: page', page, 'no-btc-eth')
152
- get_coingecko_rates(cb)
153
- }, 5000)
167
+ get_coingecko_rates(cb)
154
168
  } else {
155
169
  for (let cur in result) {
156
170
  result[cur].BTC = result[cur].USD / result.BTC.USD
157
171
  result[cur].ETH = result[cur].USD / result.ETH.USD
158
172
  }
159
173
  if (body.length < per_page_nums) {
174
+ lock = false
160
175
  cb(result)
161
176
  } else {
162
177
  page++
163
- setTimeout(() => {
164
- console.log('fetching coingecko rate: page', page, 'good')
165
- get_coingecko_rate(page, cb)
166
- }, 5000)
178
+ console.log('fetching coingecko rate: page', page, 'good')
179
+ get_coingecko_rate_page(page, cb)
167
180
  }
168
181
  }
169
182
  } else {
@@ -175,7 +188,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
175
188
  })
176
189
  }
177
190
  console.log('fetching coingecko rate: page', page, 'first')
178
- get_coingecko_rate(page, cb)
191
+ get_coingecko_rate_page(page, cb)
179
192
  }
180
193
  }
181
194
  if (_.isEmpty(coins_map)) {
@@ -192,15 +205,9 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
192
205
  // (res) => {
193
206
  // console.log(res)
194
207
  // },
195
- // // ['BTCUSD', 'ETHUSD'],
196
- // // 'pairs'
197
- // // )
198
208
  // ['RED', 'PIAS', 'PRMX'],
199
209
  // 'curs'
200
210
  // )
201
- // }, 'ETHUSDC', 'pair')
202
- // }, 'BTC')
203
- // })
204
211
 
205
212
  // get_coins_map((res) => {
206
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(`http://www.apilayer.net/api/live?access_key=d679190b0d9dab9d9353f20caea4682f&source=${cur}&currencies=${base_cur}&format=1`, (err, res, body) => {
7
- if (body.success) {
8
- cb(body.quotes[pair])
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.14",
3
+ "version": "1.7.16",
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.6.0",
29
+ "needle": "^2.9.1",
30
30
  "nodemailer": "^4.7.0",
31
31
  "qrcode-terminal": "^0.12.0",
32
32
  "websocket": "^1.0.33",