-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalerts.js
57 lines (54 loc) · 1.46 KB
/
alerts.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
const rp = require("request-promise");
const EXCHANGES = require("./config/exchanges.json");
const API = "https://api.semux.info/v2.3.0/";
const TX_VALUE = 2500; // SEM
let BEST_HEIGHT = 0;
async function scanNewBlock() {
let lastHeight;
try {
lastHeight = JSON.parse(await rp(`${API}latest-block-number`));
} catch (e) {
console.error("Failed to get latest block number", e.message);
return { error: true };
}
lastHeight = parseInt(lastHeight.result, 10);
if (lastHeight === BEST_HEIGHT) {
return { error: true };
}
BEST_HEIGHT = lastHeight;
let block;
try {
block = JSON.parse(await rp(`${API}block-by-number?number=${BEST_HEIGHT}`));
} catch (e) {
console.error("Failed to get block by number", e.message);
return { error: true };
}
if (!block.result || !block.result.transactions) {
return { error: true };
}
let transfers = [];
for (let tx of block.result.transactions) {
let value = parseInt(tx.value, 10) / 1e9;
if (tx.type !== "TRANSFER" || value < TX_VALUE) {
continue;
}
if (EXCHANGES[tx.from]) {
transfers.push({
exchange: EXCHANGES[tx.from],
value: value.toFixed(2),
type: "withdrawn",
});
}
if (EXCHANGES[tx.to]) {
transfers.push({
exchange: EXCHANGES[tx.to],
value: value.toFixed(2),
type: "deposited",
});
}
}
return { success: true, transfers: transfers };
}
module.exports = {
scanNewBlock,
};