-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
648 lines (615 loc) · 19.2 KB
/
index.js
File metadata and controls
648 lines (615 loc) · 19.2 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const axios = require("axios");
const https = require("https");
const fs = require("fs");
const algosdk = require("algosdk");
const { Database } = require("./database.js");
require("dotenv").config();
const {
MN,
RECAPTCHA_SITE_KEY,
RECAPTCHA_SECRET_KEY,
ALGO_SERVER,
ALGO_INDEXER_SERVER,
DB_PATH,
PORT,
} = process.env;
const algodClient = new algosdk.Algodv2(
process.env.ALGOD_TOKEN || "",
process.env.ALGOD_SERVER || ALGO_SERVER,
process.env.ALGOD_PORT || ""
);
const getLastRound = async () => {
const status = await algodClient.status().do();
return status["last-round"] || 0;
};
const indexerClient = new algosdk.Indexer(
process.env.INDEXER_TOKEN || "",
process.env.INDEXER_SERVER || ALGO_INDEXER_SERVER,
process.env.INDEXER_PORT || ""
);
const dbPath = DB_PATH || "./db.sqlite";
const db = new Database(dbPath);
const app = express();
const port = PORT || "3001";
app.use(cors());
//app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json());
// cors
const corsOptions = {
origin: "https://nautilus.sh", // Allow nautilus
methods: "GET,POST",
allowedHeaders: "Content-Type,Authorization",
optionsSuccessStatus: 200,
};
// middleware
const ADDRESS_REGEX = /[A-Z0-9]{58}/;
// Address validation middleware
const validateKey = (req, res, next) => {
const address = req.query.key;
if (!address) {
return res.status(400).json({ error: "Address is required" });
}
// Define your regex pattern for validating the address
const addressRegex = ADDRESS_REGEX;
if (!addressRegex.test(address)) {
return res.status(400).json({ error: "Invalid address format" });
}
// If validation passes, proceed to the next middleware or route handler
next();
};
const validateAction = (req, res, next) => {
const { action, data } = req.body;
if (!action || !data) {
return res.status(400).json({ error: "Action and data are required" });
}
switch (action) {
case "hmbl_token_create": {
const { tokenId } = data;
if (isNaN(Number(tokenId))) {
return res.status(400).json({ message: "Invalid token id" });
}
break;
}
case "hmbl_pool_create":
case "hmbl_pool_add":
case "hmbl_pool_swap":
case "hmbl_pool_swap_daily":
const { poolId } = data;
if (isNaN(Number(poolId))) {
return res.status(400).json({ message: "Invalid pool id" });
}
case "connect_wallet":
case "sale_list_once":
case "sale_buy_once":
case "sale_list_once":
case "timed_sale_list_1minute":
case "timed_sale_list_1hour":
case "timed_sale_list_15minutes":
case "swap_execute_once":
case "faucet_drip_once":
case "swap_list_once":
case "swap_list_once":
case "swap_execute_once":
case "timed_sale_list_1minute":
case "timed_sale_list_15minutes":
case "timed_sale_list_1hour":
case "hmbl_token_create":
case "hmbl_pool_create":
case "hmbl_pool_add":
case "hmbl_farm_stake":
case "hmbl_farm_claim":
case "hmbl_farm_create":
case "not-a-quest": {
// check address validitiy
const ADDRESS_REGEX = /[A-Z0-9]{58}/;
const [{ address }] = data.wallets;
if (!ADDRESS_REGEX.test(address)) {
return res.status(400).json({ message: "Invalid address" });
}
break;
}
default:
return res
.status(400)
.json({ message: `Unsupported action '${action}'` });
}
next();
};
// routes
app.get("/score", async (req, res) => {
const scores = await db.getScores();
return res.status(200).json({
scores
});
});
app.get("/quest", validateKey, async (req, res) => {
const key = req.query.key;
// validate key
const results = await db.searchInfo(key);
return res.status(200).json({ message: "ok", results });
});
const minRound = 6534432; // 1 May
const ctcInfoMp212 = 40433943;
const ctcInfoStakr = 36898212;
const ctcInfoMp = 29117863;
app.post("/quest", cors(corsOptions), validateAction, async (req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Response-Type", "application/json");
const { action, data } = req.body;
try {
const { wallets, contractId, tokenId, poolId } = data;
const [{ address }] = wallets;
const key = `${action}:${address}`;
console.log(key);
const info = await db.getInfo(key);
switch (action) {
case "connect_wallet": {
if (!info) await db.setInfo(key, Date.now());
break;
}
case "sale_list_once": {
const propertyName = "listings";
if (!info) {
const { data } = await axios.get(
`https://arc72-idx.nftnavigator.xyz/nft-indexer/v1/mp/listings?seller=${address}&min-round=${minRound}`
);
if (data[propertyName].length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "sale_buy_once": {
const propertyName = "sales";
if (!info) {
const { data } = await axios.get(
`https://arc72-idx.nftnavigator.xyz/nft-indexer/v1/mp/sales?buyer=${address}&min-round=${minRound}`
);
if (data[propertyName].length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "swap_list_once": {
// 000004
const spec = {
name: "",
desc: "",
methods: [],
events: [
{
name: "e_swap_ListEvent",
args: [
{
type: "uint256",
name: "listingId",
},
{
type: "uint64",
name: "contractId",
},
{
type: "uint256",
name: "tokenId",
},
{
type: "uint64",
name: "contractId2",
},
{
type: "uint256",
name: "tokenId2",
},
{
type: "uint64",
name: "endTime",
},
],
},
],
};
const { CONTRACT } = await import("ulujs");
const ci = new CONTRACT(ctcInfoMp212, algodClient, indexerClient, spec);
const evts = await ci.getEvents({ minRound, address, sender: address });
const listEvents =
evts.find((el) => el.name === "e_swap_ListEvent")?.events || [];
if (listEvents.length > 0) await db.setInfo(key, Date.now());
}
case "swap_execute_once": {
// 000005:
const spec = {
name: "",
desc: "",
methods: [],
events: [
{
name: "e_swap_SwapEvent",
args: [
{
type: "uint256",
name: "listingId",
},
{
type: "address",
name: "holder1",
},
{
type: "address",
name: "holder2",
},
],
},
],
};
const { CONTRACT } = await import("ulujs");
const ci = new CONTRACT(ctcInfoMp212, algodClient, indexerClient, spec);
const evts = await ci.getEvents({ minRound, address, sender: address });
const swapEvents =
evts.find((el) => el.name === "e_swap_SwapEvent")?.events || [];
if (swapEvents.length > 0) await db.setInfo(key, Date.now());
break;
}
case "timed_sale_list_1minute": {
// 000006
if (!info) {
const status = await algodClient.status().do();
const { mp, arc72 } = await import("ulujs");
const ci = new mp(ctcInfoMp, algodClient, indexerClient);
const evts = await ci.ListEvent({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
address,
sender: address,
});
const fEvts = evts.filter((evt) => {
const addr = evt[6];
return addr === address;
});
if (fEvts.length < 1) break;
const fEvt = fEvts.pop();
const listTimestamp = fEvt[2];
const ciARC72 = new arc72(
data.contractId,
algodClient,
indexerClient
);
const evts2 = await ciARC72.arc72_Transfer({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
});
const fEvts2 = evts2.filter((evt) => {
const addrFrom = evt[3];
const addrTo = evt[4];
return (
addrFrom ===
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ" &&
addrTo === address
);
});
if (fEvts2.length < 1) break;
const fEvt2 = fEvts2.pop();
const mintTimestamp = fEvt2[2];
const threshold = 60; // !!
const elapsedTime = Math.abs(listTimestamp - mintTimestamp);
if (elapsedTime <= threshold) {
await db.setInfo(key, Date.now());
}
}
break;
}
case "timed_sale_list_15minutes": {
// 000007
if (!info) {
const status = await algodClient.status().do();
const { mp, arc72 } = await import("ulujs");
const ci = new mp(ctcInfoMp, algodClient, indexerClient);
const evts = await ci.ListEvent({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
address,
sender: address,
});
const fEvts = evts.filter((evt) => {
const addr = evt[6];
return addr === address;
});
if (fEvts.length < 1) break;
const fEvt = fEvts.pop();
const listTimestamp = fEvt[2];
const ciARC72 = new arc72(
data.contractId,
algodClient,
indexerClient
);
const evts2 = await ciARC72.arc72_Transfer({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
});
const fEvts2 = evts2.filter((evt) => {
const addrFrom = evt[3];
const addrTo = evt[4];
return (
addrFrom ===
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ" &&
addrTo === address
);
});
if (fEvts2.length < 1) break;
const fEvt2 = fEvts2.pop();
const mintTimestamp = fEvt2[2];
const threshold = 60 * 15; // !!
const elapsedTime = Math.abs(listTimestamp - mintTimestamp);
if (elapsedTime <= threshold) {
await db.setInfo(key, Date.now());
}
}
break;
}
case "timed_sale_list_1hour": {
// 000008
if (!info) {
const status = await algodClient.status().do();
const { mp, arc72 } = await import("ulujs");
const ci = new mp(ctcInfoMp, algodClient, indexerClient);
const evts = await ci.ListEvent({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
address,
sender: address,
});
const fEvts = evts.filter((evt) => {
const addr = evt[6];
return addr === address;
});
if (fEvts.length < 1) break;
const fEvt = fEvts.pop();
const listTimestamp = fEvt[2];
const ciARC72 = new arc72(
data.contractId,
algodClient,
indexerClient
);
const evts2 = await ciARC72.arc72_Transfer({
minRound: Math.max(0, (status["last-round"] || 0) - 1000),
});
const fEvts2 = evts2.filter((evt) => {
const addrFrom = evt[3];
const addrTo = evt[4];
return (
addrFrom ===
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ" &&
addrTo === address
);
});
if (fEvts2.length < 1) break;
const fEvt2 = fEvts2.pop();
const mintTimestamp = fEvt2[2];
const threshold = 60 * 60; // !!
const elapsedTime = Math.abs(listTimestamp - mintTimestamp);
if (elapsedTime <= threshold) {
await db.setInfo(key, Date.now());
}
}
break;
}
case "hmbl_pool_swap": {
// 000009
if (!info) {
const { swap } = await import("ulujs");
const ci = new swap(poolId, algodClient, indexerClient);
const evts = await ci.SwapEvents({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 10,
});
if (evts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_pool_swap_daily": {
const parentKey = `hmbl_pool_swap:${address}`;
const lastInfo = await db.getInfo(parentKey);
if (lastInfo) {
const now = Date.now();
const lastValue = Number(lastInfo.value);
const diff = Math.abs(now - lastValue);
console.log({ now, lastValue, diff });
const threshold = 86400 * 1000; // day_ms
if(diff > threshold) {
if(info) {
const lastValue = Number(info.value);
const { swap } = await import("ulujs");
const ci = new swap(poolId, algodClient, indexerClient);
const evts = await ci.SwapEvents({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 10,
});
if (evts.length > 0) {
await db.setInfo(key, lastValue + 1);
await db.setInfo(parentKey, Date.now());
}
} else {
await db.setInfo(key, 1);
await db.setInfo(parentKey, Date.now());
}
}
}
break;
}
case "hmbl_pool_add": {
// 000010
if (!info) {
const { swap } = await import("ulujs");
const ci = new swap(poolId, algodClient, indexerClient);
const evts = await ci.DepositEvents({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 10,
});
console.log(evts);
if (evts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_token_create": {
// 000011
if (!info) {
const { swap } = await import("ulujs");
const ci = new swap(tokenId, algodClient, indexerClient);
const evts = (
await ci.arc200_Transfer({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 1,
})
).slice(0, 1);
console.log(evts);
const fEvts = evts.filter((evt) => {
const addrFrom = evt[3];
const addrTo = evt[4];
return (
addrFrom ===
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ" &&
addrTo === address
);
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_pool_create": {
// 000012
if (!info) {
const { swap } = await import("ulujs");
const ci = new swap(poolId, algodClient, indexerClient);
const evts = (
await ci.arc200_Transfer({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 1,
})
)//.slice(0, 1);
console.log(evts);
const fEvts = evts.filter((evt) => {
const addrFrom = evt[3];
const addrTo = evt[4];
return (
addrFrom ===
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ" &&
addrTo === algosdk.getApplicationAddress(poolId)
);
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_farm_stake": {
// 000013
if (!info) {
const { abi, CONTRACT } = await import("ulujs");
const ci = new CONTRACT(
contractId,
algodClient,
indexerClient,
abi.stakr200
);
const evts = (
await ci.Stake({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 1,
})
).slice(0, 1);
const fEvts = evts.filter((evt) => {
const addr = evt[4];
return addr === address;
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_farm_claim": {
// 000014
if (!info) {
const { abi, CONTRACT } = await import("ulujs");
const ci = new CONTRACT(
contractId,
algodClient,
indexerClient,
abi.stakr200
);
const evts = (
await ci.Harvest({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 1,
})
).slice(0, 1);
const fEvts = evts.filter((evt) => {
const addr = evt[4];
return addr === address;
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "hmbl_farm_create": {
// 000015
if (!info) {
const { abi, CONTRACT } = await import("ulujs");
const ci = new CONTRACT(
contractId,
algodClient,
indexerClient,
abi.stakr200
);
const evts = (
await ci.Pool({
minRound: Math.max(0, (await getLastRound()) - 1000),
address,
sender: address,
limit: 1,
})
).slice(0, 1);
const fEvts = evts.filter((evt) => {
const addr = evt[4];
return addr === address;
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
break;
}
case "faucet_drip_once": {
// 000016
if (!info) {
const { arc200 } = await import("ulujs");
const ci = new arc200(data.contractId, algodClient, indexerClient);
const faucetAddress =
"2CMESXKIAZ5HLRKGGC3RBS7XYDXK5WPH3LRN4J4ICUHNTJQIYJQPH6KX3M";
const evts = await ci.arc200_Transfer({
minRound,
address: faucetAddress,
sender: faucetAddress,
});
const fEvts = evts.filter((evt) => {
const addrTo = evt[4];
return addrTo === address;
});
if (fEvts.length > 0) await db.setInfo(key, Date.now());
}
}
default:
break; // impossible
}
return res.status(200).json({ message: "ok" });
} catch (e) {
console.log(e);
return res.status(503).json({ message: "Something went wrong" });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});