-
Notifications
You must be signed in to change notification settings - Fork 0
/
nft_check.js
61 lines (51 loc) · 1.74 KB
/
nft_check.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
const cross_fetch = require("cross-fetch");
const idlFactory = require('./Factories/ext.did');
const {HttpAgent, Actor} = require("@dfinity/agent");
const collections = require('./collections.json').collections;
const _ = require('lodash');
const bot_methods = require('./bot');
const db_tools = require("./db_tools");
const timeOut = 15; //Время в минутах
const checkNFT = async (address, collection) => {
const agent = createAgent();
let actor = createActor(agent, collection);
return actor.tokens(address);
}
const createAgent = () => {
return new HttpAgent({
host: 'https://raw.ic0.app',
fetch: cross_fetch.default
});
}
const createActor = (agent, collection) => {
return Actor.createActor(idlFactory, {
agent,
canisterId: collection.canisterId
})
}
const checkAllUsersNFT = async () => {
let users = await db_tools.getAllUsers();
await Promise.all(_.map(users, async value => {
let count = 0;
let addresses = await db_tools.getAddressesByUser(value.dataValues.id);
await Promise.all(addresses.map(async (address) => {
return await Promise.all(collections.map(async token => {
let data = await checkNFT(address.dataValues.address, token);
if (data.ok){
count += data.ok.length;
}
console.log(data);
}))
}))
if (count === 0){
await bot_methods.remove_role(value.dataValues.name, value.dataValues.discriminator);
await db_tools.removeRoleDBUser({principal: value.dataValues.principal});
}
}))
}
const init = () => {
setInterval(checkAllUsersNFT, timeOut * 60000);
}
module.exports = {
init
}