@icgio/icg-utils 1.7.0 → 1.7.2

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 @@
1
+ module.exports = ['USDT', 'TUSD', 'PAX', 'BUSD', 'USDK', 'USDC', 'HUSD', 'DAI', 'IUSD', 'USDD', 'FEI']
package/index.js CHANGED
@@ -9,5 +9,8 @@ module.exports = {
9
9
  mail: require('./lib/mail.js'),
10
10
  rate_limiter: require('./lib/rate-limiter.js'),
11
11
  utils: require('./lib/utils.js'),
12
- wechat: require('./lib/wechat-bot-client.js')
12
+ wechat: require('./lib/wechat-bot-client.js'),
13
+ constants: {
14
+ stablecoins: require('./constants/stablecoins')
15
+ }
13
16
  }
package/jsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "target": "es6"
5
+ },
6
+ "exclude": ["node_modules", "**/node_modules/*"]
7
+ }
package/lib/coingecko.js CHANGED
@@ -4,6 +4,7 @@ const needle = require('needle')
4
4
 
5
5
  const coins_map = require('./coingecko-coins-map.js')
6
6
  const parse_pair = require('./utils.js').parse_pair
7
+ const stablecoins = require('../constants/stablecoins')
7
8
 
8
9
  const options = {
9
10
  headers: {
@@ -14,7 +15,9 @@ const options = {
14
15
  }
15
16
 
16
17
  function determine_type(pair_or_pairs_or_cur_curs) {
17
- if (Array.isArray(pair_or_pairs_or_cur_curs)) {
18
+ if (!pair_or_pairs_or_cur_curs || pair_or_pairs_or_cur_curs.length === 0) {
19
+ return 'null'
20
+ } else if (Array.isArray(pair_or_pairs_or_cur_curs)) {
18
21
  let pairs_or_curs = pair_or_pairs_or_cur_curs[0]
19
22
  let [cur, base_cur] = parse_pair(pairs_or_curs)
20
23
  if (!base_cur || !cur) {
@@ -33,77 +36,73 @@ function determine_type(pair_or_pairs_or_cur_curs) {
33
36
  }
34
37
 
35
38
  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
39
+ let type = determine_type(pair_or_pairs_or_cur_curs)
40
+ if (type === 'pair') {
41
+ let pair = pair_or_pairs_or_cur_curs
42
+ let [cur, base_cur] = parse_pair(pair)
43
+ let cur_id = coins_map[cur], vs_cur = stablecoins.indexOf(base_cur) > -1 ? 'usd' : base_cur.toLowerCase()
44
+ needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
45
+ if (err) {
46
+ console.error(pair, err)
47
+ } else if (!body[cur_id]) {
48
+ console.error('coingecko cur_id incorrect:', pair, cur_id, vs_cur, err)
49
+ }
50
+ cb(body[cur_id][vs_cur])
51
+ })
52
+ } else if (type === 'pairs') {
53
+ let pairs = pair_or_pairs_or_cur_curs
54
+ let cur_id_map = _.fromPairs(pairs.map(pair => {
40
55
  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
- }
56
+ return [coins_map[cur], cur]
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
63
+ }
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
+ cb(result)
73
+ })
74
+ } else if (type === 'cur') {
75
+ let cur = pair_or_pairs_or_cur_curs, cur_id = coins_map[cur]
76
+ needle.get(`https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=usd`, (err, res, body) => {
77
+ if (err) {
78
+ console.error(cur, err)
79
+ } else if (!body[cur_id]) {
80
+ console.error('coingecko cur_id incorrect:', cur, cur_id, 'usd', err)
81
+ }
82
+ cb(body[cur_id]['usd'])
83
+ })
84
+ } else if (type === 'curs') {
85
+ let curs = pair_or_pairs_or_cur_curs
86
+ let cur_id_list = curs.map(cur => coins_map[cur])
87
+ let cur_id_map = _.fromPairs(curs.map(cur => {
88
+ return [coins_map[cur], cur]
89
+ }))
90
+ let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
91
+ needle.get(url, (err, res, body) => {
92
+ if (err) {
93
+ console.error(err)
94
+ return
95
+ }
96
+ let result = {}
97
+ for (let cur_id in body) {
98
+ let temp = body[cur_id]
99
+ temp.USD = temp.usd
100
+ temp.USDT = temp.usd
101
+ delete temp['usd']
102
+ result[cur_id_map[cur_id]] = body[cur_id]
103
+ }
104
+ cb(result)
105
+ })
107
106
  } else {
108
107
  let result = {}, quote_cur = 'USD', page = 1, per_page_nums = 250
109
108
  let get_coingecko_rate = (page, cb) => {
@@ -167,11 +166,12 @@ let get_coingecko_rates = (cb, pair_or_pairs_or_cur_curs) => {
167
166
  }
168
167
 
169
168
  // get_coingecko_rates((res) => {
170
- // console.log(res)
171
- // }, ['BTCUSD', 'ETHUSD'])
172
- // }, ['BTC', 'ETH'])
173
- // }, 'BTCUSDT')
174
- // }, 'BTC')
169
+ // // console.log(res)
170
+ // // }, ['BTCUSD', 'ETHUSD'])
171
+ // // }, ['BTC', 'ETH'])
172
+ // // }, 'BTCUSDT')
173
+ // // }, 'BTC')
174
+ // })
175
175
 
176
176
  // https://api.coingecko.com/api/v3/coins/list
177
177
 
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.2",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {