forked from balancer/bal-mining-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum.js
92 lines (79 loc) · 2.89 KB
/
sum.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
const BigNumber = require('bignumber.js');
const Web3 = require('web3');
const fs = require('fs');
const fileService = require('./lib/fileService');
const { argv } = require('yargs');
BigNumber.config({
EXPONENTIAL_AT: [-100, 100],
ROUNDING_MODE: BigNumber.ROUND_DOWN,
DECIMAL_PLACES: 18,
});
function bnum(val) {
return new BigNumber(val.toString());
}
const END_BLOCK = argv.endBlock; // Closest block to reference time at end of week
const START_BLOCK = argv.startBlock; // Closest block to reference time at beginning of week
const WEEK = argv.week;
const BLOCKS_PER_SNAPSHOT = 256;
(async function () {
let userTotals = {};
let sortedUserTotal = {};
let sortedUserTotalPreRedirect = {};
let userBal = {};
let balTotal = bnum(0);
try {
// Get all files in report directory
for (i = END_BLOCK; i > START_BLOCK; i -= BLOCKS_PER_SNAPSHOT) {
const jsonString = fs.readFileSync(`./reports/${WEEK}/${i}.json`);
const report = JSON.parse(jsonString)[1];
Object.keys(report).forEach((user) => {
balTotal = balTotal.plus(bnum(report[user]));
if (userTotals[user]) {
userTotals[user] = bnum(userTotals[user])
.plus(bnum(report[user]))
.toString();
} else {
userTotals[user] = report[user];
}
});
}
Object.entries(userTotals)
.sort((a, b) => a[0] - b[0])
.forEach(([key, val]) => {
if (val > 0) {
sortedUserTotalPreRedirect[key] = val;
}
});
fileService.writeData(
sortedUserTotalPreRedirect,
`${WEEK}/_totalsPreRedirect`
);
const jsonRedirect = fs.readFileSync(`./redirect.json`);
const redirects = JSON.parse(jsonRedirect);
Object.keys(userTotals).forEach((user) => {
if (userTotals[user] == 0) {
delete userTotals[user];
}
if (redirects[user]) {
let newAddress = redirects[user];
if (userTotals[newAddress]) {
userTotals[newAddress] = bnum(userTotals[newAddress])
.plus(bnum(userTotals[user]))
.toString();
} else {
userTotals[newAddress] = userTotals[user];
}
delete userTotals[user];
}
});
Object.entries(userTotals)
.sort((a, b) => a[0] - b[0])
.forEach(([key, val]) => {
sortedUserTotal[key] = val;
});
console.log(`Total BAL distributed ${balTotal.toString()}`);
fileService.writeData(sortedUserTotal, `${WEEK}/_totals`);
} catch (e) {
console.error('Error reading reports', e);
}
})();