@icgio/icg-utils 1.7.0 → 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 +75 -76
  2. package/package.json +1 -1
package/lib/coingecko.js CHANGED
@@ -14,7 +14,9 @@ const options = {
14
14
  }
15
15
 
16
16
  function determine_type(pair_or_pairs_or_cur_curs) {
17
- if (Array.isArray(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)) {
18
20
  let pairs_or_curs = pair_or_pairs_or_cur_curs[0]
19
21
  let [cur, base_cur] = parse_pair(pairs_or_curs)
20
22
  if (!base_cur || !cur) {
@@ -33,77 +35,73 @@ function determine_type(pair_or_pairs_or_cur_curs) {
33
35
  }
34
36
 
35
37
  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
38
+ let type = determine_type(pair_or_pairs_or_cur_curs)
39
+ if (type === 'pair') {
40
+ let pair = pair_or_pairs_or_cur_curs
41
+ let [cur, base_cur] = parse_pair(pair)
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()
43
+ needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
44
+ if (err) {
45
+ console.error(pair, err)
46
+ } else if (!body[cur_id]) {
47
+ console.error('coingecko cur_id incorrect:', pair, cur_id, vs_cur, err)
48
+ }
49
+ cb(body[cur_id][vs_cur])
50
+ })
51
+ } else if (type === 'pairs') {
52
+ let pairs = pair_or_pairs_or_cur_curs
53
+ let cur_id_map = _.fromPairs(pairs.map(pair => {
40
54
  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)
47
- }
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
- }
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
+ })
107
105
  } else {
108
106
  let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
109
107
  let get_coingecko_rate = (page, cb) => {
@@ -167,11 +165,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs) => {
167
165
  }
168
166
 
169
167
  // get_coingecko_rates((res) => {
170
- // console.log(res)
171
- // }, ['BTCUSD', 'ETHUSD'])
172
- // }, ['BTC', 'ETH'])
173
- // }, 'BTCUSDT')
174
- // }, 'BTC')
168
+ // // console.log(res)
169
+ // // }, ['BTCUSD', 'ETHUSD'])
170
+ // // }, ['BTC', 'ETH'])
171
+ // // }, 'BTCUSDT')
172
+ // // }, 'BTC')
173
+ // })
175
174
 
176
175
  // https://api.coingecko.com/api/v3/coins/list
177
176
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {