This repository was archived by the owner on Jul 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathairdrop.js
More file actions
177 lines (140 loc) · 6.11 KB
/
airdrop.js
File metadata and controls
177 lines (140 loc) · 6.11 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var fs = require('fs');
var csv = require('fast-csv');
var BigNumber = require('bignumber.js');
const polyDistributionArtifacts = require('../build/contracts/PolyDistribution.json');
const polyTokenArtifacts = require('../build/contracts/PolyToken.json');
const contract = require('truffle-contract');
let PolyDistribution = contract(polyDistributionArtifacts);
let PolyToken = contract(polyTokenArtifacts);
const Web3 = require('web3');
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
PolyDistribution.setProvider(web3.currentProvider);
//dirty hack for [email protected] support for localhost testrpc, see https://github.com/trufflesuite/truffle-contract/issues/56#issuecomment-331084530
if (typeof PolyDistribution.currentProvider.sendAsync !== "function") {
PolyDistribution.currentProvider.sendAsync = function() {
return PolyDistribution.currentProvider.send.apply(
PolyDistribution.currentProvider, arguments
);
};
}
PolyToken.setProvider(web3.currentProvider);
//dirty hack for [email protected] support for localhost testrpc, see https://github.com/trufflesuite/truffle-contract/issues/56#issuecomment-331084530
if (typeof PolyToken.currentProvider.sendAsync !== "function") {
PolyToken.currentProvider.sendAsync = function() {
return PolyToken.currentProvider.send.apply(
PolyToken.currentProvider, arguments
);
};
}
let polyDistributionAddress = process.argv.slice(2)[0];
let BATCH_SIZE = process.argv.slice(2)[1];
if(!BATCH_SIZE) BATCH_SIZE = 80;
let distribData = new Array();
let allocData = new Array();
let fullFileData = new Array();
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function setAllocation() {
console.log(`
--------------------------------------------
---------Performing allocations ------------
--------------------------------------------
`);
let accounts = await web3.eth.getAccounts();
let userBalance = await web3.eth.getBalance(accounts[0]);
let polyDistribution = await PolyDistribution.at(polyDistributionAddress);
//console.log("%%%%%%%%%%%%%%%",distribData);
//console.log(polyDistribution);
for(var i = 0;i< distribData.length;i++){
try{
let gPrice = 10000000000;
console.log("Attempting to allocate 250 POLYs to accounts:",distribData[i],"\n\n");
let r = await polyDistribution.airdropTokens(distribData[i],{from:accounts[0], gas:4500000, gasPrice: gPrice});
console.log("---------- ---------- ---------- ----------");
console.log("Allocation + transfer was successful.", r.receipt.gasUsed, "gas used. Spent:",r.receipt.gasUsed * gPrice,"wei");
console.log("---------- ---------- ---------- ----------\n\n")
} catch (err){
console.log("ERROR:",err);
}
}
console.log("Distribution script finished successfully.")
console.log("Waiting 2 minutes for transactions to be mined...")
await delay(90000);
console.log("Retrieving logs to inform total amount of tokens distributed so far. This may take a while...")
let polytokenAddress = await polyDistribution.POLY({from:accounts[0]});
let polyToken = await PolyToken.at(polytokenAddress);
var sumAccounts = 0;
var sumTokens = 0;
var eventData = new Array();
var events = await polyToken.Transfer({from: polyDistribution.address},{fromBlock: 0, toBlock: 'latest'});
events.get(function(error, log) {
event_data = log;
//console.log(log);
for (var i=0; i<event_data.length;i++){
//let tokens = event_data[i].args.value.times(10 ** -18).toString(10);
//let addressB = event_data[i].args.to;
sumTokens += event_data[i].args.value.times(10 ** -18).toNumber();
sumAccounts +=1;
eventData.push(event_data[i].args.to);
//console.log(`Distributed ${tokens} POLY to address ${addressB}`);
}
console.log(`A total of ${sumTokens} POLY tokens have been distributed to ${sumAccounts} accounts so far.`);
var eventData_s = new Set(eventData);
let missingDistribs = fullFileData.filter(x => !eventData_s.has(x));
if(missingDistribs.length >0){
console.log("************************");
console.log("-- No Transfer event was found for the followign accounts. Please review them manually --")
for(var i = 0; i<missingDistribs.length;i++){
console.log('\x1b[31m%s\x1b[0m',`No Transfer event was found for account ${missingDistribs[i]}`);
}
console.log("************************");
}
//console.log(`Run 'node scripts/verify_airdrop.js ${polyDistribution.address} > scripts/data/review.csv' to get a log of all the accounts that were distributed the airdrop tokens.`)
});
}
function readFile() {
var stream = fs.createReadStream("data/airdrop.csv");
let index = 0;
let batch = 0;
console.log(`
--------------------------------------------
--------- Parsing distrib.csv file ---------
--------------------------------------------
******** Removing beneficiaries without tokens or address data
`);
//console.log("QQQ",distribData);
var csvStream = csv()
.on("data", function(data){
let isAddress = web3.utils.isAddress(data[0]);
if(isAddress && data[0]!=null && data[0]!='' ){
allocData.push(data[0]);
fullFileData.push(data[0]);
index++;
if(index >= BATCH_SIZE)
{
distribData.push(allocData);
// console.log("DIS",distribData);
allocData = [];
// console.log("ALLOC",allocData);
index = 0;
}
}
})
.on("end", function(){
//Add last remainder batch
distribData.push(allocData);
allocData = [];
setAllocation();
});
stream.pipe(csvStream);
}
if(polyDistributionAddress){
console.log("Processing airdrop. Batch size is",BATCH_SIZE, "accounts per transaction");
readFile();
}else{
console.log("Please run the script by providing the address of the PolyDistribution contract");
}