-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtakeIncome.js
135 lines (115 loc) · 3.8 KB
/
takeIncome.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
131
132
133
134
135
Eos = require('eosjs'); // Eos = require('./src')
fs = require("fs");
Papa = require("papaparse");
filename = "incomeReport-" + Date.now() + ".csv";
process.on('unhandledRejection', error => {
// Nodejs generic handler
});
// if you are running an airgrab, specify your contract
airgrab = false; //set to false if no airgrab
config = {
chainId: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906', // 32 byte (64 char) hex string
httpEndpoint: 'https://mainnet.genereos.io', //probably localhost
expireInSeconds: 60,
broadcast: true,
debug: false, // API and transactions
sign: true
}
eosClient = Eos(config);
async function IsRegistered(token,name) {
try {
return await eosClient.getCurrencyBalance(token, name);
} catch(e) {
return [];
}
}
async function GetDetails(name,tries=0) {
try {
const account = await eosClient.getAccount(name);
if(airgrab) {
const registered = await IsRegistered(airgrab,name);
account.registered = registered;
} else {
account.registered = [];
}
return account;
} catch(e) {
console.log(name + ": " + tries);
if(tries < 3) {
return await GetDetails(name,++tries);
} else {
return false;
}
}
}
async function GetProxyVotes(proxy) {
const account = await eosClient.getAccount(proxy);
if(account.voter_info.proxy) {
return await GetProxyVotes(account.voter_info.proxy);
} else {
return account.voter_info.producers.length
}
}
async function CreateRow(row) {
let name = row[0].trim()
if(name === '') return;
let details = await GetDetails(name);
if(details) {
try {
let eos = Number(details.core_liquid_balance ? details.core_liquid_balance.split(' ')[0] : 0);
let net = Number(details.self_delegated_bandwidth ? details.self_delegated_bandwidth.net_weight.split(' ')[0] : 0);
let cpu = Number(details.self_delegated_bandwidth ? details.self_delegated_bandwidth.cpu_weight.split(' ')[0] : 0);
let refundNet = Number(details.refund_request ? details.refund_request.net_amount.split(' ')[0] : 0);
let refundCpu = Number(details.refund_request ? details.refund_request.cpu_amount.split(' ')[0] : 0);
let proxyVotes = 0;
if(details.voter_info && details.voter_info.proxy) {
proxyVotes = await GetProxyVotes(details.voter_info.proxy);
}
let formatted = {
"name": details.account_name,
"eos": Number(eos.toFixed(4)),
"stake": Number((net+cpu).toFixed(4)),
"refund": Number((refundNet+refundCpu).toFixed(4)),
"totalEos": Number((net+cpu+eos+refundNet+refundCpu).toFixed(4)),
"totalRam": Number(details.ram_quota),
};
var write = Papa.unparse([formatted],{header:false});
fs.appendFile(__dirname + '/' + filename, '\n'+write, (err) => {
if (err) throw err;
});
} catch(e) {
console.log(JSON.stringify(details));
console.log(JSON.stringify(e));
}
} else {
//console.log("Skipping: " + name);
}
}
async function GetAllAccounts() {
fs.readFile(__dirname + '/' + 'income.csv', 'utf8', async function (err,csv) {
if (err) {
return console.log(err);
}
var data = Papa.parse(csv).data;
let total = data.length;
console.log("Found " + total + " accounts.");
let proc = 0;
fs.appendFile(__dirname + '/' + filename, 'name,eos,stake,totalEos,proxy,proxyVotes,votes,registered', (err) => {
if (err) throw err;
});
while(data.length > 0) {
let names = data.splice(0,100);
await Promise.all(
names.map(CreateRow)
);
proc += names.length;
process.stdout.write("Have processed " + proc + " of " + total + "\r");
}
console.log("Have processed " + proc + " of " + total);
console.log("Done");
});
}
async function run() {
await GetAllAccounts();
}
run();