-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvoters.js
110 lines (98 loc) · 4.93 KB
/
voters.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
/*AIRDROP object example: (as declared in ./menu.js)
{ trx: false,
token_name: 'CommunityNodeToken',
token_abbr: 'TRUC',
token_id: '1000322',
token_precision: 0,
token_balance: 35000, //balance of the airdropper account (either for TRX or for TOKEN)
amount: '3500000',
criteria: ( CRITERIAS.VOTERS_PROPORTIONAL | CRITERIAS.VOTERS_EQUAL | CRITERIAS.HOLDERS | CRITERIAS.CSV)
SR_address: 'TDGy2M9qWBepSHDEutWWxWd1JZfmAed3BP',
SR_name: 'CommunityNode'
}
*/
var apilister = require("./apilist.js"); //download from apilist.tronscan.org
/**
* Returns voters data for a gives SR/candidate
*
* @param {*} aidrop A JS object containing all the airdrop configuration
* @returns A json containing a "data" array with all the <voters,amount> for the given @airdrop
*/
async function getVoters(airdrop){
var allPages = await apilister.getVoters(airdrop.SR_address);
var voters = allPages.data;
airdrop.SR_votersCount = allPages.total;
airdrop.SR_votesCount = allPages.totalVotes;
/* return rp(votes_options).catch(function (err) {//download the first page..
console.log(" Error getting voters list from " + votes_url ) ;
}).then(page => { //..to find out how many voters
airdrop.SR_votersCount = page.total;
airdrop.SR_votesCount = page.totalVotes; //we already had that value from 'menu.js' but may have changed, so better update
var numPages = Math.floor(page.total / 40);
var promises = [];
console.log("El numero de paginas solicitadas es : " + numPages);
for(var i = 1 ; i<=numPages; i++){
votes_options.qs.start = i*40; // prepare paginations: 40, 80, 120, 160, ..
promises.push(rp(votes_options)); //request paginated pages
}
return Promise.all(promises).then(function(otherPages) {
//Merge all 'otherPages' with the first 'page' (result lies in 'page')
otherPages.forEach(nextPage => { Array.prototype.push.apply(page.data, nextPage.data); } );
return page; //REMEMBER: Array.prototype.push.apply MODIFIES ¡page!
})
.catch(function(err) {
console.log(" Error getting voters list from " + votes_url + " err:" + err ) ;
})
.then(function( allPages) { */
airdrop["SR_total_votes"] = allPages.totalVotes;
//console.log({voters});
//---------------------------- THRESHOLD VOTING -------------------------------------------------
//Getting ready for the future THRESHOLD functionality
airdrop["SR_total_votes_filtered"] = airdrop.SR_votesCount; //Discard airdrop.SR_votesCount, since now we may (*)REMOVE some voters depending on user desires
var testing_sum = 0;
//(*)REMOVE voters depending on their number of votes (default: remove 0 votes voters)
//console.log("DEBUG: Remove voters with less than " + airdrop.votes_threshold + "? " + airdrop.remove_voters_with_THRESHOLD_votes);
//process.stdout.write("DEBUG: From number-of-voters: " + voters.length + " ... ");
/* FUNCIONALIDAD FUTURA: Remove voters below threshold TODO TODO TODO
if(airdrop.remove_voters_with_THRESHOLD_votes){
voters = voters.filter(voter => {
if(voter.votes > airdrop.votes_threshold) { airdrop["SR_total_votes_filtered"] += voter.votes; }
return (voter.votes > airdrop.votes_threshold); });
}
console.log("...to number-of-voters: " + voters.length + " after removing voters with votes > " + airdrop.votes_threshold );
*/
airdrop["num_wallets_filtered"] = voters.length; //Getting ready for the future THRESHOLD functionality
//---------------------------- THRESHOLD VOTING -------------------------------------------------
var arrTargets;
//Adjust rewards amount according to SUBCRITERIAS and trim out the relevant information:
switch (airdrop.criteria){
case airdrop.CRITERIAS.VOTERS_PROPORTIONAL:
//console.log("\nairdrop.amount: " + airdrop.amount)
//console.log("SR_total_votes_filtered: " + airdrop.SR_total_votes_filtered);
var reward_per_vote = airdrop.amount / airdrop.SR_total_votes_filtered;
//console.log("reward per vote: " + reward_per_vote)
arrTargets = voters.map( voter => {
var reward = voter.votes*reward_per_vote;
var amount = reward * Math.pow(10, airdrop.token_precision);
amount = Math.floor(amount);//discard the decimals
//console.log(airdrop.token_precision + " >>> " + voter.voterAddress + " reward: " + reward + " amount: " + amount);
testing_sum += amount;
return {address: voter.voterAddress, amount: amount};
});
break;
case airdrop.CRITERIAS.VOTERS_EQUAL:
var reward = airdrop.amount / airdrop.num_wallets_filtered;
var amount = reward * Math.pow(10, airdrop.token_precision);
amount = Math.floor(amount);//discard the decimals
arrTargets = voters.map( voter => {
testing_sum += amount;
return {address: voter.voterAddress, amount: amount};
});
break;
//case airdrop.CRITERIAS.HOLDERS:
//case Add here new CRITERIAS: ...
}
//DEBUG console.log(testing_sum);
return arrTargets;
}
module.exports.getVoters = getVoters;