@icgio/icg-utils 1.7.12 → 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.
- package/constants/forex-rate.js +3 -3
- package/lib/coingecko.js +165 -150
- package/package.json +1 -1
- package/lib/coingecko-coins-map.js +0 -10921
package/constants/forex-rate.js
CHANGED
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,192 @@ const options = {
|
|
|
14
13
|
},
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
let
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} else if (!body[cur_id]) {
|
|
27
|
-
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)
|
|
28
25
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
39
|
+
let [cur, base_cur] = parse_pair(pair)
|
|
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)
|
|
47
|
+
}
|
|
48
|
+
cb(body[cur_id][vs_cur])
|
|
37
49
|
})
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
result[cur_id_map[cur_id]] = body[cur_id]
|
|
52
|
-
}
|
|
53
|
-
for (let stable of stablecoins) {
|
|
54
|
-
result[stable] = {
|
|
55
|
-
USD: 1,
|
|
56
|
-
USDT: 1,
|
|
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
|
|
57
63
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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]
|
|
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,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
cb(result)
|
|
78
79
|
})
|
|
79
|
-
)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
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,
|
|
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)
|
|
98
88
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
if (
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
|
115
134
|
|
|
116
|
-
|
|
135
|
+
symbol = symbol.toUpperCase()
|
|
117
136
|
|
|
118
|
-
|
|
119
|
-
|
|
137
|
+
if (symbol === 'BAT' && coin.id !== 'basic-attention-token') continue
|
|
138
|
+
else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') continue
|
|
120
139
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
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
|
+
}
|
|
140
149
|
}
|
|
141
|
-
if (
|
|
142
|
-
cb(result)
|
|
143
|
-
} else {
|
|
144
|
-
page++
|
|
150
|
+
if (!result.BTC || !result.ETH) {
|
|
145
151
|
setTimeout(() => {
|
|
146
|
-
console.log('fetching coingecko rate: page', page, '
|
|
147
|
-
|
|
152
|
+
console.log('fetching coingecko rate: page', page, 'no-btc-eth')
|
|
153
|
+
get_coingecko_rates(cb)
|
|
148
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
|
+
}
|
|
149
169
|
}
|
|
170
|
+
} else {
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
console.log('fetching coingecko rate: page', page, 'timeout')
|
|
173
|
+
get_coingecko_rates(cb)
|
|
174
|
+
}, 60000)
|
|
150
175
|
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}, 60000)
|
|
156
|
-
}
|
|
157
|
-
})
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
console.log('fetching coingecko rate: page', page, 'first')
|
|
179
|
+
get_coingecko_rate(page, cb)
|
|
158
180
|
}
|
|
159
|
-
|
|
160
|
-
|
|
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
|
+
})
|
|
161
189
|
}
|
|
162
190
|
}
|
|
163
191
|
|
|
164
192
|
// get_coingecko_rates((res) => {
|
|
165
193
|
// console.log(res)
|
|
166
|
-
//
|
|
167
|
-
// }, ['SWFTC', 'BTC', 'ETH'], 'curs')
|
|
168
|
-
// }, 'ETHUSDC', 'pair')
|
|
169
|
-
// }, 'BTC')
|
|
194
|
+
// // }, ['BTCUSD', 'ETHUSD'])
|
|
195
|
+
// // }, ['SWFTC', 'BTC', 'ETH'], 'curs')
|
|
196
|
+
// // }, 'ETHUSDC', 'pair')
|
|
197
|
+
// // }, 'BTC')
|
|
170
198
|
// })
|
|
171
199
|
|
|
172
|
-
//
|
|
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))
|
|
200
|
+
// get_coins_map((res) => {
|
|
201
|
+
// console.log(res)
|
|
187
202
|
// })
|
|
188
203
|
|
|
189
204
|
module.exports = get_coingecko_rates
|