@icgio/icg-utils 1.9.42 → 1.9.44

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,5 @@
1
1
  module.exports = {
2
- coinmarketcap: require('./lib/crypto-rates.js'),
3
- coingecko: require('./lib/crypto-rates.js'),
2
+ crypto_rates: require('./lib/crypto-rates.js'),
4
3
  forex_rate: require('./lib/forex-rate.js'),
5
4
  influx: require('./lib/influxdb.js'),
6
5
  ip: require('./lib/ip.js'),
@@ -9,5 +8,4 @@ module.exports = {
9
8
  ohlcv: require('./lib/ohlcv'),
10
9
  rate_limiter: require('./lib/rate-limiter.js'),
11
10
  utils: require('./lib/utils.js'),
12
- constants: {},
13
11
  }
@@ -2,13 +2,8 @@ const _ = require('lodash')
2
2
 
3
3
  const needle = require('needle')
4
4
 
5
- let coins_map_raw_coingecko = require('../constants/coins-map-coingecko.js')
6
- let coins_map_raw_coinmarketcap = require('../constants/coins-map-coinmarketcap.js')
7
- let coins_map = {}
8
-
9
- const sort_keys = require('./utils.js').sort_keys
10
-
11
5
  const Limiter = require('./rate-limiter.js')
6
+ const { dex_rate, token_dict } = require('./dex-rate.js')
12
7
 
13
8
  let limiter = new Limiter({
14
9
  max_concurrent: 1,
@@ -17,107 +12,50 @@ let limiter = new Limiter({
17
12
 
18
13
  const options = {}
19
14
 
20
- let process_coins_map_coingecko = (res) => {
21
- res['AXL'] = 'axelar'
22
- res['ETH'] = 'ethereum'
23
- res['MTR'] = 'meter-stable'
24
- res['WAXL'] = 'axelar'
25
- return res
26
- }
27
-
28
- let process_coins_map_coinmarketcap = (res) => {
29
- return res
30
- }
31
-
32
- let get_coins_map_coingecko = (cb) => {
33
- limiter.process(needle.get, 'https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
34
- if (body && Array.isArray(body)) {
35
- let res = {}
36
- for (let line of body) {
37
- let cur = line.symbol.toUpperCase()
38
- _.set(res, [cur], line.id)
39
- }
40
- cb({ success: true, body: process_coins_map_coingecko(res) })
41
- } else {
42
- cb({ success: false, body })
43
- }
44
- })
45
- }
46
-
47
- let get_coins_map_coinmarketcap = (cb) => {
48
- limiter.process(needle.get, 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/map?CMC_PRO_API_KEY=47a30ef1-a773-4da0-8ac5-69bce757fdfa', (err, res, body) => {
49
- if (body && body.data && Array.isArray(body.data)) {
50
- let res = {}
51
- for (let line of body.data) {
52
- let cur = line.symbol.toUpperCase()
53
- _.set(res, [cur], line.name)
54
- }
55
- res = sort_keys(res)
56
- cb({ success: true, body: process_coins_map_coinmarketcap(res) })
57
- } else {
58
- cb({ success: false, body })
59
- }
60
- })
61
- }
62
-
63
15
  let result = {}
64
- let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko') => {
16
+ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
65
17
  function get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
66
18
  if (type === 'cur') {
67
- let cur = pair_or_pairs_or_cur_curs,
68
- cur_id = coins_map[cur]
69
- limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
19
+ let cur = pair_or_pairs_or_cur_curs
20
+ // Fetch all tickers and find by symbol
21
+ limiter.process(needle.get, 'https://api.coinpaprika.com/v1/tickers', (err, res, body) => {
70
22
  if (err) {
71
23
  console.error(cur, err)
72
- } else if (!body[cur_id]) {
73
- console.error('coingecko cur_id incorrect:', cur, cur_id, 'usd', err)
74
- }
75
- cb(body[cur_id]['usd'])
76
- })
77
- } else if (type === 'curs' && source === 'coingecko') {
78
- let curs = pair_or_pairs_or_cur_curs
79
- let cur_id_list = curs.map((cur) => coins_map[cur])
80
- let cur_id_map = _.fromPairs(
81
- curs.map((cur) => {
82
- return [coins_map[cur], cur]
83
- }),
84
- )
85
- let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
86
- limiter.process(needle.get, url, (err, res, body) => {
87
- if (err) {
88
- console.error(err)
24
+ cb(null)
89
25
  return
90
26
  }
91
- for (let cur_id in body) {
92
- let temp = body[cur_id]
93
- temp.USD = temp.usd
94
- temp.USDT = temp.usd
95
- delete temp['usd']
96
- result[cur_id_map[cur_id]] = body[cur_id]
27
+ if (body && Array.isArray(body)) {
28
+ for (let ticker of body) {
29
+ if (ticker.symbol.toUpperCase() === cur.toUpperCase()) {
30
+ cb(ticker.quotes.USD.price)
31
+ return
32
+ }
33
+ }
97
34
  }
98
- cb(result)
35
+ console.error('coinpaprika cur not found:', cur)
36
+ cb(null)
99
37
  })
100
- } else if (type === 'curs' && source === 'coinmarketcap') {
38
+ } else if (type === 'curs') {
101
39
  let curs = pair_or_pairs_or_cur_curs
102
- let cur_slug_list = curs.map((cur) => coins_map[cur])
103
- let url = `https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest?symbol=${String(curs)}&CMC_PRO_API_KEY=47a30ef1-a773-4da0-8ac5-69bce757fdfa`
40
+ let curs_upper = curs.map((c) => c.toUpperCase())
41
+ // Fetch all tickers and filter by symbol
42
+ let url = `https://api.coinpaprika.com/v1/tickers`
104
43
  limiter.process(needle.get, url, (err, res, body) => {
105
44
  if (err) {
106
45
  console.error(err)
107
46
  return
108
47
  }
109
- for (let cur in body.data) {
110
- let rates = body.data[cur]
111
- let temp = {}
112
- for (let rate of rates) {
113
- if (rate.name === coins_map[cur]) {
114
- temp = {
115
- USD: rate.quote.USD.price,
116
- USDT: rate.quote.USD.price,
48
+ if (body && Array.isArray(body)) {
49
+ for (let ticker of body) {
50
+ let symbol = ticker.symbol.toUpperCase()
51
+ if (curs_upper.includes(symbol)) {
52
+ let price = ticker.quotes.USD.price
53
+ result[symbol] = {
54
+ USD: price,
55
+ USDT: price,
117
56
  }
118
57
  }
119
58
  }
120
- result[cur] = temp
121
59
  }
122
60
  cb(result)
123
61
  })
@@ -267,26 +205,6 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
267
205
  console.error(pair, error)
268
206
  }
269
207
  })
270
- } else if (exchange === 'coingecko') {
271
- let pair = cur.toLowerCase() + 'usdt'
272
- let url = `https://api.coinex.com/v1/market/depth?market=${pair}&limit=20&merge=0`
273
- limiter.process(needle.get, url, (err, res, body) => {
274
- try {
275
- const asks = body.data.asks
276
- const bids = body.data.bids
277
- let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
278
- result[cur] = {
279
- USD: rate,
280
- USDT: rate,
281
- }
282
- let result_length = _.keys(result).length
283
- if (result_length === length) {
284
- cb(result)
285
- }
286
- } catch (error) {
287
- console.error(pair, error)
288
- }
289
- })
290
208
  } else if (exchange === 'coinstore') {
291
209
  if (cur === 'SWAGG') {
292
210
  cur = 'SWAGGERC20'
@@ -549,94 +467,101 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
549
467
  console.error(pair, error)
550
468
  }
551
469
  })
470
+ } else if (exchange === 'dex') {
471
+ if (!token_dict[cur]) {
472
+ console.error('dex token not in token_dict:', cur)
473
+ let result_length = _.keys(result).length
474
+ if (result_length === length) {
475
+ cb(result)
476
+ }
477
+ } else {
478
+ dex_rate(cur, (rate_result) => {
479
+ let rate = rate_result[cur]
480
+ result[cur] = {
481
+ USD: rate,
482
+ USDT: rate,
483
+ }
484
+ let result_length = _.keys(result).length
485
+ if (result_length === length) {
486
+ cb(result)
487
+ }
488
+ })
489
+ }
552
490
  }
553
491
  }
554
492
  } else {
555
- let quote_cur = 'USD',
556
- page = 1,
557
- per_page_nums = 250
558
- let get_coingecko_rate_page = (page, cb) => {
559
- let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
493
+ // Coinpaprika returns all tickers in one call (no pagination needed)
494
+ let get_coinpaprika_rates = (cb) => {
495
+ let url = 'https://api.coinpaprika.com/v1/tickers'
560
496
  limiter.process(needle.get, url, options, (err, res, body) => {
561
- console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
562
- if (body && body.length > 0) {
497
+ console.log('fetching coinpaprika rate: body length', _.get(body, 'length', body))
498
+ if (body && Array.isArray(body) && body.length > 0) {
563
499
  for (let coin of body) {
564
500
  let symbol = coin.symbol
565
- if (!symbol || !coin.current_price) {
501
+ if (!symbol || !coin.quotes || !coin.quotes.USD || !coin.quotes.USD.price) {
566
502
  continue
567
503
  }
568
504
  symbol = symbol.toUpperCase()
569
- if (symbol === 'BAT' && coin.id !== 'basic-attention-token') {
505
+ if (symbol === 'BAT' && coin.id !== 'bat-basic-attention-token') {
570
506
  continue
571
- } else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') {
507
+ } else if (symbol === 'SBTC' && coin.id !== 'sbtc-super-bitcoin') {
572
508
  continue
573
509
  }
574
510
  result[symbol] = {
575
- USD: parseFloat(coin.current_price),
576
- USDT: parseFloat(coin.current_price),
577
- '24h_change': parseFloat(coin.price_change_percentage_24h),
578
- '24h_volume_usd': parseFloat(coin.total_volume),
579
- rank: parseInt(coin.market_cap_rank) || 10000,
580
- market_cap: parseInt(coin.market_cap),
511
+ USD: parseFloat(coin.quotes.USD.price),
512
+ USDT: parseFloat(coin.quotes.USD.price),
513
+ '24h_change': parseFloat(coin.quotes.USD.percent_change_24h),
514
+ '24h_volume_usd': parseFloat(coin.quotes.USD.volume_24h),
515
+ rank: parseInt(coin.rank) || 10000,
516
+ market_cap: parseInt(coin.quotes.USD.market_cap),
581
517
  update_time: new Date(coin.last_updated),
582
518
  }
583
519
  }
584
- if (!result.BTC || !result.ETH) {
585
- get_crypto_rates(cb)
586
- } else {
520
+ // Calculate BTC/ETH rates for all currencies
521
+ if (result.BTC && result.ETH) {
587
522
  for (let cur in result) {
588
523
  result[cur].BTC = result[cur].USD / result.BTC.USD
589
524
  result[cur].ETH = result[cur].USD / result.ETH.USD
590
525
  }
591
- if (body.length < per_page_nums) {
592
- cb(result)
593
- } else {
594
- page++
595
- console.log('fetching coingecko rate: page', page, 'good')
596
- get_coingecko_rate_page(page, cb)
526
+ cb(result)
527
+ } else {
528
+ console.warn('BTC or ETH not found in Coinpaprika response, returning partial results')
529
+ for (let cur in result) {
530
+ if (result.BTC) result[cur].BTC = result[cur].USD / result.BTC.USD
531
+ if (result.ETH) result[cur].ETH = result[cur].USD / result.ETH.USD
597
532
  }
533
+ cb(result)
598
534
  }
599
535
  } else {
600
- setTimeout(() => {
601
- console.log('fetching coingecko rate: page', page, 'timeout')
602
- get_crypto_rates(cb)
603
- }, 60000)
536
+ // Handle error or empty response
537
+ if (err) {
538
+ console.error('Coinpaprika error:', err)
539
+ setTimeout(() => {
540
+ console.log('fetching coinpaprika rate: retry after error')
541
+ get_coinpaprika_rates(cb)
542
+ }, 60000)
543
+ } else if (body && body.error) {
544
+ // API error response
545
+ console.error('Coinpaprika API error:', body.error)
546
+ setTimeout(() => {
547
+ console.log('fetching coinpaprika rate: retry after API error')
548
+ get_coinpaprika_rates(cb)
549
+ }, 60000)
550
+ } else {
551
+ // Unknown error - retry
552
+ console.error('fetching coinpaprika rate: error', err || body)
553
+ setTimeout(() => {
554
+ get_coinpaprika_rates(cb)
555
+ }, 60000)
556
+ }
604
557
  }
605
558
  })
606
559
  }
607
- console.log('fetching coingecko rate: page', page, 'first')
608
- get_coingecko_rate_page(page, cb)
560
+ console.log('fetching coinpaprika rate: starting')
561
+ get_coinpaprika_rates(cb)
609
562
  }
610
563
  }
611
- coins_map = source === 'coingecko' ? process_coins_map_coingecko(coins_map_raw_coingecko) : process_coins_map_coinmarketcap(coins_map_raw_coinmarketcap)
612
564
  get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type)
613
565
  }
614
566
 
615
- // get_crypto_rates(
616
- // (res) => {
617
- // console.log(res)
618
- // },
619
- // {
620
- // ETHF: 'mexc',
621
- // },
622
- // 'curs_dict'
623
- // )
624
-
625
- // get_crypto_rates(
626
- // (res) => {
627
- // console.log(res)
628
- // },
629
- // ['AVAV'],
630
- // 'curs',
631
- // 'coinmarketcap'
632
- // )
633
-
634
- // get_coins_map_coingecko((res) => {
635
- // console.log(res.body)
636
- // })
637
-
638
- // get_coins_map_coinmarketcap((res) => {
639
- // console.log(res.body)
640
- // })
641
-
642
567
  module.exports = get_crypto_rates
package/lib/dex-rate.js CHANGED
@@ -3,31 +3,97 @@ const _ = require('lodash')
3
3
  const needle = require('needle')
4
4
 
5
5
  const token_dict = {
6
+ // BSC tokens (using token_price API)
6
7
  FREN: {
7
8
  address: '0xe6a768464b042a6d029394db1fdef360cb60bbeb',
8
9
  network: 'bsc',
10
+ type: 'token',
9
11
  },
10
12
  RWX: {
11
13
  address: '0xb0a1c87b890fe171371f81a59f7ed4636e82595d',
12
14
  network: 'bsc',
15
+ type: 'token',
13
16
  },
14
- USDT: {
15
- address: '0x55d398326f99059fF775485246999027B3197955',
16
- network: 'bsc',
17
+ // Pool-based tokens (using pools API)
18
+ ARMY: {
19
+ pool: '41524D5900000000000000000000000000000000.rGG3wQ4kUzd7Jnmk1n5NWPZjjut62kCBfC_XRP',
20
+ network: 'xrpl',
21
+ type: 'pool',
22
+ },
23
+ CCDOG: {
24
+ pool: '0x9ea8d5d68a4b6bd5cc5ed6b505f920ce6db9e93a',
25
+ network: 'eth',
26
+ type: 'pool',
27
+ },
28
+ JYAI: {
29
+ pool: '0x2623edc6008d057786a6bf9dd34185dcddebbf2f',
30
+ network: 'eth',
31
+ type: 'pool',
32
+ },
33
+ LBAI: {
34
+ pool: '0x65a33e74e18c99388f4989c296ab8e860219ee76',
35
+ network: 'eth',
36
+ type: 'pool',
37
+ },
38
+ MINDFAK: {
39
+ pool: '0x40c03fb952df5f4ec72414efb0b378d70cded263',
40
+ network: 'eth',
41
+ type: 'pool',
42
+ },
43
+ SHRAP: {
44
+ pool: '0x58f0caa7a5a3a92e33a348c282b4e415b84d863a',
45
+ network: 'avax',
46
+ type: 'pool',
17
47
  },
18
48
  }
19
49
 
20
50
  let dex_rate = function (cur, cb) {
21
- const { address, network } = token_dict[cur]
22
- const url = `https://api.geckoterminal.com/api/v2/simple/networks/${network}/token_price/${address}`
23
- needle.get(url, (res, error, body) => {
24
- if (body && body.data && body.data.id) {
25
- let rate = parseFloat(body.data.attributes.token_prices[address])
26
- cb({
27
- [cur]: rate,
28
- })
29
- }
30
- })
51
+ const token = token_dict[cur]
52
+ if (!token) {
53
+ console.error('dex_rate: token not found in token_dict:', cur)
54
+ cb(null)
55
+ return
56
+ }
57
+
58
+ if (token.type === 'pool') {
59
+ // Pool-based API
60
+ const url = `https://api.geckoterminal.com/api/v2/networks/${token.network}/pools/${token.pool}`
61
+ needle.get(url, (err, res, body) => {
62
+ try {
63
+ if (body && body.data && body.data.attributes) {
64
+ let rate = parseFloat(body.data.attributes.base_token_price_usd)
65
+ cb({
66
+ [cur]: rate,
67
+ })
68
+ } else {
69
+ console.error('dex_rate pool error:', cur, body)
70
+ cb(null)
71
+ }
72
+ } catch (error) {
73
+ console.error('dex_rate pool error:', cur, error)
74
+ cb(null)
75
+ }
76
+ })
77
+ } else {
78
+ // Token price API
79
+ const url = `https://api.geckoterminal.com/api/v2/simple/networks/${token.network}/token_price/${token.address}`
80
+ needle.get(url, (err, res, body) => {
81
+ try {
82
+ if (body && body.data && body.data.id) {
83
+ let rate = parseFloat(body.data.attributes.token_prices[token.address])
84
+ cb({
85
+ [cur]: rate,
86
+ })
87
+ } else {
88
+ console.error('dex_rate token error:', cur, body)
89
+ cb(null)
90
+ }
91
+ } catch (error) {
92
+ console.error('dex_rate token error:', cur, error)
93
+ cb(null)
94
+ }
95
+ })
96
+ }
31
97
  }
32
98
 
33
- module.exports = dex_rate
99
+ module.exports = { dex_rate, token_dict }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.42",
3
+ "version": "1.9.44",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {