@icgio/icg-utils 1.7.12 → 1.7.14

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