@icgio/icg-utils 1.6.31 → 1.7.0

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