-
Notifications
You must be signed in to change notification settings - Fork 4
/
airdropper.js
106 lines (90 loc) · 3.08 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
async function doAirdrop(tronWeb, airdrop){
//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 = "";
var air_result = {};
if (airdrop.isToken) {
air_result = await doAirdropToken(tronWeb, airdrop); //TRX
} else {
air_result = await doAirdropTRX(tronWeb, airdrop); //TOKENS
}
var success = [];
var failures = [];
air_result.forEach(tx => {
try {
if (tx.msg == "SUCCESS") {
success.push(tx);
} else {
failures.push(tx);
}
} catch (err) {
console.log('Error sorting transactions: ' + err);
failures.push(tx);
}
});
return {success: success, failures: failures};
}
//For TRX
async function doAirdropTRX(tronWeb, airdrop){
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");
return Promise.all(airdrop.list.map(wallet =>
tronWeb.trx.sendTrx(wallet.address, wallet.amount, function (xnul, res) {
if (xnul != null) {
wallet.msg = "Error: " + xnul;
wallet.ok = false;
wallet.tx = "";
} else {
wallet.ok = res.result;
if (res.result) {
wallet.msg = "SUCCESS";
wallet.tx = res.transaction.txID;
} else {
wallet.msg = "FAILURE"
wallet.tx = JSON.stringify(res);
}
}
return wallet;
})
));
}
//TOKENS
async function doAirdropToken(tronWeb, airdrop) {
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");
return Promise.all(airdrop.list.map(wallet =>
tronWeb.trx.sendToken(wallet.address, wallet.amount, airdrop.token_id, function (xnul, res) {
if (xnul != null) {
wallet.msg = "Error: " + xnul;
wallet.ok = false;
wallet.tx = "";
} else {
wallet.ok = res.result;
if (res.result) {
wallet.msg = "SUCCESS";
wallet.tx = res.transaction.txID;
} else {
wallet.msg = "FAILURE"
wallet.tx = JSON.stringify(res);
}
}
return wallet;
})
));
}
//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;