@icgio/icg-utils 1.7.11 → 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/.prettierignore +7 -0
- package/.prettierrc.json +6 -0
- package/constants/forex-rate.js +3 -3
- package/lib/coingecko.js +170 -149
- package/package.json +1 -1
- package/lib/coingecko-coins-map.js +0 -10921
package/.prettierignore
ADDED
package/.prettierrc.json
ADDED
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
|
|
|
@@ -11,173 +10,195 @@ const options = {
|
|
|
11
10
|
// 'authority': 'api.coingecko.com',
|
|
12
11
|
// 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
|
|
13
12
|
// 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
14
|
-
}
|
|
13
|
+
},
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
let
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
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)
|
|
27
25
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
33
39
|
let [cur, base_cur] = parse_pair(pair)
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
let result = {}
|
|
43
|
-
for (let cur_id in body) {
|
|
44
|
-
let temp = body[cur_id]
|
|
45
|
-
temp.USD = temp.usd
|
|
46
|
-
temp.USDT = temp.usd
|
|
47
|
-
delete temp['usd']
|
|
48
|
-
result[cur_id_map[cur_id]] = body[cur_id]
|
|
49
|
-
}
|
|
50
|
-
for (let stable of stablecoins) {
|
|
51
|
-
result[stable] = {
|
|
52
|
-
USD: 1,
|
|
53
|
-
USDT: 1
|
|
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)
|
|
54
47
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
let cur_id_list = curs.map(cur => coins_map[cur])
|
|
71
|
-
let cur_id_map = _.fromPairs(curs.map(cur => {
|
|
72
|
-
return [coins_map[cur], cur]
|
|
73
|
-
}))
|
|
74
|
-
let url = `https://api.coingecko.com/api/v3/simple/price?ids=${String(_.keys(cur_id_map))}&vs_currencies=usd`
|
|
75
|
-
needle.get(url, (err, res, body) => {
|
|
76
|
-
if (err) {
|
|
77
|
-
console.error(err)
|
|
78
|
-
return
|
|
79
|
-
}
|
|
80
|
-
let result = {}
|
|
81
|
-
for (let cur_id in body) {
|
|
82
|
-
let temp = body[cur_id]
|
|
83
|
-
temp.USD = temp.usd
|
|
84
|
-
temp.USDT = temp.usd
|
|
85
|
-
delete temp['usd']
|
|
86
|
-
result[cur_id_map[cur_id]] = body[cur_id]
|
|
87
|
-
}
|
|
88
|
-
for (let stable of stablecoins) {
|
|
89
|
-
result[stable] = {
|
|
90
|
-
USD: 1,
|
|
91
|
-
USDT: 1
|
|
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(
|
|
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
|
|
92
63
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (!symbol || !coin.current_price)
|
|
106
|
-
continue
|
|
107
|
-
|
|
108
|
-
symbol = symbol.toUpperCase()
|
|
109
|
-
|
|
110
|
-
if (symbol === 'BAT' && coin.id !== 'basic-attention-token')
|
|
111
|
-
continue
|
|
112
|
-
else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin')
|
|
113
|
-
continue
|
|
114
|
-
|
|
115
|
-
result[symbol] = {
|
|
116
|
-
'USD': parseFloat(coin.current_price),
|
|
117
|
-
'USDT': parseFloat(coin.current_price),
|
|
118
|
-
'24h_change': parseFloat(coin.price_change_percentage_24h),
|
|
119
|
-
'24h_volume_usd': parseFloat(coin.total_volume),
|
|
120
|
-
'rank': parseInt(coin.market_cap_rank) || 10000,
|
|
121
|
-
'market_cap': parseInt(coin.market_cap),
|
|
122
|
-
'update_time': new Date(coin.last_updated)
|
|
123
|
-
}
|
|
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,
|
|
124
76
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
77
|
+
}
|
|
78
|
+
cb(result)
|
|
79
|
+
})
|
|
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)
|
|
88
|
+
}
|
|
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
|
|
134
|
+
|
|
135
|
+
symbol = symbol.toUpperCase()
|
|
136
|
+
|
|
137
|
+
if (symbol === 'BAT' && coin.id !== 'basic-attention-token') continue
|
|
138
|
+
else if (symbol === 'SBTC' && coin.id !== 'super-bitcoin') continue
|
|
139
|
+
|
|
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
|
+
}
|
|
134
149
|
}
|
|
135
|
-
if (
|
|
136
|
-
cb(result)
|
|
137
|
-
} else {
|
|
138
|
-
page++
|
|
150
|
+
if (!result.BTC || !result.ETH) {
|
|
139
151
|
setTimeout(() => {
|
|
140
|
-
console.log('fetching coingecko rate: page', page, '
|
|
141
|
-
|
|
142
|
-
},
|
|
152
|
+
console.log('fetching coingecko rate: page', page, 'no-btc-eth')
|
|
153
|
+
get_coingecko_rates(cb)
|
|
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
|
+
}
|
|
143
169
|
}
|
|
170
|
+
} else {
|
|
171
|
+
setTimeout(() => {
|
|
172
|
+
console.log('fetching coingecko rate: page', page, 'timeout')
|
|
173
|
+
get_coingecko_rates(cb)
|
|
174
|
+
}, 60000)
|
|
144
175
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}, 60000)
|
|
150
|
-
}
|
|
151
|
-
})
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
console.log('fetching coingecko rate: page', page, 'first')
|
|
179
|
+
get_coingecko_rate(page, cb)
|
|
152
180
|
}
|
|
153
|
-
|
|
154
|
-
|
|
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
|
+
})
|
|
155
189
|
}
|
|
156
190
|
}
|
|
157
191
|
|
|
158
192
|
// get_coingecko_rates((res) => {
|
|
159
193
|
// console.log(res)
|
|
160
|
-
//
|
|
161
|
-
// }, ['SWFTC', 'BTC', 'ETH'], 'curs')
|
|
162
|
-
// }, 'ETHUSDC', 'pair')
|
|
163
|
-
// }, 'BTC')
|
|
194
|
+
// // }, ['BTCUSD', 'ETHUSD'])
|
|
195
|
+
// // }, ['SWFTC', 'BTC', 'ETH'], 'curs')
|
|
196
|
+
// // }, 'ETHUSDC', 'pair')
|
|
197
|
+
// // }, 'BTC')
|
|
164
198
|
// })
|
|
165
199
|
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
// needle.get('https://api.coingecko.com/api/v3/coins/list', (err, res, body) => {
|
|
169
|
-
// let list = body
|
|
170
|
-
|
|
171
|
-
// let map = {}
|
|
172
|
-
// for (let line of list) {
|
|
173
|
-
// let cur = line.symbol.toUpperCase()
|
|
174
|
-
// _.set(map, [cur], line.id)
|
|
175
|
-
// }
|
|
176
|
-
|
|
177
|
-
// map['ETH'] = 'ethereum'
|
|
178
|
-
// map['MTR'] = 'meter-stable'
|
|
179
|
-
|
|
180
|
-
// console.log(JSON.stringify(map, null, 4))
|
|
200
|
+
// get_coins_map((res) => {
|
|
201
|
+
// console.log(res)
|
|
181
202
|
// })
|
|
182
203
|
|
|
183
204
|
module.exports = get_coingecko_rates
|