-
Notifications
You must be signed in to change notification settings - Fork 2
/
slackWebhook.js
72 lines (57 loc) · 2.54 KB
/
slackWebhook.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
const axios = require('axios');
class SlackWebhook {
constructor(webhookUrl) {
this.webhookUrl = webhookUrl;
}
async sendMessage(text) {
if (process.env.DEBUG) console.log(`DEBUG: Sending message to Slack:\n${text}`);
try {
const message = { text };
await axios.post(this.webhookUrl, message);
console.log('Message sent to Slack');
} catch (error) {
console.error('Error sending message to Slack:', error.message);
}
}
formatTokenEvent(data, eventType) {
const eventTitle = eventType === 'upgrade' ? 'Upgrade Token Event' : 'Downgrade Token Event';
return `*Block Number: * \`${data.blockNumber}\` - *Event Name:* ${data.eventName} - *Date:* ${data.date}\n
*Transaction Hash: *<${data.transactionHash}|${data.transactionHash.split('/').pop()}>\n*Token Address: *<${data.tokenAddress}|${data.tokenAddress.split('/').pop()}>\n*Token Name:* ${data.tokenName}\n*Amount: * ${data.formattedAmount} (wei: ${data.amount})`;
}
// v2 event formatters
// generic helpers
getHeader(data) {
return `*${data.networkName}* - Block \`${data.blockNumber}\` - Event *${data.eventName}` +
(data.data !== undefined ? `* at ${data.date}\n` : '\n');
}
getTxString(txHash, explorerUrlBase) {
return `<${explorerUrlBase}/tx/${txHash}|${txHash}>`;
}
getAddressString(address, explorerUrlBase) {
return `<${explorerUrlBase}/address/${address}|${address}>`;
}
// event specific - naming convention: format<event name>
formatAppRegistered(data, explorerUrlBase) {
return `${this.getHeader(data)}
App address: ${this.getAddressString(data.app, explorerUrlBase)}\n
Tx: ${this.getTxString(data.transactionHash, explorerUrlBase)}`;
}
formatJail(data, explorerUrlBase) {
return `${this.getHeader(data)}
App address: ${this.getAddressString(data.app, explorerUrlBase)}\n
Reason: ${data.reason}\n
Tx: ${this.getTxString(data.transactionHash, explorerUrlBase)}`;
}
formatVestingScheduleCreated(data, explorerUrlBase) {
return `${this.getHeader(data)}
Token: ${data.tokenSymbol} (${this.getAddressString(data.tokenAddr, explorerUrlBase)})\n
Sender: ${this.getAddressString(data.sender, explorerUrlBase)}\n
Receiver: ${this.getAddressString(data.receiver, explorerUrlBase)}\n
Tx: ${this.getTxString(data.transactionHash, explorerUrlBase)}`;
}
formatFlowScheduleCreated(data, explorerUrlBase) {
// same format as for Vesting Schedules, thus we just delegate
return this.formatVestingScheduleCreated(data, explorerUrlBase);
}
}
module.exports = SlackWebhook;