forked from CommunityNode/tronair-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairdropper.js
130 lines (118 loc) · 5.23 KB
/
airdropper.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var tw; //tronWeb
async function doAirdrop(tronWeb, airdrop){
var air_result = {};
//After dividing rewards by targets, some targets may have ended up receiving a few cents , dropped by Math.floor()
//TODO: store this into SQLite For now, remove them from the final list to avoid invoking API with 0 amount
//airdrop.list = sqliteZeroAmounts(airdrop.list);
//TENER EN CUENTA LA PRECISION para decidir si se ha de quitar o no
//if(airdrop.list.length >= 1){ //i.d: airdropping to little tokens (ie:500) amongst 1000 wallets would generate list.length = 0 !!
// air_test.msg = "";
if(airdrop.isToken){
air_result = await doAirdropToken(tronWeb, airdrop); //TRX
}else{
air_result = await doAirdropTRX(tronWeb, airdrop); //TOKENS
}
//}else{
// var msg = "Trying to EQUALLY airdrop too little tokens amongst too many wallets causes all wallets receive 0.xxx";
// air_test = {success: [], failures: [], msg: msg};
//}
return air_result;
}
//For TRX
async function doAirdropTRX(tronWeb, airdrop){ //WARNING: test not implemented!!
tw = tronWeb;
console.log("* * * tronair-cli * * * COMMUNITY NODE aidrop tool * * *");
console.log("* * * Airdropping \x1b[96m" + airdrop.token_name + "\x1b[97m (\x1b[96m" + airdrop.token_abbr + "\x1b[97m) to " + airdrop.list.length + " wallets");
var success = [];
var failures = [];
var all = Promise.all(airdrop.list.map(wallet => //sendTrx callback behaviour: https://github.com/tronprotocol/tron-web/blob/2.1.5/src/lib/trx.js#L686
tronWeb.trx.sendTrx(wallet.address, wallet.amount, function (xnul, res){ //closures mantain 'target' value
if(xnul != null){ //err
wallet.msg = xnul;
wallet.ok = false;
wallet.tx = "";
failures.push(wallet);
}else{
wallet.ok = res.result; //some kind of error may also have happen
if(res.result){ //all ok
wallet.msg = "SUCCESS";
wallet.tx = res.transaction.txID;
}else{
wallet.msg = "Something happened: " + JSON.stringify(res);
wallet.tx = ""; //no succesful tx
}
success.push(wallet);
}
//console.log((wallet.ok?"true ":"false") + " " + wallet.address + " " + wallet.amount + " reason: " + (wallet.ok?wallet.tx:wallet.msg));
return wallet;
}) //closure
)//map
).then(data => { //separate into SUCCESS and others
var success = [];
var failures = [];
data.forEach( tx => {
if(tx.msg == "SUCCESS"){
success.push(tx);
}else{
failures.push(tx);
}
});
return {success: success, failures: failures};
});
return all;
}
async function doAirdropToken(tronWeb, airdrop){//TOKENS
tw = tronWeb;
console.log("* * * tronair-cli * * * COMMUNITY NODE aidrop tool * * *");
console.log("* * * Airdropping \x1b[96m" + airdrop.token_name + "\x1b[97m (\x1b[96m" + airdrop.token_abbr + "\x1b[97m) to " + airdrop.list.length + " wallets");
var success = [];
var failures = [];
var all = Promise.all(airdrop.list.map(wallet => //sendTrx callback behaviour: https://github.com/tronprotocol/tron-web/blob/2.1.5/src/lib/trx.js#L686
tronWeb.trx.sendToken(wallet.address, wallet.amount, airdrop.token_id, function (xnul, res){ //closures mantain 'target' value
if(xnul != null){ //err
wallet.msg = xnul;
wallet.ok = false;
wallet.tx = "";
failures.push(wallet);
}else{
wallet.ok = res.result; //some kind of error may also have happen
if(res.result){ //all ok
wallet.msg = "SUCCESS";
wallet.tx = res.transaction.txID;
}else{
wallet.msg = "Something happened: " + JSON.stringify(res);
wallet.tx = ""; //no succesful tx
}
success.push(wallet);
}
//console.log((wallet.ok?"true ":"false") + " " + wallet.address + " " + wallet.amount + " reason: " + (wallet.ok?wallet.tx:wallet.msg));
return wallet;
}) //closure
)//map
).then(data => { //separate into SUCCESS and others
var success = [];
var failures = [];
data.forEach( tx => {
if(tx.msg == "SUCCESS"){
success.push(tx);
}else{
failures.push(tx);
}
});
return {success: success, failures: failures};
});
return all;
}
//TODO TODO
function sqliteZeroAmounts(arr){
console.log("Removing targets with infinitesimal amounts..");
var counter = 0;
var aux = arr.filter(x => {
if(x.amount <1) process.stdout.write("\x1b[96m\r" + (counter++) + ": Removing " + x.address + "\x1b[97m\r");
return x.amount > 1;
})
console.log("");
return aux;
}
module.exports.doAirdrop = doAirdrop;
module.exports.sqliteZeroAmounts = sqliteZeroAmounts;