@icgio/icg-utils 1.7.10 → 1.7.12

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.
@@ -0,0 +1,7 @@
1
+ .DS_Store
2
+ node_modules/
3
+ lib/test*
4
+ lib/bash.sh
5
+ *.key
6
+ test/
7
+ test*
@@ -0,0 +1,6 @@
1
+ {
2
+ "tabWidth": 2,
3
+ "semi": false,
4
+ "singleQuote": true,
5
+ "printWidth": 180
6
+ }
package/lib/coingecko.js CHANGED
@@ -11,14 +11,15 @@ const options = {
11
11
  // 'authority': 'api.coingecko.com',
12
12
  // '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',
13
13
  // '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',
14
- }
14
+ },
15
15
  }
16
16
 
17
17
  let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
18
18
  if (type === 'pair') {
19
19
  let pair = pair_or_pairs_or_cur_curs
20
20
  let [cur, base_cur] = parse_pair(pair)
21
- let cur_id = coins_map[cur], vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
21
+ let cur_id = coins_map[cur],
22
+ vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
22
23
  needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
23
24
  if (err) {
24
25
  console.error(pair, err)
@@ -29,10 +30,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
29
30
  })
30
31
  } else if (type === 'pairs') {
31
32
  let pairs = pair_or_pairs_or_cur_curs
32
- let cur_id_map = _.fromPairs(pairs.map(pair => {
33
- let [cur, base_cur] = parse_pair(pair)
34
- return [coins_map[cur], cur]
35
- }))
33
+ let cur_id_map = _.fromPairs(
34
+ pairs.map((pair) => {
35
+ let [cur, base_cur] = parse_pair(pair)
36
+ return [coins_map[cur], cur]
37
+ })
38
+ )
36
39
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
37
40
  needle.get(url, (err, res, body) => {
38
41
  if (err) {
@@ -50,13 +53,14 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
50
53
  for (let stable of stablecoins) {
51
54
  result[stable] = {
52
55
  USD: 1,
53
- USDT: 1
56
+ USDT: 1,
54
57
  }
55
58
  }
56
59
  cb(result)
57
60
  })
58
61
  } else if (type === 'cur') {
59
- let cur = pair_or_pairs_or_cur_curs, cur_id = coins_map[cur]
62
+ let cur = pair_or_pairs_or_cur_curs,
63
+ cur_id = coins_map[cur]
60
64
  needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
61
65
  if (err) {
62
66
  console.error(cur, err)
@@ -67,10 +71,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
67
71
  })
68
72
  } else if (type === 'curs') {
69
73
  let curs = pair_or_pairs_or_cur_curs
70
- let cur_id_list = curs.map(cur => coins_map[cur])
71
- let cur_id_map = _.fromPairs(curs.map(cur => {
72
- return [coins_map[cur], cur]
73
- }))
74
+ let cur_id_list = curs.map((cur) => coins_map[cur])
75
+ let cur_id_map = _.fromPairs(
76
+ curs.map((cur) => {
77
+ return [coins_map[cur], cur]
78
+ })
79
+ )
74
80
  let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
75
81
  needle.get(url, (err, res, body) => {
76
82
  if (err) {
@@ -88,13 +94,16 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
88
94
  for (let stable of stablecoins) {
89
95
  result[stable] = {
90
96
  USD: 1,
91
- USDT: 1
97
+ USDT: 1,
92
98
  }
93
99
  }
94
100
  cb(result)
95
101
  })
96
102
  } else {
97
- let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
103
+ let result = {},
104
+ quote_cur = 'USD',
105
+ page = 1,
106
+ per_page_nums = 250
98
107
  let get_coingecko_rate = (page, cb) => {
99
108
  let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
100
109
  needle.get(url, options, (err, res, body) => {
@@ -102,30 +111,27 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
102
111
  if (body && body.length > 0) {
103
112
  for (let coin of body) {
104
113
  let symbol = coin.symbol
105
- if (!symbol || !coin.current_price)
106
- continue
107
-
114
+ if (!symbol || !coin.current_price) continue
115
+
108
116
  symbol = symbol.toUpperCase()
109
-
110
- if (symbol === 'BAT' && coin.id !== 'basic-attention-token')
111
- continue
112
- else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin')
113
- continue
114
-
117
+
118
+ if (symbol === 'BAT' && coin.id !== 'basic-attention-token') continue
119
+ else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') continue
120
+
115
121
  result[symbol] = {
116
- 'USD': parseFloat(coin.current_price),
117
- 'USDT': parseFloat(coin.current_price),
122
+ USD: parseFloat(coin.current_price),
123
+ USDT: parseFloat(coin.current_price),
118
124
  '24h_change': parseFloat(coin.price_change_percentage_24h),
119
125
  '24h_volume_usd': parseFloat(coin.total_volume),
120
- 'rank': parseInt(coin.market_cap_rank) || 10000,
121
- 'market_cap': parseInt(coin.market_cap),
122
- 'update_time': new Date(coin.last_updated)
126
+ rank: parseInt(coin.market_cap_rank) || 10000,
127
+ market_cap: parseInt(coin.market_cap),
128
+ update_time: new Date(coin.last_updated),
123
129
  }
124
130
  }
125
131
  if (!result.BTC || !result.ETH) {
126
- setTimeout(() => {
132
+ setTimeout(() => {
127
133
  console.log('fetching coingecko rate: page', page, 'no-btc-eth')
128
- get_coingecko_rates(cb)
134
+ get_coingecko_rates(cb)
129
135
  }, 5000)
130
136
  } else {
131
137
  for (let cur in result) {
@@ -139,11 +145,11 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
139
145
  setTimeout(() => {
140
146
  console.log('fetching coingecko rate: page', page, 'good')
141
147
  get_coingecko_rate(page, cb)
142
- }, 3000)
148
+ }, 5000)
143
149
  }
144
150
  }
145
151
  } else {
146
- setTimeout(() => {
152
+ setTimeout(() => {
147
153
  console.log('fetching coingecko rate: page', page, 'timeout')
148
154
  get_coingecko_rates(cb)
149
155
  }, 60000)
@@ -165,19 +171,19 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
165
171
 
166
172
  // https://api.coingecko.com/api/v3/coins/list
167
173
 
168
- needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
169
- let list = body
174
+ // needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
175
+ // let list = body
170
176
 
171
- let map = {}
172
- for (let line of list) {
173
- let cur = line.symbol.toUpperCase()
174
- _.set(map, [cur], line.id)
175
- }
177
+ // let map = {}
178
+ // for (let line of list) {
179
+ // let cur = line.symbol.toUpperCase()
180
+ // _.set(map, [cur], line.id)
181
+ // }
176
182
 
177
- map['ETH'] = 'ethereum'
178
- map['MTR'] = 'meter-stable'
179
-
180
- console.log(JSON.stringify(map, null, 4))
181
- })
183
+ // map['ETH'] = 'ethereum'
184
+ // map['MTR'] = 'meter-stable'
185
+
186
+ // console.log(JSON.stringify(map, null, 4))
187
+ // })
182
188
 
183
189
  module.exports = get_coingecko_rates
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.10",
3
+ "version": "1.7.12",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {