-
Notifications
You must be signed in to change notification settings - Fork 89
/
balances.js
55 lines (43 loc) · 1.48 KB
/
balances.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
"use strict";
var BigNumber = require("bignumber.js");
const enumerable = require("linq");
module.exports.createBalances = async data => {
const balances = new Map();
const closingBalances = [];
const setDeposits = event => {
const wallet = event.to;
let deposits = (balances.get(wallet) || {}).deposits || new BigNumber(0);
let withdrawals = (balances.get(wallet) || {}).withdrawals || new BigNumber(0);
if (event.value) {
deposits = deposits.plus(new BigNumber(event.value));
balances.set(wallet, { deposits, withdrawals });
}
};
const setWithdrawals = event => {
const wallet = event.from;
let deposits = (balances.get(wallet) || {}).deposits || new BigNumber(0);
let withdrawals = (balances.get(wallet) || {}).withdrawals || new BigNumber(0);
if (event.value) {
withdrawals = withdrawals.plus(new BigNumber(event.value));
balances.set(wallet, { deposits, withdrawals });
}
};
for (const event of data.events) {
setDeposits(event);
setWithdrawals(event);
}
for (const [key, value] of balances.entries()) {
if (key === "0x0000000000000000000000000000000000000000") {
continue;
}
const balance = value.deposits.minus(value.withdrawals);
closingBalances.push({
wallet: key,
balance: balance.div(10 ** parseInt(data.decimals)).toFixed(data.decimals)
});
}
return enumerable
.from(closingBalances)
.orderByDescending(x => parseFloat(x.balance))
.toArray();
};