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 pathdistribute.js
More file actions
154 lines (125 loc) · 5.22 KB
/
distribute.js
File metadata and controls
154 lines (125 loc) · 5.22 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
var fs = require('fs');
var csv = require('fast-csv');
var BigNumber = require('bignumber.js');
const polyDistributionArtifacts = require('../build/contracts/PolyDistribution.json');
const contract = require('truffle-contract');
let PolyDistribution = contract(polyDistributionArtifacts);
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
);
};
}
let polyDistributionAddress = process.argv.slice(2)[0];
let ALLOC_TYPE = parseInt(process.argv.slice(2)[1]);
if(!ALLOC_TYPE) ALLOC_TYPE = 0;
let allocData = new Array();
async function distributeTokens() {
console.log(`
--------------------------------------------
---------Performing allocations ------------
--------------------------------------------
`);
var sumDistributed = 0;
var sumAccountsDistributed = 0;
let accounts = await web3.eth.getAccounts();
let userBalance = await web3.eth.getBalance(accounts[0]);
let polyDistribution = await PolyDistribution.at(polyDistributionAddress);
console.log(allocData);
let block = await web3.eth.getBlock("latest");
let currentTime = block.timestamp;
console.log("Current block timestamp:",currentTime);
for(var i = 0;i< allocData.length;i++){
let prevAllocation = await polyDistribution.allocations(allocData[i][0],{from:accounts[0]});
if(prevAllocation[3].toNumber() == 0){
console.log('\x1b[31m%s\x1b[0m',"SKIPPED token distribution for account:",allocData[i][0]," No allocation has been made to this account");
}else if(currentTime < prevAllocation[1].toNumber()){
console.log('\x1b[31m%s\x1b[0m',"SKIPPED token distribution for account:",allocData[i][0],". Cliff unlock date not reached:", prevAllocation[1].toString(10));
}else if(prevAllocation[4].toNumber() >= prevAllocation[3].toNumber()){
console.log('\x1b[31m%s\x1b[0m',"SKIPPED token distribution for account:",allocData[i][0],". All tokens already claimed", prevAllocation[4].toString(10));
}else{
try{
console.log("Distributing vested tokens for account:",allocData[i][0]);
let receipt = await polyDistribution.transferTokens(allocData[i][0],{from:accounts[0], gas:200000, gasPrice: 10000000000});
if(receipt && receipt.logs.length >0){
let tokensClaimed = receipt.logs[0].args._amountClaimed.times(10 ** -18).toString(10);
console.log("Distributed", tokensClaimed, "tokens for account:",allocData[i][0]);
sumDistributed += parseInt(tokensClaimed);
sumAccountsDistributed +=1;
}else{
console.log('\x1b[31m%s\x1b[0m',"Failed to distribute vested POLY tokens for account:",allocData[i][0]);
}
} catch (err){
console.log(err);
}
}
}
console.log('\x1b[32m%s\x1b[0m',"Successfully distributed",sumDistributed, "POLY tokens to ", sumAccountsDistributed,"accounts");
}
function readFile() {
var stream;
//console.log(ALLOC_TYPE, "=====");
switch (ALLOC_TYPE) {
case 0: //PRESALE
stream = fs.createReadStream("data/presale.csv");
break;
case 1: //FOUNDER
stream = fs.createReadStream("data/founders.csv");
break;
case 2: // AIRDROP
break;
case 3: // ADVISOR
stream = fs.createReadStream("data/advisors.csv");
break;
case 4: // RESERVE
stream = fs.createReadStream("data/reserve.csv");
break;
case 5: // BONUS1
stream = fs.createReadStream("data/bonus1.csv");
break;
case 6: // BONUS2
stream = fs.createReadStream("data/bonus2.csv");
break;
case 7: // BONUS3
stream = fs.createReadStream("data/bonus3.csv");
break;
default:
}
let index = 0;
let batch = 0;
console.log(`
--------------------------------------------
------------- Parsing csv file -------------
--------------------------------------------
******** Removing beneficiaries without tokens or address data
`);
var csvStream = csv()
.on("data", function(data){
let isAddress = web3.utils.isAddress(data[0]);
if(isAddress && data[0]!=null && data[0]!='' ){
data[1] = parseInt(data[1]);
allocData.push([data[0],data[1]]);
}
})
.on("end", function(){
//Add last remainder batch
//console.log(allocData);
distributeTokens();
});
stream.pipe(csvStream);
}
if(polyDistributionAddress){
readFile();
}else{
console.log("Please run the script by providing the address of the PolyDistribution contract");
}