-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
79 lines (63 loc) · 2.01 KB
/
index.js
File metadata and controls
79 lines (63 loc) · 2.01 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
import 'dotenv/config';
import cron from 'node-cron';
import axios from 'axios';
// Launch task every day
cron.schedule(process.env.CRON, () => {
chooseWinner();
});
const chooseWinner = async () => {
const coins = await getCoins();
const delegators = await getDelegators(coins);
if (delegators.length > 0) {
const delegatorsFiltered = delegators
.filter(i => i.total_value >= 1000);
const winnerIndex = Math.floor(Math.random() * delegatorsFiltered.length);
const winner = delegatorsFiltered[winnerIndex];
// Do anything with the winner
} else {
// No delegators found
}
}
// EXPLORER_API = https://explorer-api.apps.minter.network/api/v1/
const getCoins = () => (
new Promise((resolve) => {
axios.get(`${process.env.EXPLORER_API}coins`)
.then((response) => resolve(response.data.data))
.catch((error) => {
// Catch error
});
}));
const getDelegators = (coins) => (
new Promise((resolve) => {
axios.get(`${process.env.EXPLORER_API}validators/${process.env.VALIDATOR}`)
.then((response) => {
const delegators = [];
const stakes = response.data.data.delegator_list;
// Get total BIP value for each delegator
stakes.forEach((d) => {
if (d.bip_value > 0 && verifyCoin(d.coin, coins)) {
const index = delegators.findIndex(i => i.address === d.address);
if (index === -1) {
delegators.push({
address: d.address,
total_value: +d.bip_value,
});
} else delegators[index].total_value += +d.bip_value;
}
});
delegators = delegators.map(i => i.total_value = +i.total_value.toFixed(4));
resolve(delegators);
})
.catch((error) => {
// Catch error
});
}));
const validCoins = [
'BIP',
'CONSULGAME',
];
const verifyCoin = (coin, coins) => {
if (validCoins.includes(coin)) return true;
if (coins.find(i => i.symbol === coin).crr >= 40) return true;
return false;
}