-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
257 lines (237 loc) · 7.08 KB
/
main.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
const fs = require("fs");
const TurndownService = require("turndown");
const dotenv = require("dotenv");
dotenv.config();
const turndownService = new TurndownService();
turndownService.addRule("removeImages", {
filter: ["img"],
replacement: () => "",
});
const juicebox_discord_webhook = process.env.JUICEBOX_WEBHOOK;
const peel_discord_webhook = process.env.PEEL_WEBHOOK;
const juicebox_subgraph = process.env.JUICEBOX_SUBGRAPH;
console.log("Juicebox Heartbeat initialized.");
if (!fs.existsSync("recent-runs.json")) {
fs.writeFileSync(
"recent-runs.json",
JSON.stringify({
lastPayEventTime: Math.floor(Date.now() / 1000),
lastProjectCreateEventTime: Math.floor(Date.now() / 1000),
}),
{ encoding: "utf8" }
);
console.log("Created new recent-runs.json");
}
let { lastPayEventTime, lastProjectCreateEventTime } = JSON.parse(
fs.readFileSync("recent-runs.json")
);
const payEventsQuery = `{
payEvents(where:{timestamp_gt: ${lastPayEventTime}}){
project {
handle
metadataUri
}
amount
projectId
beneficiary
txHash
pv
timestamp
note
}
}`;
const projectCreateEventsQuery = `{
projectCreateEvents(where:{timestamp_gt: ${lastProjectCreateEventTime}}){
project{
handle
metadataUri
}
from
projectId
txHash
pv
timestamp
}
}`;
// Utils
async function querySubgraph(query) {
return fetch(juicebox_subgraph, {
headers: { "Content-Type": "application/json" },
method: "POST",
body: JSON.stringify({ query }),
}).then((res) => res.json());
}
async function resolveMetadata(metadataUri) {
return fetch(`https://ipfs.io/ipfs/${metadataUri}`).then((res) => res.json());
}
async function resolveEns(address) {
const ens = await fetch(
`https://api.ensideas.com/ens/resolve/${address}`
).then((res) => res.json());
return ens.name ? ens.name : address;
}
async function postToDiscordWebhook(
webhook_url,
title,
url,
fields,
thumbnail
) {
console.log(`New webhook post: ${title}`);
fetch(webhook_url, {
headers: { "Content-Type": "application/json" },
method: "POST",
body: JSON.stringify({
embeds: [
{
title,
url,
color: Math.floor(16777215 * Math.random()), // Random decimal color
fields,
thumbnail,
},
],
}),
})
.then((res) => res.text())
.then((x) => console.log(x));
}
async function handlePayEvents() {
return new Promise((resolve, reject) => {
querySubgraph(payEventsQuery).then(async (json) => {
for (const payEvent of json.data.payEvents) {
const [metadata, beneficiary] = await Promise.all([
resolveMetadata(payEvent.project.metadataUri),
resolveEns(payEvent.beneficiary),
]);
const project_name = metadata.name
? metadata.name
: `v${payEvent.pv} project ${payEvent.projectId}`;
postToDiscordWebhook(
juicebox_discord_webhook,
`Payment to ${project_name}`,
`https://juicebox.money/${
payEvent.pv === "2"
? "v2/p/" + payEvent.projectId
: "p/" + payEvent.project.handle
}`,
[
...(payEvent.note
? [{ name: `Note`, value: `*${payEvent.note}*`, inline: false }]
: []),
{
name: `Amount`,
value: `${payEvent.amount / 1e18} ETH`,
inline: true,
},
{
name: `Beneficiary`,
value: `[${beneficiary}](https://juicebox.money/account/${payEvent.beneficiary})`,
inline: true,
},
{
name: `Transaction`,
value: `[Etherscan](https://etherscan.io/tx/${payEvent.txHash})`,
inline: true,
},
],
{
url: metadata.logoUri
? `https://ipfs.io/ipfs/${metadata.logoUri.substring(
metadata.logoUri.lastIndexOf("/") + 1
)}`
: undefined,
}
)
.then(() => {
if (payEvent.timestamp > lastPayEventTime)
lastPayEventTime = payEvent.timestamp;
})
.catch((e) => {
fs.appendFileSync("errors.txt", e, { encoding: "utf8" });
reject(e);
});
}
resolve();
});
});
}
async function handleCreateEvents() {
return new Promise((resolve, reject) => {
querySubgraph(projectCreateEventsQuery).then(async (json) => {
for (const projectCreateEvent of json.data.projectCreateEvents) {
const [metadata, from] = await Promise.all([
resolveMetadata(projectCreateEvent.project.metadataUri),
resolveEns(projectCreateEvent.from),
]);
const project_name = metadata.name
? metadata.name
: `v${projectCreateEvent.pv} project ${projectCreateEvent.projectId}`;
const containsHTML = /<\/?[a-z][\s\S]*>/i.test(metadata.description);
const processedDescription = containsHTML
? turndownService
.turndown(metadata.description)
.replace(/\n\s*/g, "\n\n")
.slice(0, 1000)
: metadata.description.replace(/\n\s*/g, "\n\n").slice(0, 1000);
for (const webhook of [juicebox_discord_webhook, peel_discord_webhook])
postToDiscordWebhook(
webhook,
`New Project: ${project_name}`,
`https://juicebox.money/${
projectCreateEvent.pv === "2"
? "v2/p/" + projectCreateEvent.projectId
: "p/" + projectCreateEvent.project.handle
}`,
[
{
name: `Creator`,
value: `[${from}](https://juicebox.money/account/${projectCreateEvent.from})`,
inline: true,
},
{
name: `Transaction`,
value: `[Etherscan](https://etherscan.io/tx/${projectCreateEvent.txHash})`,
inline: true,
},
{
name: `Description`,
value: processedDescription,
inline: false,
},
],
{
url: metadata.logoUri
? `https://ipfs.io/ipfs/${metadata.logoUri.substring(
metadata.logoUri.lastIndexOf("/") + 1
)}`
: undefined,
}
)
.then(() => {
if (projectCreateEvent.timestamp > lastProjectCreateEventTime)
lastProjectCreateEventTime = projectCreateEvent.timestamp;
})
.catch((e) => {
fs.appendFileSync("errors.txt", e, { encoding: "utf8" });
reject(e);
});
}
resolve();
});
});
}
async function main() {
await Promise.all([handlePayEvents(), handleCreateEvents()]);
fs.writeFileSync(
"recent-runs.json",
JSON.stringify({
lastPayEventTime,
lastProjectCreateEventTime,
}),
{ encoding: "utf8" }
);
}
main().catch((error) => {
console.error("An error occurred:", error);
});