@icgio/icg-utils 1.6.31 → 1.7.1

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.
Files changed (2) hide show
  1. package/lib/coingecko.js +149 -75
  2. package/package.json +2 -2
package/lib/coingecko.js CHANGED
@@ -1,101 +1,175 @@
1
+ const _ = require('lodash')
2
+
1
3
  const needle = require('needle')
2
4
 
3
5
  const coins_map = require('./coingecko-coins-map.js')
4
6
  const parse_pair = require('./utils.js').parse_pair
5
7
 
6
- let get_coingecko_rates = (cb, pair) => {
7
- if (pair) {
8
+ const options = {
9
+ headers: {
10
+ // 'authority': 'api.coingecko.com',
11
+ // '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',
12
+ // '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',
13
+ }
14
+ }
15
+
16
+ function determine_type(pair_or_pairs_or_cur_curs) {
17
+ if (!pair_or_pairs_or_cur_curs || pair_or_pairs_or_cur_curs.length === 0) {
18
+ return 'null'
19
+ } else if (Array.isArray(pair_or_pairs_or_cur_curs)) {
20
+ let pairs_or_curs = pair_or_pairs_or_cur_curs[0]
21
+ let [cur, base_cur] = parse_pair(pairs_or_curs)
22
+ if (!base_cur || !cur) {
23
+ return 'curs'
24
+ } else {
25
+ return 'pairs'
26
+ }
27
+ } else {
28
+ let [cur, base_cur] = parse_pair(pair_or_pairs_or_cur_curs)
29
+ if (!base_cur || !cur) {
30
+ return 'cur'
31
+ } else {
32
+ return 'pair'
33
+ }
34
+ }
35
+ }
36
+
37
+ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs) => {
38
+ let type = determine_type(pair_or_pairs_or_cur_curs)
39
+ if (type === 'pair') {
40
+ let pair = pair_or_pairs_or_cur_curs
8
41
  let [cur, base_cur] = parse_pair(pair)
9
42
  let cur_id = coins_map[cur], vs_cur = ['USDT', 'TUSD', 'PAX', 'BUSD', 'USDK', 'USDC', 'HUSD', 'DAI'].indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
10
43
  needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
11
44
  if (err) {
12
45
  console.error(pair, err)
13
- return
14
46
  } else if (!body[cur_id]) {
15
47
  console.error('coingecko cur_id incorrect:', pair, cur_id, vs_cur, err)
16
- return
17
48
  }
18
49
  cb(body[cur_id][vs_cur])
19
50
  })
20
- return
21
- }
22
- let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
23
- let get_coingecko_rate = (page, cb) => {
24
- needle.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page, (err, res, body) => {
25
- if (body && body.length > 0) {
26
- for (let coin of body) {
27
- let symbol = coin.symbol
28
- if (!symbol || !coin.current_price)
29
- continue
30
-
31
- symbol = symbol.toUpperCase()
32
-
33
- if (symbol === 'BAT' && coin.id !== 'basic-attention-token')
34
- continue
35
- else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin')
36
- continue
37
- // else if (coin.symbol === 'BTG' && coin.id === 'bitgem')
38
- // continue
39
- // else if (coin.symbol === 'BTM' && coin.id === 'bitmark')
40
- // coin.symbol = 'BITM'
41
- // else if (coin.symbol === 'CMT' && coin.id === 'comet')
42
- // continue
43
- // else if (coin.symbol === 'ICN' && coin.id === 'icoin')
44
- // continue
45
- // else if (coin.symbol === 'KNC' && coin.id === 'kingn-coin')
46
- // continue
47
- // else if (coin.symbol === 'MIOTA')
48
- // coin.symbol = 'IOTA'
49
- // else if (coin.symbol === 'PAI' && coin.id === 'project-pai')
50
- // coin.symbol = 'PPAI'
51
- // else if (coin.symbol === 'RCN' && coin.id === 'rcoin')
52
- // continue
53
- // else if (coin.symbol === 'ST' && coin.id === 'simple-token')
54
- // coin.symbol = 'OST'
55
- // else if (coin.symbol === 'TIX' && coin.id === 'tickets')
56
- // continue
57
- // else if (coin.symbol === 'BLZ' && coin.id === 'blazecoin')
58
- // continue
59
- // else if (coin.symbol === 'USNBT')
60
- // coin.symbol = 'NBT'
61
- // if (result[coin.symbol]) {
62
- // console.log(coin.symbol)
63
- // }
64
-
65
- result[symbol] = {
66
- 'USD': parseFloat(coin.current_price),
67
- 'USDT': parseFloat(coin.current_price),
68
- '24h_change': parseFloat(coin.price_change_percentage_24h),
69
- '24h_volume_usd': parseFloat(coin.total_volume),
70
- 'rank': parseInt(coin.market_cap_rank) || 10000,
71
- 'market_cap': parseInt(coin.market_cap),
72
- 'update_time': new Date(coin.last_updated)
73
- }
74
- }
75
- if (!result.BTC || !result.ETH) {
76
- setTimeout(() => { get_coingecko_rates(cb) }, 5000)
77
- } else {
78
- for (let cur in result) {
79
- result[cur].BTC = result[cur].USD / result.BTC.USD
80
- result[cur].ETH = result[cur].USD / result.ETH.USD
51
+ } else if (type === 'pairs') {
52
+ let pairs = pair_or_pairs_or_cur_curs
53
+ let cur_id_map = _.fromPairs(pairs.map(pair => {
54
+ let [cur, base_cur] = parse_pair(pair)
55
+ return [coins_map[cur], cur]
56
+ }))
57
+ 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) => {
59
+ if (err) {
60
+ console.error(err)
61
+ return
62
+ }
63
+ let result = {}
64
+ for (let cur_id in body) {
65
+ let temp = body[cur_id]
66
+ temp.USD = temp.usd
67
+ temp.USDT = temp.usd
68
+ delete temp['usd']
69
+ result[cur_id_map[cur_id]] = body[cur_id]
70
+ }
71
+ cb(result)
72
+ })
73
+ } else if (type === 'cur') {
74
+ let cur = pair_or_pairs_or_cur_curs, cur_id = coins_map[cur]
75
+ needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
76
+ if (err) {
77
+ console.error(pair, err)
78
+ } else if (!body[cur_id]) {
79
+ console.error('coingecko cur_id incorrect:', pair, cur_id, 'usd', err)
80
+ }
81
+ cb(body[cur_id]['usd'])
82
+ })
83
+ } else if (type === 'curs') {
84
+ let curs = pair_or_pairs_or_cur_curs
85
+ let cur_id_list = curs.map(cur => coins_map[cur])
86
+ let cur_id_map = _.fromPairs(curs.map(cur => {
87
+ return [coins_map[cur], cur]
88
+ }))
89
+ let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
90
+ needle.get(url, (err, res, body) => {
91
+ if (err) {
92
+ console.error(err)
93
+ return
94
+ }
95
+ let result = {}
96
+ for (let cur_id in body) {
97
+ let temp = body[cur_id]
98
+ temp.USD = temp.usd
99
+ temp.USDT = temp.usd
100
+ delete temp['usd']
101
+ result[cur_id_map[cur_id]] = body[cur_id]
102
+ }
103
+ cb(result)
104
+ })
105
+ } else {
106
+ let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
107
+ let get_coingecko_rate = (page, cb) => {
108
+ let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
109
+ needle.get(url, options, (err, res, body) => {
110
+ console.log(body.length)
111
+ if (body && body.length > 0) {
112
+ for (let coin of body) {
113
+ let symbol = coin.symbol
114
+ if (!symbol || !coin.current_price)
115
+ continue
116
+
117
+ symbol = symbol.toUpperCase()
118
+
119
+ if (symbol === 'BAT' && coin.id !== 'basic-attention-token')
120
+ continue
121
+ else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin')
122
+ continue
123
+
124
+ result[symbol] = {
125
+ 'USD': parseFloat(coin.current_price),
126
+ 'USDT': parseFloat(coin.current_price),
127
+ '24h_change': parseFloat(coin.price_change_percentage_24h),
128
+ '24h_volume_usd': parseFloat(coin.total_volume),
129
+ 'rank': parseInt(coin.market_cap_rank) || 10000,
130
+ 'market_cap': parseInt(coin.market_cap),
131
+ 'update_time': new Date(coin.last_updated)
132
+ }
81
133
  }
82
- if (body.length < per_page_nums) {
83
- cb(result)
134
+ if (!result.BTC || !result.ETH) {
135
+ setTimeout(() => {
136
+ console.log('fetching coingecko rate: page', page, 'no-btc-eth')
137
+ get_coingecko_rates(cb)
138
+ }, 5000)
84
139
  } else {
85
- page++
86
- get_coingecko_rate(page, cb)
140
+ for (let cur in result) {
141
+ result[cur].BTC = result[cur].USD / result.BTC.USD
142
+ result[cur].ETH = result[cur].USD / result.ETH.USD
143
+ }
144
+ if (body.length < per_page_nums) {
145
+ cb(result)
146
+ } else {
147
+ page++
148
+ setTimeout(() => {
149
+ console.log('fetching coingecko rate: page', page, 'good')
150
+ get_coingecko_rate(page, cb)
151
+ }, 2000)
152
+ }
87
153
  }
154
+ } else {
155
+ setTimeout(() => {
156
+ console.log('fetching coingecko rate: page', page, 'timout')
157
+ get_coingecko_rates(cb)
158
+ }, 10000)
88
159
  }
89
- } else {
90
- setTimeout(() => { get_coingecko_rates(cb) }, 5000)
91
- }
92
- })
160
+ })
161
+ }
162
+ console.log('fetching coingecko rate: page', page, 'first')
163
+ get_coingecko_rate(page, cb)
93
164
  }
94
- get_coingecko_rate(page, cb)
95
165
  }
96
166
 
97
167
  // get_coingecko_rates((res) => {
98
- // console.log(res)
168
+ // // console.log(res)
169
+ // // }, ['BTCUSD', 'ETHUSD'])
170
+ // // }, ['BTC', 'ETH'])
171
+ // // }, 'BTCUSDT')
172
+ // // }, 'BTC')
99
173
  // })
100
174
 
101
175
  // https://api.coingecko.com/api/v3/coins/list
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.6.31",
3
+ "version": "1.7.1",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -24,7 +24,7 @@
24
24
  "external-ip": "^2.3.1",
25
25
  "htmlparser2": "^3.10.1",
26
26
  "influx": "^5.6.3",
27
- "lodash": "^4.17.20",
27
+ "lodash": "^4.17.21",
28
28
  "mailgun-js": "^0.18.1",
29
29
  "needle": "^2.6.0",
30
30
  "nodemailer": "^4.7.0",