forked from 0xSlot/erc721-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalances.js
More file actions
62 lines (47 loc) · 1.54 KB
/
Copy pathbalances.js
File metadata and controls
62 lines (47 loc) · 1.54 KB
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
"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;
value.withdrawals.forEach(withdrawal => {
const tokenIdIndex = tokenIds.indexOf(withdrawal);
if (tokenIdIndex < 0) return;
tokenIds.splice(tokenIdIndex, 1);
});
closingBalances.push({
wallet: key,
tokenIds
});
}
return closingBalances.filter(b => b.tokenIds.length > 0);
};