@icgio/icg-utils 1.7.11 → 1.7.13

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
+ }
@@ -1,5 +1,5 @@
1
1
  module.exports = {
2
- USDCNY: 6.74,
3
- USDKRW: 1262.75,
4
- USDTHB: 34.2
2
+ USDCNY: 7.28,
3
+ USDKRW: 1416.53,
4
+ USDTHB: 37.76,
5
5
  }
package/lib/coingecko.js CHANGED
@@ -2,7 +2,6 @@ const _ = require('lodash')
2
2
 
3
3
  const needle = require('needle')
4
4
 
5
- const coins_map = require('./coingecko-coins-map.js')
6
5
  const parse_pair = require('./utils.js').parse_pair
7
6
  const stablecoins = require('../constants/stablecoins')
8
7
 
@@ -11,173 +10,195 @@ const options = {
11
10
  // 'authority': 'api.coingecko.com',
12
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',
13
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',
14
- }
13
+ },
15
14
  }
16
15
 
17
- let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
18
- if (type === 'pair') {
19
- let pair = pair_or_pairs_or_cur_curs
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()
22
- needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
23
- if (err) {
24
- console.error(pair, err)
25
- } else if (!body[cur_id]) {
26
- console.error('coingecko cur_id incorrect:', pair, cur_id, vs_cur, err)
16
+ let coins_map = {}
17
+ let get_coins_map = (cb) => {
18
+ needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
19
+ if (body && Array.isArray(body)) {
20
+ let list = body
21
+ let map = {}
22
+ for (let line of list) {
23
+ let cur = line.symbol.toUpperCase()
24
+ _.set(map, [cur], line.id)
27
25
  }
28
- cb(body[cur_id][vs_cur])
29
- })
30
- } else if (type === 'pairs') {
31
- let pairs = pair_or_pairs_or_cur_curs
32
- let cur_id_map = _.fromPairs(pairs.map(pair => {
26
+ map['ETH'] = 'ethereum'
27
+ map['MTR'] = 'meter-stable'
28
+ cb({ success: true, body })
29
+ } else {
30
+ cb({ success: false, body })
31
+ }
32
+ })
33
+ }
34
+
35
+ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs, type) => {
36
+ function get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
37
+ if (type === 'pair') {
38
+ let pair = pair_or_pairs_or_cur_curs
33
39
  let [cur, base_cur] = parse_pair(pair)
34
- return [coins_map[cur], cur]
35
- }))
36
- let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
37
- needle.get(url, (err, res, body) => {
38
- if (err) {
39
- console.error(err)
40
- return
41
- }
42
- let result = {}
43
- for (let cur_id in body) {
44
- let temp = body[cur_id]
45
- temp.USD = temp.usd
46
- temp.USDT = temp.usd
47
- delete temp['usd']
48
- result[cur_id_map[cur_id]] = body[cur_id]
49
- }
50
- for (let stable of stablecoins) {
51
- result[stable] = {
52
- USD: 1,
53
- USDT: 1
40
+ let cur_id = coins_map[cur],
41
+ vs_cur = stablecoins.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)
54
47
  }
55
- }
56
- cb(result)
57
- })
58
- } else if (type === 'cur') {
59
- let cur = pair_or_pairs_or_cur_curs, cur_id = coins_map[cur]
60
- needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
61
- if (err) {
62
- console.error(cur, err)
63
- } else if (!body[cur_id]) {
64
- console.error('coingecko cur_id incorrect:', cur, cur_id, 'usd', err)
65
- }
66
- cb(body[cur_id]['usd'])
67
- })
68
- } else if (type === 'curs') {
69
- 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 url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
75
- needle.get(url, (err, res, body) => {
76
- if (err) {
77
- console.error(err)
78
- return
79
- }
80
- let result = {}
81
- for (let cur_id in body) {
82
- let temp = body[cur_id]
83
- temp.USD = temp.usd
84
- temp.USDT = temp.usd
85
- delete temp['usd']
86
- result[cur_id_map[cur_id]] = body[cur_id]
87
- }
88
- for (let stable of stablecoins) {
89
- result[stable] = {
90
- USD: 1,
91
- USDT: 1
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(
53
+ pairs.map((pair) => {
54
+ let [cur, base_cur] = parse_pair(pair)
55
+ return [coins_map[cur], cur]
56
+ })
57
+ )
58
+ let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
59
+ needle.get(url, (err, res, body) => {
60
+ if (err) {
61
+ console.error(err)
62
+ return
92
63
  }
93
- }
94
- cb(result)
95
- })
96
- } else {
97
- let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
98
- let get_coingecko_rate = (page, cb) => {
99
- let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
100
- needle.get(url, options, (err, res, body) => {
101
- console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
102
- if (body && body.length > 0) {
103
- for (let coin of body) {
104
- let symbol = coin.symbol
105
- if (!symbol || !coin.current_price)
106
- continue
107
-
108
- 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
-
115
- result[symbol] = {
116
- 'USD': parseFloat(coin.current_price),
117
- 'USDT': parseFloat(coin.current_price),
118
- '24h_change': parseFloat(coin.price_change_percentage_24h),
119
- '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)
123
- }
64
+ let result = {}
65
+ for (let cur_id in body) {
66
+ let temp = body[cur_id]
67
+ temp.USD = temp.usd
68
+ temp.USDT = temp.usd
69
+ delete temp['usd']
70
+ result[cur_id_map[cur_id]] = body[cur_id]
71
+ }
72
+ for (let stable of stablecoins) {
73
+ result[stable] = {
74
+ USD: 1,
75
+ USDT: 1,
124
76
  }
125
- if (!result.BTC || !result.ETH) {
126
- setTimeout(() => {
127
- console.log('fetching coingecko rate: page', page, 'no-btc-eth')
128
- get_coingecko_rates(cb)
129
- }, 5000)
130
- } else {
131
- for (let cur in result) {
132
- result[cur].BTC = result[cur].USD / result.BTC.USD
133
- result[cur].ETH = result[cur].USD / result.ETH.USD
77
+ }
78
+ cb(result)
79
+ })
80
+ } else if (type === 'cur') {
81
+ let cur = pair_or_pairs_or_cur_curs,
82
+ cur_id = coins_map[cur]
83
+ needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
84
+ if (err) {
85
+ console.error(cur, err)
86
+ } else if (!body[cur_id]) {
87
+ console.error('coingecko cur_id incorrect:', cur, cur_id, 'usd', err)
88
+ }
89
+ cb(body[cur_id]['usd'])
90
+ })
91
+ } else if (type === 'curs') {
92
+ let curs = pair_or_pairs_or_cur_curs
93
+ let cur_id_list = curs.map((cur) => coins_map[cur])
94
+ let cur_id_map = _.fromPairs(
95
+ curs.map((cur) => {
96
+ return [coins_map[cur], cur]
97
+ })
98
+ )
99
+ let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
100
+ needle.get(url, (err, res, body) => {
101
+ if (err) {
102
+ console.error(err)
103
+ return
104
+ }
105
+ let result = {}
106
+ for (let cur_id in body) {
107
+ let temp = body[cur_id]
108
+ temp.USD = temp.usd
109
+ temp.USDT = temp.usd
110
+ delete temp['usd']
111
+ result[cur_id_map[cur_id]] = body[cur_id]
112
+ }
113
+ for (let stable of stablecoins) {
114
+ result[stable] = {
115
+ USD: 1,
116
+ USDT: 1,
117
+ }
118
+ }
119
+ cb(result)
120
+ })
121
+ } else {
122
+ let result = {},
123
+ quote_cur = 'USD',
124
+ page = 1,
125
+ per_page_nums = 250
126
+ let get_coingecko_rate = (page, cb) => {
127
+ let url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=' + quote_cur.toLowerCase() + '&per_page=' + per_page_nums + '&page=' + page
128
+ needle.get(url, options, (err, res, body) => {
129
+ console.log('fetching coingecko rate: body length', _.get(body, 'length', body))
130
+ if (body && body.length > 0) {
131
+ for (let coin of body) {
132
+ let symbol = coin.symbol
133
+ if (!symbol || !coin.current_price) continue
134
+
135
+ symbol = symbol.toUpperCase()
136
+
137
+ if (symbol === 'BAT' && coin.id !== 'basic-attention-token') continue
138
+ else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') continue
139
+
140
+ result[symbol] = {
141
+ USD: parseFloat(coin.current_price),
142
+ USDT: parseFloat(coin.current_price),
143
+ '24h_change': parseFloat(coin.price_change_percentage_24h),
144
+ '24h_volume_usd': parseFloat(coin.total_volume),
145
+ rank: parseInt(coin.market_cap_rank) || 10000,
146
+ market_cap: parseInt(coin.market_cap),
147
+ update_time: new Date(coin.last_updated),
148
+ }
134
149
  }
135
- if (body.length < per_page_nums) {
136
- cb(result)
137
- } else {
138
- page++
150
+ if (!result.BTC || !result.ETH) {
139
151
  setTimeout(() => {
140
- console.log('fetching coingecko rate: page', page, 'good')
141
- get_coingecko_rate(page, cb)
142
- }, 3000)
152
+ console.log('fetching coingecko rate: page', page, 'no-btc-eth')
153
+ get_coingecko_rates(cb)
154
+ }, 5000)
155
+ } else {
156
+ for (let cur in result) {
157
+ result[cur].BTC = result[cur].USD / result.BTC.USD
158
+ result[cur].ETH = result[cur].USD / result.ETH.USD
159
+ }
160
+ if (body.length < per_page_nums) {
161
+ cb(result)
162
+ } else {
163
+ page++
164
+ setTimeout(() => {
165
+ console.log('fetching coingecko rate: page', page, 'good')
166
+ get_coingecko_rate(page, cb)
167
+ }, 5000)
168
+ }
143
169
  }
170
+ } else {
171
+ setTimeout(() => {
172
+ console.log('fetching coingecko rate: page', page, 'timeout')
173
+ get_coingecko_rates(cb)
174
+ }, 60000)
144
175
  }
145
- } else {
146
- setTimeout(() => {
147
- console.log('fetching coingecko rate: page', page, 'timeout')
148
- get_coingecko_rates(cb)
149
- }, 60000)
150
- }
151
- })
176
+ })
177
+ }
178
+ console.log('fetching coingecko rate: page', page, 'first')
179
+ get_coingecko_rate(page, cb)
152
180
  }
153
- console.log('fetching coingecko rate: page', page, 'first')
154
- get_coingecko_rate(page, cb)
181
+ }
182
+ if (_.isEmpty(coins_map)) {
183
+ get_coins_map((res) => {
184
+ if (res.success) {
185
+ coins_map = res.body
186
+ }
187
+ get_coingecko_rates_base(cb, pair_or_pairs_or_cur_curs, type)
188
+ })
155
189
  }
156
190
  }
157
191
 
158
192
  // get_coingecko_rates((res) => {
159
193
  // console.log(res)
160
- // // }, ['BTCUSD', 'ETHUSD'])
161
- // }, ['SWFTC', 'BTC', 'ETH'], 'curs')
162
- // }, 'ETHUSDC', 'pair')
163
- // }, 'BTC')
194
+ // // }, ['BTCUSD', 'ETHUSD'])
195
+ // // }, ['SWFTC', 'BTC', 'ETH'], 'curs')
196
+ // // }, 'ETHUSDC', 'pair')
197
+ // // }, 'BTC')
164
198
  // })
165
199
 
166
- // https://api.coingecko.com/api/v3/coins/list
167
-
168
- // needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
169
- // let list = body
170
-
171
- // let map = {}
172
- // for (let line of list) {
173
- // let cur = line.symbol.toUpperCase()
174
- // _.set(map, [cur], line.id)
175
- // }
176
-
177
- // map['ETH'] = 'ethereum'
178
- // map['MTR'] = 'meter-stable'
179
-
180
- // console.log(JSON.stringify(map, null, 4))
200
+ // get_coins_map((res) => {
201
+ // console.log(res)
181
202
  // })
182
203
 
183
204
  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.11",
3
+ "version": "1.7.13",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {