@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.
- package/constants/stablecoins.js +1 -0
- package/index.js +4 -1
- package/jsconfig.json +7 -0
- package/lib/coingecko.js +76 -76
- package/package.json +1 -1
|
@@ -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
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 (
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
|