-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitting-data.js
58 lines (54 loc) · 1.87 KB
/
splitting-data.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
// fetching tokens sent to the address
app.get("/fetchGarage", async (req, res) => {
try {
const client = await initializeClient();
const totalSupply = await client
.publicApi()
.getDatastoreEntries([
{
key: "Counter",
address: erc721_address,
},
])
.then((data) => {
const byteArr = data[0].candidate_value;
const utf8Array = new Uint8Array(byteArr);
return parseInt(bytesToU64(utf8Array));
});
const batchSize = 1000;
const fetchPromises = [];
for (let i = 0; i < totalSupply; i += batchSize) {
const batchKeys = [];
for (let j = i; j < i + batchSize && j < totalSupply; j++) {
batchKeys.push({ address: collection_address, key: "ownerOf_" + j });
}
fetchPromises.push(client.publicApi().getDatastoreEntries(batchKeys));
}
const startTime = new Date().getTime();
Promise.all(fetchPromises)
.then((results) => {
const allData = results.flat();
const targetAddress = req.query.address;
const targetData = [];
allData.forEach((item, index) => {
const byteArr = item.candidate_value;
const utf8Array = new Uint8Array(byteArr);
const desData = bytesToStr(utf8Array);
if (desData === targetAddress) {
targetData.push({ index, address: targetAddress });
}
});
const endTime = new Date().getTime();
const durationInSeconds = (endTime - startTime) / 1000;
console.log(`The process took ${durationInSeconds} seconds.`);
res.status(200).json(targetData);
})
.catch((error) => {
console.error("Fetch error:", error);
});
} catch (error) {
// Return 500 (Internal Server Error) in case of error
console.log(error);
res.status(500).json({ error: "Internal Server Error" });
}
});