@icgio/icg-utils 1.7.39 → 1.7.41

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/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  module.exports = {
2
- coinmarketcap: require('./lib/coingecko.js'),
3
- coingecko: require('./lib/coingecko.js'),
2
+ coinmarketcap: require('./lib/crypto-rates.js'),
3
+ coingecko: require('./lib/crypto-rates.js'),
4
4
  forex_rate: require('./lib/forex-rate.js'),
5
5
  html_parser: require('./lib/html-parser.js'),
6
6
  influx: require('./lib/influxdb.js'),
@@ -3,55 +3,72 @@ const _ = require('lodash')
3
3
  const needle = require('needle')
4
4
 
5
5
  const parse_pair = require('./utils.js').parse_pair
6
- const stablecoins = require('../constants/stablecoins')
7
- let coins_map_raw = require('../constants/coins-map.js')
6
+ const stablecoins = require('../constants/stablecoins.js')
7
+ let coins_map_raw_coingecko = require('../constants/coins-map-coingecko.js')
8
+ let coins_map_raw_coinmarketcap = require('../constants/coins-map-coinmarketcap.js')
8
9
  let coins_map = {}
9
10
 
11
+ const sort_keys = require('./utils.js').sort_keys
12
+
10
13
  const Limiter = require('./rate-limiter.js')
11
14
 
12
- let cmc_limiter = new Limiter({
15
+ let limiter = new Limiter({
13
16
  max_concurrent: 1,
14
17
  interval_concurrent: 3000,
15
18
  })
16
19
 
17
- const options = {
18
- headers: {
19
- // 'authority': 'api.coingecko.com',
20
- // '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',
21
- // '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',
22
- },
23
- }
20
+ const options = {}
24
21
 
25
- let process_coins_map = (body) => {
26
- let result = {}
27
- for (let line of body) {
28
- let cur = line.symbol.toUpperCase()
29
- _.set(result, [cur], line.id)
30
- }
22
+ let process_coins_map_coingecko = (result) => {
31
23
  result['ETH'] = 'ethereum'
32
24
  result['MTR'] = 'meter-stable'
33
25
  result['WAXL'] = 'axelar'
34
26
  return result
35
27
  }
36
28
 
37
- let get_coins_map = (cb) => {
38
- cmc_limiter.process(needle.get, 'https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
29
+ let process_coins_map_coinmarketcap = (result) => {
30
+ return result
31
+ }
32
+
33
+ let get_coins_map_coingecko = (cb) => {
34
+ limiter.process(needle.get, 'https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
39
35
  if (body && Array.isArray(body)) {
40
- cb({ success: true, body: process_coins_map(body) })
36
+ let result = {}
37
+ for (let line of body) {
38
+ let cur = line.symbol.toUpperCase()
39
+ _.set(result, [cur], line.id)
40
+ }
41
+ cb({ success: true, body: process_coins_map_coingecko(result) })
41
42
  } else {
42
43
  cb({ success: false, body })
43
44
  }
44
45
  })
45
46
  }
46
47
 
47
- let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
48
- function get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
48
+ let get_coins_map_coinmarketcap = (cb) => {
49
+ limiter.process(needle.get, 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map?CMC_PRO_API_KEY=47a30ef1-a773-4da0-8ac5-69bce757fdfa', (err, res, body) => {
50
+ if (body && body.data && Array.isArray(body.data)) {
51
+ let result = {}
52
+ for (let line of body.data) {
53
+ let cur = line.symbol.toUpperCase()
54
+ _.set(result, [cur], line.name)
55
+ }
56
+ result = sort_keys(result)
57
+ cb({ success: true, body: process_coins_map_coinmarketcap(result) })
58
+ } else {
59
+ cb({ success: false, body })
60
+ }
61
+ })
62
+ }
63
+
64
+ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko') => {
65
+ function get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
49
66
  if (type === 'pair') {
50
67
  let pair = pair_or_pairs_or_cur_curs
51
68
  let [cur, base_cur] = parse_pair(pair)
52
69
  let cur_id = coins_map[cur],
53
70
  vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
54
- cmc_limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
71
+ limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
55
72
  if (err) {
56
73
  console.error(pair, err)
57
74
  } else if (!body[cur_id]) {
@@ -68,7 +85,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
68
85
  })
69
86
  )
70
87
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
71
- cmc_limiter.process(needle.get, url, (err, res, body) => {
88
+ limiter.process(needle.get, url, (err, res, body) => {
72
89
  if (err) {
73
90
  console.error(err)
74
91
  return
@@ -92,7 +109,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
92
109
  } else if (type === 'cur') {
93
110
  let cur = pair_or_pairs_or_cur_curs,
94
111
  cur_id = coins_map[cur]
95
- cmc_limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
112
+ limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
96
113
  if (err) {
97
114
  console.error(cur, err)
98
115
  } else if (!body[cur_id]) {
@@ -100,7 +117,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
100
117
  }
101
118
  cb(body[cur_id]['usd'])
102
119
  })
103
- } else if (type === 'curs') {
120
+ } else if (type === 'curs' && source === 'coingecko') {
104
121
  let curs = pair_or_pairs_or_cur_curs
105
122
  let cur_id_list = curs.map((cur) => coins_map[cur])
106
123
  let cur_id_map = _.fromPairs(
@@ -109,7 +126,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
109
126
  })
110
127
  )
111
128
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
112
- cmc_limiter.process(needle.get, url, (err, res, body) => {
129
+ limiter.process(needle.get, url, (err, res, body) => {
113
130
  if (err) {
114
131
  console.error(err)
115
132
  return
@@ -130,6 +147,37 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
130
147
  }
131
148
  cb(result)
132
149
  })
150
+ } else if (type === 'curs' && source === 'coinmarketcap') {
151
+ let curs = pair_or_pairs_or_cur_curs
152
+ let cur_slug_list = curs.map((cur) => coins_map[cur])
153
+ let url = `https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=${String(curs)}&CMC_PRO_API_KEY=47a30ef1-a773-4da0-8ac5-69bce757fdfa`
154
+ limiter.process(needle.get, url, (err, res, body) => {
155
+ if (err) {
156
+ console.error(err)
157
+ return
158
+ }
159
+ let result = {}
160
+ for (let cur in body.data) {
161
+ let rates = body.data[cur]
162
+ let temp = {}
163
+ for (let rate of rates) {
164
+ if (rate.name === coins_map[cur]) {
165
+ temp = {
166
+ USD: rate.quote.USD.price,
167
+ USDT: rate.quote.USD.price,
168
+ }
169
+ }
170
+ }
171
+ result[cur] = temp
172
+ }
173
+ for (let stable of stablecoins) {
174
+ result[stable] = {
175
+ USD: 1,
176
+ USDT: 1,
177
+ }
178
+ }
179
+ cb(result)
180
+ })
133
181
  } else {
134
182
  let result = {},
135
183
  quote_cur = 'USD',
@@ -137,7 +185,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
137
185
  per_page_nums = 250
138
186
  let get_coingecko_rate_page = (page, cb) => {
139
187
  let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
140
- cmc_limiter.process(needle.get, url, options, (err, res, body) => {
188
+ limiter.process(needle.get, url, options, (err, res, body) => {
141
189
  console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
142
190
  if (body && body.length > 0) {
143
191
  for (let coin of body) {
@@ -160,7 +208,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
160
208
  }
161
209
  }
162
210
  if (!result.BTC || !result.ETH) {
163
- get_coingecko_rates(cb)
211
+ get_crypto_rates(cb)
164
212
  } else {
165
213
  for (let cur in result) {
166
214
  result[cur].BTC = result[cur].USD / result.BTC.USD
@@ -177,7 +225,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
177
225
  } else {
178
226
  setTimeout(() => {
179
227
  console.log('fetching coingecko rate: page', page, 'timeout')
180
- get_coingecko_rates(cb)
228
+ get_crypto_rates(cb)
181
229
  }, 60000)
182
230
  }
183
231
  })
@@ -186,29 +234,25 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
186
234
  get_coingecko_rate_page(page, cb)
187
235
  }
188
236
  }
189
- // if (_.isEmpty(coins_map)) {
190
- // get_coins_map((res) => {
191
- // if (res.success) {
192
- // coins_map = res.body
193
- // }
194
- // get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type)
195
- // })
196
- // } else {
197
- coins_map = process_coins_map(coins_map_raw)
198
- get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type)
199
- // }
237
+ coins_map = source === 'coingecko' ? process_coins_map_coingecko(coins_map_raw_coingecko) : process_coins_map_coinmarketcap(coins_map_raw_coinmarketcap)
238
+ get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type)
200
239
  }
201
240
 
202
- // get_coingecko_rates(
241
+ // get_crypto_rates(
203
242
  // (res) => {
204
243
  // console.log(res)
205
244
  // },
206
- // ['BTC', 'ETH'],
207
- // 'curs'
245
+ // ['BTC', 'ETH', 'DIS'],
246
+ // 'curs',
247
+ // 'coinmarketcap'
208
248
  // )
209
249
 
210
- // get_coins_map((res) => {
211
- // console.log(res)
250
+ // get_coins_map_coingecko((res) => {
251
+ // console.log(res.body)
252
+ // })
253
+
254
+ // get_coins_map_coinmarketcap((res) => {
255
+ // console.log(res.body)
212
256
  // })
213
257
 
214
- module.exports = get_coingecko_rates
258
+ module.exports = get_crypto_rates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.39",
3
+ "version": "1.7.41",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {