-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
117 lines (101 loc) · 3.07 KB
/
main.ts
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
import BotConfiguration, { loadConfiguration } from "./botConfiguration.ts";
import { Client } from "./deps.ts";
import getLatestTransactions from "./whaleAlertClient.ts";
import { TransactionsEntity, WhaleAlert } from "./whaleAlert.ts";
import { getLogger } from "./logger.ts";
let connected = false;
const configuration: BotConfiguration = await loadConfiguration();
const logger: any = await getLogger();
const formatter = new Intl.NumberFormat("en-US");
const client = new Client({
nick: configuration.botNick,
channels: [configuration.channel],
});
async function connect() {
if (!connected) {
await client.connect(configuration.ircServer, configuration.ircPort);
}
}
client.on("join", async (_) => {
if (configuration.enableNickServ) {
await client.privmsg(
"NickServ",
`IDENTIFY ${configuration.botNick} ${configuration.nickServPassword}`,
);
logger.debug(
`Sent IDENTIFY ${configuration.botNick} <REDACTED>`,
);
}
});
client.on("connected", (msg) => {
logger.info(`$Connected! Info: ${msg}`);
connected = true;
});
client.on("disconnected", async (msg) => {
logger.error(`$Disconnected! Info: ${msg}`);
connected = false;
await connect();
});
client.on("error", (error) => {
logger.error(`Error: ${JSON.stringify(error)}`);
});
client.on("raw", (msg) => {
logger.debug(`Raw: ${JSON.stringify(msg)}`);
});
client.on("privmsg", (msg) => {
const { origin, target, text } = msg;
logger.debug(`${origin.nick} on ${target} says ${text}`);
});
await connect();
let start = Math.floor(Date.now() / 1000);
const interval = configuration.pollInterval / 1000;
const messageQueue: TransactionsEntity[] = [];
setInterval(async () => {
const alerts: WhaleAlert | undefined = await getLatestTransactions(
configuration.whaleAlertApiKey,
configuration.minValue,
start,
logger,
);
if (!alerts) {
return;
}
logger.debug(
`Retrieved latest transactions starting from ${start}. Current date (ticks): ${
Math.floor(Date.now() / 1000)
}`,
);
logger.debug(`Transactions: ${JSON.stringify(alerts)}`);
if (
alerts.result === "success" && alerts.transactions &&
alerts.transactions.length
) {
alerts.transactions.forEach((transaction) => {
messageQueue.push(transaction);
});
}
start += interval;
}, configuration.pollInterval);
setInterval(() => {
if (!messageQueue || messageQueue.length <= 0) {
return;
}
const transaction = messageQueue.shift();
if (!transaction) {
return;
}
const source = transaction.from.owner_type.toLowerCase() === "unknown"
? "Unknown wallet"
: (transaction.from.owner ?? transaction.from.owner_type);
const target = transaction.to.owner_type.toLowerCase() === "unknown"
? "Unknown wallet"
: (transaction.to.owner ?? transaction.to.owner_type);
client.privmsg(
configuration.channel,
`${
formatter.format(transaction.amount)
} \u0002#${transaction.symbol.toUpperCase()} (${
formatter.format(transaction.amount_usd)
} USD)\u000F transferred from ${source} to ${target}`,
);
}, configuration.rateLimit);