-
Notifications
You must be signed in to change notification settings - Fork 2
/
slack.ts
74 lines (67 loc) · 2.11 KB
/
slack.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
import axios from "axios";
import { dollars, lookupClosure, percent } from "../../utils";
import type {
CrNotificationParams,
NotificationTarget,
TroveClosureNotificationParams,
TroveCrNotificationParams
} from "..";
const postAlert = async (webhookUrl: string, title: string, messageBody: string) => {
await axios.post(webhookUrl, {
text: `Liquity Alert: ${title}`,
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: title
}
},
{
type: "section",
text: {
type: "mrkdwn",
text: messageBody
}
}
]
});
};
const notifyTcr = (webhookUrl: string) => (params: CrNotificationParams) =>
postAlert(
webhookUrl,
"TCR threshold crossed",
[
`*Current TCR*: ${percent(params.current)}`,
`*Threshold*: ${percent(params.threshold)}`,
`*Current price*: ${dollars(params.price.value)} (source: ${params.price.source})`
].join("\n")
);
const notifyTroveCr = (webhookUrl: string) => (params: TroveCrNotificationParams) =>
postAlert(
webhookUrl,
`Trove CR threshold crossed (${params.name})`,
[
`*Name*: ${params.name}`,
`*Address*: <https://etherscan.io/address/${params.address}|${params.address}>`,
`*Current CR*: ${percent(params.current)}`,
`*Threshold*: ${percent(params.threshold)}`,
`*Current price*: ${dollars(params.price.value)} (source: ${params.price.source})`
].join("\n")
);
const notifyTroveClosure = (webhookUrl: string) => (params: TroveClosureNotificationParams) =>
postAlert(
webhookUrl,
`Trove closed (${params.name})`,
[
`*Name*: ${params.name}`,
`*Address*: <https://etherscan.io/address/${params.address}|${params.address}>`,
`*Closed by*: ${lookupClosure(params.status)}`,
`*Current price*: ${dollars(params.price.value)} (source: ${params.price.source})`
].join("\n")
);
export default (webhookUrl: string): NotificationTarget => ({
notifyTcr: notifyTcr(webhookUrl),
notifyTroveCr: notifyTroveCr(webhookUrl),
notifyTroveClosure: notifyTroveClosure(webhookUrl)
});