@icgio/icg-utils 1.8.5 → 1.9.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.
- package/index.js +1 -0
- package/lib/circular-buffer.js +42 -0
- package/lib/crypto-rates.js +3 -3
- package/lib/forex-rate.js +2 -2
- package/lib/utils.js +5 -5
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
class CircularBuffer {
|
|
2
|
+
constructor(size) {
|
|
3
|
+
this.size = size
|
|
4
|
+
this.buffer = new Array(size)
|
|
5
|
+
this.index = 0
|
|
6
|
+
this.count = 0
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
push(value) {
|
|
10
|
+
this.buffer[this.index] = value
|
|
11
|
+
this.index = (this.index + 1) % this.size
|
|
12
|
+
if (this.count < this.size) {
|
|
13
|
+
this.count++
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get_values() {
|
|
18
|
+
return this.buffer.slice(0, this.count)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
min() {
|
|
22
|
+
return Math.min(...this.getValues())
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
max() {
|
|
26
|
+
return Math.max(...this.getValues())
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
mean() {
|
|
30
|
+
const values = this.getValues()
|
|
31
|
+
return values.reduce((sum, val) => sum + val, 0) / values.length
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
std() {
|
|
35
|
+
const values = this.getValues()
|
|
36
|
+
const mean = this.mean()
|
|
37
|
+
const variance = values.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / values.length
|
|
38
|
+
return Math.sqrt(variance)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
module.exports = CircularBuffer
|
package/lib/crypto-rates.js
CHANGED
|
@@ -69,9 +69,9 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
|
|
|
69
69
|
function get_crypto_rates_base(cb, pair_or_pairs_or_cur_curs, type) {
|
|
70
70
|
if (type === 'pair') {
|
|
71
71
|
let pair = pair_or_pairs_or_cur_curs
|
|
72
|
-
let [cur,
|
|
72
|
+
let [cur, quote_cur] = parse_pair(pair)
|
|
73
73
|
let cur_id = coins_map[cur],
|
|
74
|
-
vs_cur = stablecoins.indexOf(
|
|
74
|
+
vs_cur = stablecoins.indexOf(quote_cur) > -1 ? 'usd' : quote_cur.toLowerCase()
|
|
75
75
|
limiter.process(needle.get, `https://api.coingecko.com/api/v3/simple/price?ids=${cur_id}&vs_currencies=${vs_cur}`, (err, res, body) => {
|
|
76
76
|
if (err) {
|
|
77
77
|
console.error(pair, err)
|
|
@@ -84,7 +84,7 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
|
|
|
84
84
|
let pairs = pair_or_pairs_or_cur_curs
|
|
85
85
|
let cur_id_map = _.fromPairs(
|
|
86
86
|
pairs.map((pair) => {
|
|
87
|
-
let [cur,
|
|
87
|
+
let [cur, quote_cur] = parse_pair(pair)
|
|
88
88
|
return [coins_map[cur], cur]
|
|
89
89
|
})
|
|
90
90
|
)
|
package/lib/forex-rate.js
CHANGED
|
@@ -2,10 +2,10 @@ const needle = require('needle')
|
|
|
2
2
|
const utils = require('./utils.js')
|
|
3
3
|
|
|
4
4
|
let get_forex_rate = function (pair, cb) {
|
|
5
|
-
let [cur,
|
|
5
|
+
let [cur, quote_cur] = utils.parse_pair(pair)
|
|
6
6
|
needle.get(`https://api.exchangerate.host/latest?base=USD`, (err, res, body) => {
|
|
7
7
|
if (body.rates) {
|
|
8
|
-
cb(body.rates[
|
|
8
|
+
cb(body.rates[quote_cur])
|
|
9
9
|
}
|
|
10
10
|
})
|
|
11
11
|
}
|
package/lib/utils.js
CHANGED
|
@@ -793,8 +793,8 @@ module.exports.parse_pair = function (pair) {
|
|
|
793
793
|
}
|
|
794
794
|
|
|
795
795
|
module.exports.reverse_pair = function (pair) {
|
|
796
|
-
let [cur,
|
|
797
|
-
return
|
|
796
|
+
let [cur, quote_cur] = module.exports.parse_pair(pair)
|
|
797
|
+
return quote_cur + cur
|
|
798
798
|
}
|
|
799
799
|
|
|
800
800
|
module.exports.cur_dict_to_pair_list = function (cur_dict) {
|
|
@@ -816,9 +816,9 @@ module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
|
816
816
|
for (let exchange in exchange_basecur_curs) {
|
|
817
817
|
_.set(exchange_pairs, [exchange], [])
|
|
818
818
|
_.set(exchange_curs, [exchange], [])
|
|
819
|
-
for (let
|
|
820
|
-
exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][
|
|
821
|
-
exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_basecur_curs[exchange][
|
|
819
|
+
for (let quote_cur in exchange_basecur_curs[exchange]) {
|
|
820
|
+
exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][quote_cur], (cur) => cur + quote_cur))
|
|
821
|
+
exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_basecur_curs[exchange][quote_cur], [quote_cur]))
|
|
822
822
|
}
|
|
823
823
|
}
|
|
824
824
|
let pair_exchanges = {}
|