-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrackHederaNFTs.js
287 lines (231 loc) · 6.46 KB
/
trackHederaNFTs.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const fetch = require('cross-fetch');
const fs = require('fs');
const tokenFlatFileDB = [];
let totalToProcess = 0;
const maxRetries = 8;
// const rps = 25;
const nftOwnerMap = new Map();
const tokenStatPromiseArray = [];
const delim = '\t';
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fetchJson(url, depth = 0) {
if (depth >= maxRetries) {
console.log('TIMED OUT: ', url);
return null;
}
depth++;
try {
const res = await fetchWithTimeout(url);
if (res.status != 200) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
return res.json();
}
catch (err) {
await sleep(500 * depth);
return await fetchJson(url, depth);
}
}
async function fetchWithTimeout(resource, options = {}) {
const { timeout = 5000 } = options;
const controller = new AbortController();
const id = setTimeout(() => controller.abort(), timeout);
const response = await fetch(resource, {
...options,
signal: controller.signal,
});
clearTimeout(id);
return response;
}
async function getNFTList() {
// base URL
const baseUrl = 'https://mainnet-public.mirrornode.hedera.com';
let routeUrl = '/api/v1/tokens?limit=100';
const nftList = [];
do {
const json = await fetchJson(baseUrl + routeUrl);
if (json == null) {
console.log('FATAL ERROR: no NFTs found', baseUrl + routeUrl);
// unlikely to get here but a sensible default
return;
}
const tokens = json.tokens;
for (let t = 0; t < tokens.length; t++) {
if (tokens[t].type == 'NON_FUNGIBLE_UNIQUE') {
nftList.push(tokens[t].token_id);
}
}
routeUrl = json.links.next;
}
while (routeUrl);
return nftList;
}
async function getTokenStats(tokenId) {
const url = 'https://mainnet-public.mirrornode.hedera.com/api/v1/tokens/' + tokenId;
const json = await fetchJson(url);
if (json == null) {
console.log('ERROR processing: ', tokenId, url);
return;
}
const tS = json.total_supply || 0;
const mS = json.max_supply || 0;
const symbol = json.symbol || '';
const name = json.name || '';
const tsryAcc = json.treasury_account_id;
let royaltiesBool = 0;
let fbfBool = 0;
const customFees = json.custom_fees;
let royaltiesStr = '';
if (customFees.royalty_fees !== undefined) {
const royalties = customFees.royalty_fees;
royalties.forEach((item) => {
const numerator = item.amount.numerator;
const denom = item.amount.denominator || 0;
let fallbackAmt;
try {
fallbackAmt = item.fallback_fee.amount / 100000000;
fbfBool = 1;
}
catch (error) {
fallbackAmt = 'N/A';
}
let percentage;
if (denom == 0) {
percentage = 'N/A';
}
else {
percentage = `${numerator / denom * 100}%`;
}
royaltiesStr += `amount ${percentage} - fallback ${fallbackAmt} - paid to ${item.collector_account_id || 'N/A'}`;
royaltiesBool = 1;
});
if (!royaltiesBool) {royaltiesStr = 'NONE';}
}
else {
royaltiesStr = 'NONE';
}
let supply = 0;
if (tS > supply) {
supply = tS;
}
else if (mS > supply) {
supply = mS;
}
// insert the top level record into the database
// tokenId
// name
// supply
// maxSupply
// royalties boolean
// FBF boolean
// Royalties
// -> %
// -> FBF
// -> ID
// symbol
// treasury
tokenFlatFileDB.push(`\n${tokenId}${delim}${name}${delim}${supply}${delim}${mS}${delim}${royaltiesBool}${delim}${fbfBool}${delim}${royaltiesStr}${delim}${symbol}${delim}${tsryAcc}`);
}
async function getOwners(tokenId) {
// get the tokens supply
// no need to hold up the process but store the promises to block later
tokenStatPromiseArray.push(getTokenStats(tokenId));
// if supply == 0 then remove the limit
const baseUrl = 'https://mainnet-public.mirrornode.hedera.com';
let routeUrl = '/api/v1/tokens/' + tokenId + '/balances/?limit=100';
// now get the owners
// console.log(url);
totalToProcess--;
console.log(`Processing: ${tokenId} -> ${totalToProcess} remain...`);
do {
const json = await fetchJson(baseUrl + routeUrl);
if (json == null) {
console.log('FATAL ERROR: no NFTs found', baseUrl + routeUrl);
// unlikely to get here but a sensible default
return;
}
const balances = json.balances;
for (let b = 0; b < balances.length; b++) {
const balItem = balances[b];
const nftOwner = balItem.account;
const balance = balItem.balance;
// 2D array - total NFT / unique NFT
const ownerObject = nftOwnerMap.get(nftOwner);
let count;
let uniqueCount;
if (ownerObject === undefined) {
count = 0;
uniqueCount = 0;
}
else {
count = ownerObject[0];
uniqueCount = ownerObject[1];
}
if (balance > 0) { uniqueCount++; }
nftOwnerMap.set(nftOwner, [count + balance, uniqueCount]);
}
routeUrl = json.links.next;
}
while (routeUrl);
}
async function main() {
// find list of NFTs
const nftList = await getNFTList();
console.log(`found ${nftList.length} NFTs (ignoring serial numbers)`);
totalToProcess = nftList.length;
/*
const promiseArray = [];
for (let i = 0; i < nftList.length; i++) {
promiseArray.push(getOwners(nftList[i]));
if (i % rps == 0) await sleep(1000);
}
await Promise.all(promiseArray);
*/
for (let i = 0; i < nftList.length; i++) {
await getOwners(nftList[i]);
}
// same size and descending order
const thresholds = [1, 2, 5, 100, 500, 1000];
const counts = [0, 0, 0, 0, 0, 0];
let count = 0;
let countZero = 0;
let csvOutput = '';
nftOwnerMap.forEach(function(value, key) {
const totalBalance = value[0];
const uniqueCount = value[1];
count++;
csvOutput = csvOutput + `${key},${totalBalance},${uniqueCount}\n`;
console.log(key, totalBalance);
if (totalBalance == 0) {
countZero++;
return;
}
for (let i = 0; i < thresholds.length; i++) {
if (totalBalance >= thresholds[i]) {
counts[i]++;
}
else {
return;
}
}
});
console.log(`Total accounts owning an NFT: ${count - countZero}`);
for (let i = 0; i < thresholds.length; i++) {
console.log(`Total accounts owning >= ${thresholds[i]} an NFT: ${counts[i]}`);
}
console.log(`Total accounts owning no longer owning an NFT: ${countZero}`);
fs.writeFile('./walletOwnership.csv', csvOutput, () => {
console.log('Wallet File created');
});
let tokenOutput = `tokenId${delim}name${delim}supply${delim}mS${delim}royaltiesBool${delim}fbfBool${delim}royaltiesStr${delim}symbol${delim}treasuryId`;
for (let i = 0; i < tokenFlatFileDB.length; i++) {
tokenOutput += tokenFlatFileDB[i];
}
fs.writeFile('./tokenFlatFileDB.tsv', tokenOutput, () => {
console.log('token DB File created');
});
}
main();