-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.js
145 lines (107 loc) · 4.24 KB
/
diff.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { CRYPTO, EXCHANGES, EXCHANGE_ABRV, TIME } = require('./constants');
const formatDate = require('./date');
const { sendNotification } = require('./twilio');
const diffLogs = CRYPTO.reduce((logs, currency) => {
logs[currency] = {
difference: 0,
level: 0,
lastNotification: undefined,
numNotifications: 0
};
return logs;
}, {});
const notificationLog = [];
let lastNotification = Date.now() - 5 * TIME.MINUTE;
function shouldNotify({ isKucoin, isHitBTC, level }) {
if (isKucoin && isHitBTC) return level > 4;
if (isKucoin || isHitBTC) return level > 2;
return level > 1;
}
function isUrgent({ isKucoin, isHitBTC, level }) {
if (isKucoin && isHitBTC) return level > 5;
return level > 4;
}
exports.getNotifications = function getNotifications() {
return { notifications: notificationLog };
};
exports.diff = function diff(prices) {
const now = Date.now();
const notifications = [];
const urgent = [];
CRYPTO.forEach(currency => {
const exchangeDifferences = EXCHANGES.reduce((differences, buyFrom) => {
const buyMarket = prices[currency][buyFrom];
if (!buyMarket || !buyMarket.withdrawalsEnabled || !buyMarket.ask) return differences;
EXCHANGES.forEach(sellAt => {
if (buyFrom === sellAt) return;
const sellMarket = prices[currency][sellAt];
if (!sellMarket || !sellMarket.depositsEnabled || !sellMarket.bid) return;
differences.push({
buy: buyFrom,
sell: sellAt,
bid: sellMarket.bid,
ask: buyMarket.ask,
difference: sellMarket.bid - buyMarket.ask
});
});
return differences;
}, []).sort((a, b) => {
if (a.difference < b.difference) return -1;
if (a.difference > b.difference) return 1;
return 0;
});
if (exchangeDifferences.length === 0) return;
const diffLog = diffLogs[currency];
const greatest = exchangeDifferences[exchangeDifferences.length - 1];
const percentage = (greatest.difference / greatest.bid * 100).toFixed(2);
let level = 0;
if (percentage > 50) level += 1;
if (percentage > 40) level += 1;
if (percentage > 30) level += 1;
if (percentage > 25) level += 1;
if (percentage > 20) level += 1;
if (percentage > 15) level += 1;
if (percentage > 10) level += 1;
if (percentage > 7) level += 1;
if (percentage > 5) level += 1;
if (percentage > 4) level += 1;
if (percentage > 3) level += 1;
if (percentage > 2) level += 1;
if (percentage > 1) level += 1;
const isKucoin = greatest.buy === 'kucoin' || greatest.sell == 'kucoin';
const isHitBTC = greatest.buy === 'hitbtc' || greatest.sell == 'hitbtc';
if (shouldNotify({ isKucoin, isHitBTC, level })) {
const buyFrom = EXCHANGE_ABRV[greatest.buy];
const sellAt = EXCHANGE_ABRV[greatest.sell];
console.log(`${currency} is cheaper at ${greatest.buy}: ${percentage}%`);
console.log(`${buyFrom}: ${greatest.ask}, ${sellAt}: ${greatest.bid}`);
console.log(`Previously the difference was ${diffLog.difference}%`);
const notification =
`${currency}: ${percentage}%, ${buyFrom} ${greatest.ask}, ${sellAt} ${greatest.bid}`;
notifications.push(notification);
if (isUrgent({ isKucoin, isHitBTC, level })) urgent.push(notification);
diffLog.lastNotification = now;
diffLog.numNotifications += 1;
}
if (level > 0) {
if (notificationLog.length > 500) notificationLog.shift();
notificationLog.push([
`${currency} is cheaper at ${greatest.buy}: ${percentage}%`,
`${greatest.buy}: ${greatest.ask}, ${greatest.sell}: ${greatest.bid}`,
`Previously the difference was ${diffLog.difference}%`,
formatDate(new Date())
].join('\n'))
}
diffLog.difference = percentage;
diffLog.level = level;
});
if (now - lastNotification > 5 * TIME.MINUTE && notifications.length > 0) {
lastNotification = now;
console.log('Sending notifications', notifications.join('; '));
// sendNotification(notifications.join('; '));
} else if (now - lastNotification > 30 * TIME.SECOND && urgent.length > 0) {
lastNotification = now;
console.log('Sending urgent notifications', urgent.join('; '));
sendNotification(urgent.join('; '));
}
}