forked from 0xSlot/erc721-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 6
/
balances.js
63 lines (49 loc) · 1.61 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
57
58
59
60
61
62
63
"use strict";
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 = [];
const withdrawals = [].concat(value.withdrawals);
for (let i = 0, l = value.deposits.length; i < l; i++) {
const withdrawalIndex = withdrawals.indexOf(value.deposits[i]);
if (withdrawalIndex === -1) {
tokenIds.push(value.deposits[i]);
} else {
withdrawals.splice(withdrawalIndex, 1);
}
}
closingBalances.push({
wallet: key,
tokenIds
});
}
return closingBalances.filter((b) => b.tokenIds.length > 0);
};