-
Notifications
You must be signed in to change notification settings - Fork 14
/
functions.js
123 lines (105 loc) · 2.92 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const {AbortController} = require("node-abort-controller");
global.AbortController = AbortController;
const config = require("./config");
const fs = require("fs");
module.exports = {
/**
* @return {boolean}
*/
IsDifferentEnough: function (relativeDiff, price_old, price_new) {
const max_decimals = Math.max(price_new.decimals, price_old.decimals);
const old_multiplier =
price_old.multiplier *
(price_old.decimals < price_new.decimals
? Math.pow(10, max_decimals - price_old.decimals)
: 1);
const new_multiplier =
price_new.multiplier *
(price_new.decimals < price_old.decimals
? Math.pow(10, max_decimals - price_new.decimals)
: 1);
return (
Math.abs(new_multiplier - old_multiplier) >= old_multiplier * relativeDiff
);
},
/**
* @return {number}
*/
GetAvgPrice: function (bid, ask, last){
bid = parseFloat(bid);
ask = parseFloat(ask);
last = parseFloat(last);
if (!(bid * ask) || bid > ask || ask < bid) {
return 0;
}
if (last <= bid) {
return bid;
}
if (last >= ask) {
return ask;
}
return last;
},
/**
* @return {number}
*/
GetMedianPrice: function (data, ticker) {
let values = data.reduce((object, prices) => {
if (prices?.hasOwnProperty(ticker)) {
object.push(prices[ticker]);
}
return object;
}, []);
if (config.PRINT_DEBUG) {
const textPrices = values
.map((price) => (price ? price.toFixed(4) : price))
.join(" ");
console.debug(`DEBUG: ${ticker} prices: ${textPrices}`);
}
if (!values.length) return 0;
values.sort((a, b) => a - b);
let half = Math.floor(values.length / 2);
if (values.length % 2) return values[half];
return (values[half - 1] + values[half]) / 2.0;
},
LoadJson: function (filename, ignoreError = true) {
try {
let rawData = fs.readFileSync(filename);
return JSON.parse(rawData);
} catch (e) {
if (!ignoreError) {
console.error("Failed to load JSON:", filename, e);
}
}
return null;
},
SaveJson: function (json, filename) {
try {
const data = JSON.stringify(json);
fs.writeFileSync(filename, data);
} catch (e) {
console.error("Failed to save JSON:", filename, e);
}
},
fetchWithTimeout: async function (resource, options = {}) {
const {timeout = 5000} = options;
const controller = new AbortController();
try {
const id = setTimeout(() => {
console.log(`!!!Abort on Fetch Timeout: ${resource}`);
controller.abort();
}, timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal
});
clearTimeout(id);
return response;
} catch (err) {
if(!controller.signal.aborted){
console.log(err);
}
return Promise.reject("fetchWithTimeout rejected");
}
}
};