-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
487 lines (467 loc) · 14.3 KB
/
app.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
const { App } = require('@slack/bolt');
require('dotenv').config();
const request = require('superagent');
const app = new App({
token: process.env.SLACK_API_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.action('check-balance', async({ ack, body, client }) => {
await ack();
const checkBalance = await request.get(`${process.env.BACKEND_URL}/quack/check-balance/${body.user.id}`);
await client.chat.postEphemeral({
channel: body.channel.id,
user: body.user.id,
text: `Your current balance is ${checkBalance.text} Quacks. :duck:`
});
});
app.action('send-quacks', async({ ack, body, client }) => {
await ack();
try {
const result = await client.views.open({
trigger_id: body.trigger_id,
view: {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Quackchain",
"emoji": true
},
"submit": {
"type": "plain_text",
"text": "Send Quacks",
"emoji": true
},
"close": {
"type": "plain_text",
"text": "Cancel",
"emoji": true
},
"callback_id": "send-quacks",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Welcome to the QuackChain! ",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Enter the details below to send some quacks to your fellow ducks!",
"emoji": true
}
},
{
"type": "divider"
},
{
"block_id": "receiver-id",
"type": "input",
"element": {
"type": "multi_users_select",
"max_selected_items": 1,
"placeholder": {
"type": "plain_text",
"text": "Select users",
"emoji": true
},
"action_id": "receiver-action"
},
"label": {
"type": "plain_text",
"text": "Who would you like to send Quacks to?",
"emoji": true
}
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": "Only allowed to select 1 recipient!",
"emoji": true
}
]
},
{
"block_id": "amount-input",
"type": "input",
"element": {
"type": "plain_text_input",
"action_id": "amount-action"
},
"label": {
"type": "plain_text",
"text": "How many Quacks should we send?",
"emoji": true
}
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": "Whole Quacks, only, please!",
"emoji": true
}
]
},
{
"block_id": "notes-input",
"type": "input",
"element": {
"type": "plain_text_input",
"multiline": true,
"action_id": "notes-action"
},
"label": {
"type": "plain_text",
"text": "Add a note to send with your Quacks",
"emoji": true
}
}
]
}
});
}
catch (error) {
console.error(error);
};
});
app.view('send-quacks', async({ ack, body, view, client }) => {
await ack();
const senderId = body.user.id;
const receiverId = view.state.values['receiver-id']['receiver-action'].selected_users[0];
const amount = view.state.values['amount-input']['amount-action'].value;
const balance = await request.get(`${process.env.BACKEND_URL}/quack/check-balance/${body.user.id}`);
if(balance.text < amount) {
await client.chat.postEphemeral({
channel: senderId,
user: senderId,
text: `Not enough quacks for this transaction.`
});
} else {
await request.post(`${process.env.BACKEND_URL}/quack/send-quacks`).send({
senderId,
receiverId,
amount
});
const notes = view.state.values['notes-input']['notes-action'].value;
await client.chat.postMessage({
channel: receiverId,
text: `<@${senderId}> just sent you ${amount} Quacks!
This transaction is being recorded onto the blockchain and should be available in your account in about a minute!
from <@${senderId}>: ${notes} :duck:`
});
await client.chat.postEphemeral({
channel: senderId,
user: senderId,
text: `Transaction has been initiated`
});
}
});
app.action('mint-quacks', async({ ack, body, client }) => {
await ack();
const balance = await request.get(`${process.env.BACKEND_URL}/quack/check-balance/${body.user.id}`);
if(balance.text > 0) {
try {
const result = await client.views.open({
trigger_id: body.trigger_id,
view: {
"type": "modal",
"title": {
"type": "plain_text",
"text": "Quackchain",
"emoji": true
},
"submit": {
"type": "plain_text",
"text": "Mint Quacks",
"emoji": true
},
"close": {
"type": "plain_text",
"text": "Cancel",
"emoji": true
},
"callback_id": "mint-quacks",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Welcome to the Quackchain! ",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Here you can create Quacks! In order to mint Quacks you must first burn some as a processing fee.",
"emoji": true
}
},
{
"type": "divider"
},
{
"block_id": "mint-input",
"type": "input",
"element": {
"type": "plain_text_input",
"action_id": "mint-action"
},
"label": {
"type": "plain_text",
"text": "How many Quacks would you like to mint?",
"emoji": true
}
},
{
"type": "context",
"elements": [
{
"type": "plain_text",
"text": "Whole Quacks, only, please!",
"emoji": true
}
]
}
]
}
});
}
catch (error) {
console.error(error);
}
} else {
await client.chat.postEphemeral({
channel: body.user.id,
user: body.user.id,
text: `Only holders can mint Quacks!`
});
}
});
app.view('mint-quacks', async({ ack, body, view, client }) => {
await ack();
const slackId = body.user.id;
const amount = view.state.values['mint-input']['mint-action'].value;
await request.post(`${process.env.BACKEND_URL}/quack/mint-quacks`).send({
slackId,
amount
});
const accountList = await request.get(`${process.env.BACKEND_URL}/admin/account-list`);
const perCapita = Math.floor((amount/accountList.body.length));
for(const account of accountList.body) {
await client.chat.postMessage({
channel: account,
text: `<@${slackId}> has minted ${amount} new Quacks. Each holder has received ${perCapita} Quacks.`
});
}
});
app.command('/quack', async({ command, ack, client }) => {
await ack();
const totalSupply = await request.get(`${process.env.BACKEND_URL}/admin/total-supply`);
const total = totalSupply.res.text;
await client.chat.postEphemeral(
{
"channel": command.channel_id,
"user": command.user_id,
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Quack",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Welcome to our App. Currently there are *${total}* Quacks in the pond. :duck:`
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Check Balance",
"emoji": true
},
"value": "check your balance",
"action_id": "check-balance"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Send Quacks",
"emoji": true
},
"value": "click_me_123",
"action_id": "send-quacks"
},
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Mint Quacks",
"emoji": true
},
"value": "click_me_123",
"action_id": "mint-quacks"
}
]
}
]
})
});
app.event('app_home_opened', async ({ event, client, context }) => {
try {
const result = await client.views.publish({
user_id: event.user,
view: {
"type": "home",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "Welcome to QuackChain!"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*What is Quack?*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Quack is a digital currency built on the the Ethereum ERC-20 standard. It is deployed to an Ethereum testnet and used as a workspace specific currency in Slack. This provides a permanent, distributed record of all transactions."
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*QuackChain's Mission*"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "QuackChain will allow members of the workspace to show appreciation for one another by sending tips, use Quacks to show support for proposals and participate in a digital marketplace. This will incentivize acts of appreciation, encourage camaraderie and make Slack more fun and interactive.",
"emoji": true
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*How to use it!*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "It's quite simple! To interact with the app, simply type */quack* in any channel you are a member of. You will be presented with an interface that let's you send quacks to team members, check your Quack wallet balance and mint new quacks to the blockchain."
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*What is minting?*"
}
},
{
"type": "section",
"text": {
"type": "plain_text",
"text": "Every cryptocurrency has a total supply, this is what gives it its value. Users can mint new Quacks to the blockchain, which adds more currency to the total supply. Upon minting new Quacks the minted amount is distributed evenly amongst all current Quack holders. As a fee, half of the minters portion of the distribution will be burned and removed from the total supply to avoid runaway inflation. Mint with care!! :duck:",
"emoji": true
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Here is an example of the minting process*:"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "_Alice would like to mint *1000 new Quacks*. There are currently *10* Quack holders, including Alice. Upon minting Alice will receive *50 Quacks* and Bob, another holder, will receive *100 Quacks*. The total supply will increase by *1000 Quacks*._"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Learn more about the QuackChain project!*"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<https://github.com/Alchemy-Crypto| Github Project Page>"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "<https://github.com/Alchemy-Crypto/docs/blob/main/README.md| QuackChain Creators>"
}
}
]
}
});
} catch (err) {
console.log(err);
}
});
(async() => {
await app.start(process.env.PORT || 8080);
console.log('the app is running');
})();
module.exports = { app };