-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
337 lines (310 loc) · 10.7 KB
/
index.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const {
Client,
GatewayIntentBits,
Events,
Partials,
ButtonBuilder,
ActionRowBuilder,
ButtonStyle
} = require("discord.js");
const { recoverAddress } = require("@ethersproject/transactions");
const { arrayify } = require("@ethersproject/bytes");
const { hashMessage } = require("@ethersproject/hash");
const { JsonRpcProvider } = require("@ethersproject/providers");
const { Contract } = require("@ethersproject/contracts");
const cron = require("node-cron");
const jwt = require("jsonwebtoken");
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const prisma = require("./prisma");
const {
BOT_TOKEN,
RPC_URL,
JWT_SECRET,
START_HERE_CHANNEL_ID,
SERVER_GUILD_ID,
SERVER_ADMIN_ADDRESS,
SUPER_TOKEN_CHAIN_ID,
SUPER_TOKEN_ADDRESS,
CFAV1_ADDRESS,
REQUIRED_MINIMUM_FLOW_RATE,
APP_URL
} = require("./config.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
],
partials: [Partials.GuildMember]
});
client.login(BOT_TOKEN);
const app = express();
app.use(express.json());
app.use(express.static(__dirname + "/public"));
app.use(
cors({
origin: "*",
methods: ["GET", "POST"]
})
);
const provider = new JsonRpcProvider(RPC_URL);
const cfaV1ABI = [
"function getFlow(address token, address sender, address receiver) view returns (uint256 timestamp, int96 flowRate, uint256 deposit, uint256 owedDeposit)"
];
const cfaV1Contract = new Contract(CFAV1_ADDRESS, cfaV1ABI, provider);
const chains = {
1: "ethereum mainnet",
5: "goerli",
80001: "polygon mumbai",
137: "polygon"
};
const getStreamFlowRate = async (sender, receiver) => {
try {
const { flowRate } = await cfaV1Contract.getFlow(
SUPER_TOKEN_ADDRESS,
sender,
receiver
);
return flowRate.toString();
} catch (error) {
console.log("err getting flow info", error);
return 0;
}
};
// Periodic cron job to check streams and add/remove roles accordingly
cron.schedule("0 0 * * *", async () => {
console.log(
`Checking streams and adding/removing roles at ${new Date().toUTCString()}`
);
const guild = client.guilds.cache.get(SERVER_GUILD_ID);
const streamerRole = guild.roles.cache.find(
(role) => role.name === "streamer"
);
const members = await prisma.member.findMany();
await Promise.all(
members.map(async (member) => {
const streamFlowRate = await getStreamFlowRate(
member.walletAddress || "0x0",
SERVER_ADMIN_ADDRESS
);
const guildMember = await guild.members.fetch(member.id);
console.log(
`Current flow rate for ${guildMember.user.tag} with wallet ${member.walletAddress} is ${streamFlowRate} DAIx/sec`
);
const hasRequiredStream = streamFlowRate >= REQUIRED_MINIMUM_FLOW_RATE;
const currentRole = member.role;
let updatedRole = currentRole;
if (hasRequiredStream && currentRole !== "streamer") {
// add streamer role
await guildMember.roles
.add(streamerRole)
.catch((err) => console.log("failed to add role", err));
updatedRole = "streamer";
console.log("added streamer role to member", guildMember.user.tag);
} else if (!hasRequiredStream && currentRole !== "member") {
// remove streamer role
await guildMember.roles
.remove(streamerRole)
.catch((err) => console.log("failed to remove role", err));
updatedRole = "member";
console.log("removed streamer role from member", guildMember.user.tag);
} else {
console.log(
`Stream is ${hasRequiredStream ? "active" : "not active"
} for ${currentRole}: ${guildMember.user.tag
}. No role changes required.`
);
}
// update role in db if changed
if (updatedRole !== currentRole) {
await prisma.member.update({
where: {
id: member.id
},
data: {
role: updatedRole
}
});
}
})
);
console.log("Done checking streams and adding/removing roles");
});
client.once(Events.ClientReady, () =>
console.log(`Logged in as ${client.user.tag}`)
);
client.on(Events.MessageCreate, (msg) => {
if (
msg.author.bot ||
msg.system ||
msg.channelId !== START_HERE_CHANNEL_ID ||
msg.channel.type === "DM"
)
return;
// Basic command handler
const commands = {
ping: "pong",
pong: "ping",
"ping pong": "pong ping",
date: new Date().toUTCString()
};
const commandResponse = commands[msg.content.toLowerCase()];
if (commandResponse) {
msg.reply(commandResponse);
} else {
const possibleCommands = [
"/ping",
"/verify",
...Object.keys(commands)
].join(", ");
msg.reply(
`I don't know what you mean. I can only respond to the following commands: ${possibleCommands}`
);
}
});
client.on(Events.InteractionCreate, (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "verify") {
const jwtToken = jwt.sign(
{ guildId: interaction.guildId, memberId: interaction.member.id },
JWT_SECRET,
{ expiresIn: "5m" } // 5 minutes
);
const greeting = "Hello there! Welcome to the server!";
const steps = [
`Please Click on Verify with Wallet to verify your wallet address.`,
`Make sure you have a minimum of 0.5 DAIx/month stream to our server admin account: ${SERVER_ADMIN_ADDRESS} on ${chains[SUPER_TOKEN_CHAIN_ID]}`,
"Once verified, you'll be automatically assigned the streamer role which will give you access to our exclusive channels and perks!"
];
const outro =
"If you have any questions or encounter any issues, please don't hesitate to reach out to us. Good luck and have fun!";
const message = greeting + "\n\n" + steps.join("\n") + "\n\n" + outro;
const verifyWithWalletButton = new ButtonBuilder()
.setLabel("Verify with Wallet")
.setStyle(ButtonStyle.Link)
.setURL(`${APP_URL}/verify?token=${jwtToken}`);
const actionRow = new ActionRowBuilder().addComponents(
verifyWithWalletButton
);
interaction.reply({
content: message,
components: [actionRow],
ephemeral: true
});
} else if (interaction.commandName === "ping") {
interaction.reply({
content: "pong!",
ephemeral: true
});
} else {
interaction.reply({
content:
"I don't know what you mean. I can only respond to the following commands: /verify, /ping",
ephemeral: true
});
}
});
client.on(Events.GuildMemberAdd, (member) => {
console.log("a new member hopped into server", member.user.tag);
const startHereChannel = member.guild.channels.cache.get(
START_HERE_CHANNEL_ID
);
startHereChannel.send({
content: `Welcome, ${member.user.username}. We hope you brought pizza! Please type \`/verify\` command to verify your wallet and get access to our exclusive channels and perks!`
});
});
client.on(Events.GuildMemberRemove, (member) => {
console.log("a member left the server", member.user.tag, member.id);
// delete member from db only if they have atleast member role
const hasRole = member.roles.cache.some((role) => role.name === "member");
if (hasRole) {
prisma.member
.delete({
where: {
id: member.id
}
})
.catch((err) => console.log("failed to delete member from db", err));
}
});
// verify signature and add wallet address to the member object and then add role if stream is active
app.post("/verify", async (req, res) => {
if (
["token", "address", "message", "signature"].some((key) => !req.body[key])
)
return res.status(400).json({
code: "Bad Request",
message: "Missing required fields: token/address/message/signature"
});
const { token, address, message, signature } = req.body;
try {
const { guildId, memberId } = jwt.verify(token, JWT_SECRET);
console.log("decoded from token", { guildId, memberId });
console.log("verifying signature and checking stream");
const digest = arrayify(hashMessage(message));
const recoveredAddress = recoverAddress(digest, signature);
if (recoveredAddress.toLowerCase() !== address.toLowerCase())
return res
.status(401)
.send({ code: "Unauthorized", message: "Invalid wallet signature" });
const streamFlowRate = await getStreamFlowRate(
address.toLowerCase(),
SERVER_ADMIN_ADDRESS
);
const guild = client.guilds.cache.get(guildId);
const member = await guild.members.fetch(memberId);
const startHereChannel = guild.channels.cache.get(START_HERE_CHANNEL_ID);
const hasRequiredStream = streamFlowRate >= REQUIRED_MINIMUM_FLOW_RATE;
const truncatedAddress = address.slice(0, 5) + "..." + address.slice(-4);
// add member role by default upon verifying wallet
const memberRole = guild.roles.cache.find((role) => role.name === "member");
await member.roles.add(memberRole);
if (hasRequiredStream) {
const streamerRole = guild.roles.cache.find(
(role) => role.name === "streamer"
);
// add streamer role if stream is active
await member.roles.add(streamerRole);
startHereChannel.send(
`Hey <@${memberId}>, your wallet address ${truncatedAddress} has been verified and you have been given the streamer role. You can now access the #superfluid-exclusive channel.`
);
} else {
startHereChannel.send(
`Hey <@${memberId}>, your wallet address ${truncatedAddress} has been verified and you have been given member role. but you don't have enough stream to access #superfluid-exclusive channel. A minimum of 0.5 DAIx/month stream to ${SERVER_ADMIN_ADDRESS} on ${chains[SUPER_TOKEN_CHAIN_ID]} is required.`
);
}
// upsert member object with wallet address and role
await prisma.member.upsert({
where: {
id: memberId
},
update: {
walletAddress: address,
username: member.user.username,
role: hasRequiredStream ? "streamer" : "member"
},
create: {
id: memberId,
walletAddress: address,
username: member.user.username,
role: hasRequiredStream ? "streamer" : "member"
}
});
return res.status(200).json({ code: "ok", message: "Success" });
} catch (err) {
console.log("failed to verify user:", err);
return res
.status(500)
.json({ code: "Internal Server Error", message: err.message });
}
});
app.get("/verify", (req, res) =>
res.sendFile(__dirname + "/public/index.html")
);
const port = process.env.PORT || 3000;
app.listen(port, () =>
console.log(`🔥 Server listening at http://localhost:${port} 🚀`)
);