-
Notifications
You must be signed in to change notification settings - Fork 6
/
monitorTether.js
87 lines (69 loc) · 2.15 KB
/
monitorTether.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
const Discord = require('discord.js');
const delay = require('delay');
const superagent = require('superagent');
const numeral = require('numeral');
const { last } = require('lodash');
const debug = require('debug')('shilly:monitor-tether');
const { RichEmbed } = Discord;
const BASE_URL = 'http://omniexplorer.info/ask.aspx?api=';
const TICK_INTERVAL = 30e3;
const redisKey = _ => `shilly.monitorTether.${_}`;
const monitorTether = async ({ say, redisClient }) => {
const tick = async () => {
const prevHead = await redisClient.getAsync(redisKey('head'));
const txids = await superagent
.get(
`${BASE_URL}getsenderhistory&address=3MbYQMMmSkC3AgWkj9FMo5LsPTW1zBTwXL`
)
.retry()
.then(_ => JSON.parse(_.text).transactions);
const nextHead = txids[0];
if (!prevHead) {
debug(`There is no head. Setting it to ${nextHead}`);
await redisClient.setAsync(redisKey('head'), nextHead);
return;
}
const newGrants = [];
for (const txid of txids) {
if (txid === prevHead) {
break;
}
const tx = await superagent
.get(`${BASE_URL}gettx&txid=${txid}`)
.retry()
.then(_ => JSON.parse(`{${_.text}}`));
if (tx.type !== 'Grant Property Tokens') {
continue;
}
if (!tx.valid) {
return;
}
newGrants.push({
txid,
amount: +tx.amount,
timestamp: new Date(tx.blocktime * 1e3),
});
}
// Set head to the most txid
if (prevHead !== nextHead) {
await redisClient.setAsync(redisKey('head'), nextHead);
}
for (const { txid, amount, timestamp } of newGrants) {
const human = numeral(amount).format('$0.00a');
const long = numeral(amount).format('$0,0');
say({
embed: new RichEmbed({
title: `Bitfinex just issued another ${long} (${human}) in Tether USD!`,
url: `http://omniexplorer.info/lookuptx.aspx?txid=${txid}`,
timestamp,
description: 'See details in the block explorer',
}),
});
}
};
while (true) {
await tick();
await delay(TICK_INTERVAL);
}
};
module.exports = monitorTether;