-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts-associated.js
40 lines (31 loc) · 1.32 KB
/
accounts-associated.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
// If using Node.js version 17 or earlier, import node-fetch
// Uncomment the line below if you need to install node-fetch (if using Node.js < 18)
// const fetch = require('node-fetch');
// Base API URL
const baseURL = 'https://mainnet-public.mirrornode.hedera.com/api/v1/tokens/0.0.5022567/balances?limit=100&order=desc';
// Function to fetch the token balances
async function fetchBalances(url) {
let totalAccounts = 0;
let nextURL = url;
// Continue fetching until there's no 'next' link
while (nextURL) {
try {
// Fetch the data from the current URL
const response = await fetch(nextURL);
const data = await response.json();
// Count the number of accounts in the current response
totalAccounts += data.balances.length;
// Log the current count of accounts
console.log(`Fetched ${data.balances.length} accounts, Total so far: ${totalAccounts}`);
// Check if there is a 'next' link for pagination
nextURL = data.links && data.links.next ? `https://mainnet-public.mirrornode.hedera.com${data.links.next}` : null;
} catch (error) {
console.error('Error fetching data:', error);
break;
}
}
// Final count of accounts
console.log(`Total number of accounts: ${totalAccounts}`);
}
// Call the function to start fetching and counting
fetchBalances(baseURL);