-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (69 loc) · 2.04 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
const { sequelize, Cryptobot, User } = require("./models");
const { get } = require("axios");
async function main() {
await sequelize.authenticate();
try {
const res = await updateCryptobots();
console.log(res?.length ? res.length : 0, "cryptobots added to the DB.");
} catch (err) {
console.log(err);
}
}
async function updateCryptobots() {
/*
1. Loop over the Cryptobots.
2. For every Cryptobot, check if it's in the DB.
i. Yes, break.
ii. No ->
a. Find the user.
b. Create the Cryptobot.
c. Attach the Cryptobot, to the user.
*/
const ADDRESS = "KT1V6cNW5jTUxEwmMhxvNHkMF3Bkm5a9Cfrt";
offset = 0;
const allCryptobots = [];
while (true) {
console.log(`Current offset: ${offset}`);
const resp = await get(
`https://api.better-call.dev/v1/contract/mainnet/${ADDRESS}/tokens?offset=${
10 * offset
}`
);
const cryptobots = resp.data;
if (cryptobots === null || cryptobots?.length == 0) break;
allCryptobots.push(...cryptobots);
for (cryptobot of cryptobots) {
console.log("Indexing cryptobot", cryptobot.token_id);
let bot = await Cryptobot.findOne({
where: { token_id: cryptobot.token_id },
});
if (bot) {
console.log(`Bot-${cryptobot.token_id} already is in the DB.`);
return;
}
const dateCreated = new Date(cryptobot.timestamp);
bot = await Cryptobot.create({
token_id: cryptobot.token_id,
createdAt: dateCreated.toUTCString(),
});
console.log(`Bot-${bot.token_id} created.`);
if (!cryptobot?.creators) {
console.log("Bot doesn't have a creator.");
continue;
}
const user = await User.findOne({
where: { xtzAddress: cryptobot.creators[0] },
});
if (!user) {
console.log("User not found.");
continue;
}
await user.addCryptobot(bot);
console.log("Cryptobot added to user.");
}
console.log("Increased offset.");
offset += 1;
}
return allCryptobots;
}
main();