@icgio/icg-utils 1.7.38 → 1.7.40

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,54 +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'
25
+ result['WAXL'] = 'axelar'
33
26
  return result
34
27
  }
35
28
 
36
- let get_coins_map = (cb) => {
37
- 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) => {
38
35
  if (body && Array.isArray(body)) {
39
- 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) })
40
42
  } else {
41
43
  cb({ success: false, body })
42
44
  }
43
45
  })
44
46
  }
45
47
 
46
- let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
47
- 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) {
48
66
  if (type === 'pair') {
49
67
  let pair = pair_or_pairs_or_cur_curs
50
68
  let [cur, base_cur] = parse_pair(pair)
51
69
  let cur_id = coins_map[cur],
52
70
  vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
53
- 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) => {
54
72
  if (err) {
55
73
  console.error(pair, err)
56
74
  } else if (!body[cur_id]) {
@@ -67,7 +85,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
67
85
  })
68
86
  )
69
87
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
70
- cmc_limiter.process(needle.get, url, (err, res, body) => {
88
+ limiter.process(needle.get, url, (err, res, body) => {
71
89
  if (err) {
72
90
  console.error(err)
73
91
  return
@@ -91,7 +109,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
91
109
  } else if (type === 'cur') {
92
110
  let cur = pair_or_pairs_or_cur_curs,
93
111
  cur_id = coins_map[cur]
94
- 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) => {
95
113
  if (err) {
96
114
  console.error(cur, err)
97
115
  } else if (!body[cur_id]) {
@@ -99,7 +117,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
99
117
  }
100
118
  cb(body[cur_id]['usd'])
101
119
  })
102
- } else if (type === 'curs') {
120
+ } else if (type === 'curs' && source === 'coingecko') {
103
121
  let curs = pair_or_pairs_or_cur_curs
104
122
  let cur_id_list = curs.map((cur) => coins_map[cur])
105
123
  let cur_id_map = _.fromPairs(
@@ -108,7 +126,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
108
126
  })
109
127
  )
110
128
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
111
- cmc_limiter.process(needle.get, url, (err, res, body) => {
129
+ limiter.process(needle.get, url, (err, res, body) => {
112
130
  if (err) {
113
131
  console.error(err)
114
132
  return
@@ -129,6 +147,37 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
129
147
  }
130
148
  cb(result)
131
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
+ })
132
181
  } else {
133
182
  let result = {},
134
183
  quote_cur = 'USD',
@@ -136,7 +185,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
136
185
  per_page_nums = 250
137
186
  let get_coingecko_rate_page = (page, cb) => {
138
187
  let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
139
- cmc_limiter.process(needle.get, url, options, (err, res, body) => {
188
+ limiter.process(needle.get, url, options, (err, res, body) => {
140
189
  console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
141
190
  if (body && body.length > 0) {
142
191
  for (let coin of body) {
@@ -159,7 +208,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
159
208
  }
160
209
  }
161
210
  if (!result.BTC || !result.ETH) {
162
- get_coingecko_rates(cb)
211
+ get_crypto_rates(cb)
163
212
  } else {
164
213
  for (let cur in result) {
165
214
  result[cur].BTC = result[cur].USD / result.BTC.USD
@@ -176,7 +225,7 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
176
225
  } else {
177
226
  setTimeout(() => {
178
227
  console.log('fetching coingecko rate: page', page, 'timeout')
179
- get_coingecko_rates(cb)
228
+ get_crypto_rates(cb)
180
229
  }, 60000)
181
230
  }
182
231
  })
@@ -185,29 +234,25 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
185
234
  get_coingecko_rate_page(page, cb)
186
235
  }
187
236
  }
188
- // if (_.isEmpty(coins_map)) {
189
- // get_coins_map((res) => {
190
- // if (res.success) {
191
- // coins_map = res.body
192
- // }
193
- // get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type)
194
- // })
195
- // } else {
196
- coins_map = process_coins_map(coins_map_raw)
197
- get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type)
198
- // }
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)
199
239
  }
200
240
 
201
- // get_coingecko_rates(
241
+ // get_crypto_rates(
202
242
  // (res) => {
203
243
  // console.log(res)
204
244
  // },
205
- // ['BTC', 'ETH'],
206
- // 'curs'
245
+ // ['BTC', 'ETH', 'DIS'],
246
+ // 'curs',
247
+ // 'coinmarketcap'
207
248
  // )
208
249
 
209
- // get_coins_map((res) => {
210
- // 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)
211
256
  // })
212
257
 
213
- 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.38",
3
+ "version": "1.7.40",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {