forked from binodnp/erc20-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathbalances.js
56 lines (42 loc) · 1.4 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
56
"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 || [];
let withdrawals = (balances.get(wallet) || {}).withdrawals || [];
if (!event.tokenId) {
throw new TypeError('invalid tokenId value');
}
deposits = [...deposits, event.tokenId];
balances.set(wallet, { deposits, withdrawals });
};
const setWithdrawals = event => {
const wallet = event.from;
let deposits = (balances.get(wallet) || {}).deposits || [];
let withdrawals = (balances.get(wallet) || {}).withdrawals || [];
if (!event.tokenId) {
throw new TypeError('invalid tokenId value');
}
withdrawals = [...withdrawals, event.tokenId];
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 tokenIds = value.deposits.filter(x => !value.withdrawals.includes(x));
closingBalances.push({
wallet: key,
tokenIds
});
}
return closingBalances.filter(b => b.tokenIds.length > 0);
};